Cod sursa(job #2749327)

Utilizator anetAneta Anghel anet Data 6 mai 2021 14:38:33
Problema Subsir crescator maximal Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.85 kb
#include <cstdio>
#define N 100005
 
using namespace std;
int L, a[N], poz, p[N], h[N], v[N], n;
 
inline int Bs(int x)
{
    int st = 1, dr = L, mj;
	int pos = 0;
    if (v[L] < x) return L + 1;
    while ( st <= dr)
    {
        mj = (st + dr)/2;
        if (x <= v[mj])
        { 
			pos = mj;
			st = mj + 1;
		}
        else 
			dr = mj - 1;
    }
    return pos;
}

int main()
{
    freopen("scmax.in","r", stdin);
    freopen("scmax.out","w", stdout);
 
    scanf("%d",&n);
 
    L = 0;
    for (int i = 1;i <= n; ++i)
    {
        scanf("%d",&a[i]);
		poz = Bs(a[i]);
        if (poz > L) 
			++L, poz = L;
 
        p[i] = poz;
        v[poz] = a[i];
    }
 
    printf("%d\n", L);
 
    poz = L;
 
    for (int i = n; i >= 1; --i)
		if (p[i] == poz) 
			h[poz] = a[i],
			--poz;
 
    for (int i = 1; i <= L; ++i) 
		printf("%d ", h[i]);
		
    return 0;
}