Cod sursa(job #696170)

Utilizator valentin.harsanValentin Harsan valentin.harsan Data 28 februarie 2012 17:24:51
Problema Subsir crescator maximal Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.33 kb
#include<iostream>
#include<fstream>
#include<vector>
#include<algorithm>
using namespace std;

const int N = 100010;

ifstream in("scmax.in");
ofstream out("scmax.out");

struct el {
	int val,poz;
};

int n,aib[N],dr[N],dmax,d[N],smax,elmax;
el x[N];
vector<int> v;

void update(int val, int poz) {
	int p=poz;
	
	while(p<=n) {
		if(aib[p]<val+1) {
			aib[p]=val+1;
		}
		
		if(dr[p]<poz)
			dr[p]=poz;
		
		p+=p&(-p);
	}
}
int querry(int poz) {
	int rez=0;
	dmax=0;
	
	while(poz>0) {
		if(aib[poz]>rez)
			rez=aib[poz];
		
		if(dr[poz]>dmax)
			dmax=dr[poz];
		
		poz-=poz&(-poz);
	}
	return rez;
}

inline bool cmp(el a, el b) {
	if(a.val<b.val)
		return 1;
	if(a.val>b.val)
		return 0;
	if(a.poz>b.poz)
		return 1;
	return 0;
}
inline bool cmp2(el a, el b) {
	return a.poz<b.poz;
}

int main() {
	int i,a;
	
	in >> n;
	
	for(i=1;i<=n;++i) {
		in >> a;
		x[i].val=a; x[i].poz=i;
	}
	
	sort(&x[1],&x[n+1],cmp);
	
	for(i=1;i<=n;++i) {
		
		int tm=querry(x[i].poz-1);
		
		d[x[i].poz]=dmax;
		
		update(tm,x[i].poz);
		
		if(tm>smax) {
			smax=tm;
			elmax=x[i].poz;
		}
	}
	
	out << smax+1 << "\n";
	sort(&x[1],&x[n+1],cmp2);
	i=elmax;
	while(i!=0) {
		v.push_back(x[i].val);
		i=d[i];
	}
	
	for(i=v.size()-1;i>=0;--i)
		out << v[i] << " ";
	
	return 0;
}