Cod sursa(job #182958)

Utilizator AndreyPAndrei Poenaru AndreyP Data 21 aprilie 2008 16:06:52
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.05 kb
#include<stdio.h>
#define N 100010
int n,v[N],pre[N],st[N],lmax,ct,l,d[N];
char c[2000000];
void citeste()
{
	int i,nr=-1,aux=0;
	fgets(c,2000000,stdin);
	for(i=0; c[i]!='\0'; i++)
	{
		if((c[i]>='0')&&(c[i]<='9'))
			aux=aux*10+(c[i]-'0');
		else
		{
			v[++nr]=aux;
			aux=0;
		}
	}
}
int caut(int x)
{
	int i,step;
	for(step=1; step<ct; step<<=1);
	for(i=0; step; step>>=1)
		if((i+step<ct)&&(st[i+step]<=x))
			i+=step;
	if(st[i]<x)
		i++;
	return i;
}
int main()
{
	freopen("scmax.in","r",stdin);
	freopen("scmax.out","w",stdout);
	scanf("%d\n",&n);
	int i,t;
	citeste();
	st[0]=v[0];
	lmax=1;
	pre[0]=0;
	for(i=1; i<n; i++)
	{
		if(v[i]>st[ct])
		{
			st[++ct]=v[i];
			l=ct+1;
		}
		else
		{
			l=caut(v[i]);
			st[l]=v[i];
			l++;
		}
		pre[i]=l-1;
		if(l>lmax)
		{
			lmax=l;
			t=v[i];
		}
	}
	printf("%d\n",lmax);
	i--;
	t=lmax;
	lmax--;
	while(lmax!=-1)
	{
		while(pre[i]!=lmax)
			i--;
		d[lmax]=v[i];
		lmax--;
	}
	for(i=0; i<t; i++)
		printf("%d ",d[i]);
	printf("\n");
	return 0;
}