Cod sursa(job #795212)

Utilizator radustn92Radu Stancu radustn92 Data 7 octombrie 2012 20:23:40
Problema Subsir crescator maximal Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 0.93 kb
#include <stdio.h>
#include <algorithm>
#define NMAX 100005
using namespace std;
int n,A[NMAX],best[NMAX],ant[NMAX],val[NMAX],r;
int rez,rez_poz;
void read()
{
	scanf("%d",&n);
	int i;
	for (i=1; i<=n; i++)
		scanf("%d",&A[i]);
}
int cbin(int x)
{
	int i,step;
	for (step=1; step<=r; step<<=1);
	for (i=0; step; step>>=1)
		if (i+step<=r && A[val[i+step]]<x)
			i+=step;
	return ++i;
}
void recons(int poz)
{
	if (poz==0)
		return ;
	recons(ant[poz]);
	printf("%d ",A[poz]);
}
void solve()
{
	int i,poz;
	val[1]=1; best[1]=1;
	for (i=2; i<=n; i++)
	{
		if (A[i]>A[val[r]])
		{
			val[++r]=i;
			poz=r;
		}
		else
		{
			poz=cbin(A[i]);
			val[poz]=i;
		}
		best[i]=poz;
		if (best[i]>rez)
			rez=best[i],rez_poz=i;
		ant[i]=val[poz-1];
	}
	printf("%d\n",rez);
	recons(rez_poz);
	printf("\n");
}
int main()
{
	freopen("scmax.in","r",stdin);
	freopen("scmax.out","w",stdout);
	read();
	solve();
	return 0;
}