Cod sursa(job #196503)

Utilizator andrei-alphaAndrei-Bogdan Antonescu andrei-alpha Data 27 iunie 2008 00:09:00
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#include <cstdio>
#define IN "scmax.in"
#define OUT "scmax.out"
#define N_MAX 1<<17
#define FOR(i,a,b) for(int i=a;i<=b;++i)

int v[N_MAX],best[N_MAX],pre[N_MAX],L[N_MAX];
int n,maxim, k,poz,pozmax=1,nr=1;

void scan()
{
    freopen(IN,  "r",stdin);
    freopen(OUT, "w",stdout);
	scanf("%d",&n);
	FOR(i,1,n) 
		scanf("%d", &v[i]);	
}

int caut(int x)
{
	int p=0, u, m;
	u = nr;
	while (p <= u)
	{
		m=(p+u)/2;
		if (v[L[m]] < x &&  v[L[m+1]] >= x) 
			return m;
		else 
			if (v[L[m+1]] < x) 
				p = m+1; 
			else 
				u = m-1;
	}
	return nr;
}

void solve()
{
	best[1]=L[1]=1; 
	
	FOR(i,2,n)
	{
		poz = caut(v[i]);
		pre[i] = L[poz];
		best[i] = poz+1;
		L[poz+1] = i;
		if ( nr < poz+1 )
			nr = poz+1;
		if(best[i] > maxim)
			maxim = best[i],
			pozmax = i;
	}
	
	printf("%d\n",maxim);		
}

void print(int i)
{
	if (pre[i]>0)  
		print(pre[i]);
	printf("%d ",v[i]);
}

int main()
{
    scan();
	solve();
	print(pozmax);
    return 0;   
}