Cod sursa(job #196417)

Utilizator silvia_the_bestSilvia Pripoae silvia_the_best Data 26 iunie 2008 13:25:08
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.76 kb
#include <stdio.h>
#define N 100010
int n,v[N],x[N],nr;
void read(){
	int i;
	freopen("scmax.in","r",stdin);
	scanf("%d",&n);
	for (i=1;i<=n;++i)
		scanf("%d",&v[i]);
}
int caut(int y){
	int p=1,u=nr,m;
	while (p!=u){
		m=(p+u)/2;
		if (y<=v[x[m]])
			u=m;
		else
			p=m+1;
	}
	if (v[x[p]]<y)
		++p;
	return p;
}
int lung[N],pred[N];
void rezolva(){
	int p,i;
	x[++nr]=1;x[0]=-1;pred[1]=-1;
	for(i=2;i<=n;++i){
		p=caut(v[i]);
		lung[i]=p;
		pred[i]=x[p-1];
		if(p==nr+1)
			++nr;
		x[p]=i;
	}
}
void predecesor(int x){
	if (pred[x]==-1);
	else
		predecesor(pred[x]);
	printf("%d ",v[x]);
}
void write(){
	freopen("scmax.out","w",stdout);
	printf("%d\n",nr);
	predecesor(x[nr]);
}
int main(){
	read();
	rezolva();
	write();
}