Cod sursa(job #513165)

Utilizator ZethpixZethpix Zethpix Data 15 decembrie 2010 11:20:16
Problema Subsir crescator maximal Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include <stdio.h>

int nq,pos,v[100010],P[100010],i,N,Q[100010];

int BS(int L,int R,int x)
{
	int M;
	if(L==R) return R;
	M=(L+R)/2;
	if(x<Q[M]) return BS(L,M,x);
	else
	if(x>Q[M]) return BS(M+1,R,x);
	else
	if(x==Q[M]) return M;
}

int main()
{
	freopen("scmax.in","r",stdin);
	freopen("scmax.out","w",stdout);
	
	scanf("%d",&N);
	for(i=1;i<=N;i++) scanf("%d",&v[i]);
	
	Q[1]=v[1];
	P[1]=1;
	nq=1;
	
	for(i=2;i<=N;i++)
		if(v[i]>Q[nq])
		{
			nq++;
			Q[nq]=v[i];
			P[i]=nq;
		}
		else
		{
			pos=BS(1,nq,v[i]);
			Q[pos]=v[i];
			P[i]=pos;
		}
	
	printf("%d\n",nq);
	pos=nq-1;
	Q[nq]=v[N];
	i=N-1;
	while(pos>0)
	{
		while(P[i]==pos&&v[i]<=Q[pos]) i--;
		Q[pos-1]=v[i];
		pos--;
	}
	
	for(i=1;i<=nq;i++)
		printf("%d ",Q[i]);
	
	return 0;
}