Cod sursa(job #182768)

Utilizator AndreyPAndrei Poenaru AndreyP Data 21 aprilie 2008 12:31:36
Problema Subsir crescator maximal Scor 65
Compilator cpp Status done
Runda Arhiva educationala Marime 1.01 kb
#include<stdio.h>
#define N 100010
int n,v[N],pre[N],st[N],lmax,ct,l;
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;
		}
	}
}
void copiaza()
{
	int i;
	for(i=0; i<lmax; i++)
		pre[i]=st[i];
}
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;
	citeste();
	st[0]=v[0];
	lmax=1;
	pre[0]=v[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++;
		}
		if(l>lmax)
		{
			lmax=l;
			copiaza();
		}
	}
	printf("%d\n",lmax);
	for(i=0; i<lmax-1; i++)
		printf("%d ",pre[i]);
	printf("%d\n",pre[lmax-1]);
	return 0;
}