Cod sursa(job #1550467)

Utilizator daniel.grosuDaniel Grosu daniel.grosu Data 13 decembrie 2015 19:33:21
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 2.17 kb
#define REP(a,b) for(int a=0; a<(b); ++a)
#define FWD(a,b,c) for(int a=(b); a<(c); ++a)
#define FWDS(a,b,c,d) for(int a=(b); a<(c); a+=d)
#define BCK(a,b,c) for(int a=(b); a>(c); --a)
#define ALL(a) (a).begin(), (a).end()
#define SIZE(a) ((int)(a).size())
#define VAR(x) #x ": " << x << " "
#define FILL(x,y) memset(x,y,sizeof(x))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
#define FAST ios_base::sync_with_stdio(0);cin.tie(0);
#define x first
#define y second
#define st first
#define nd second
#define pb push_back
#define nleft (n<<1)
#define nright (nleft+1)
#define lsb(a) (a&-a)
  
#include <vector>
#include <cmath>
#include <time.h>
#include <algorithm>
#include <string.h>
#include <set>
#include <map>
#include <queue>
  
using namespace std;
#ifndef ONLINE_JUDGE
#include<fstream>
ifstream cin("scmax.in");
ofstream cout("scmax.out");
#else
#include<iostream>
#endif
  
const int NMAX = 100005;
const int sep = 200000;
const int INF = 0x3f3f3f3f;
const int dx[] = {0,0,-1,1}; //1,1,-1,1};
const int dy[] = {-1,1,0,0}; //1,-1,1,-1};
  
typedef long long LL;
typedef pair<int, int> PII;
typedef long double K;
typedef pair<K, K> PKK;
typedef vector<int> VI;

int bit[NMAX];
int A[NMAX],norm[NMAX];
int res[NMAX];
int b[NMAX];
int q[NMAX], k;
int pre[NMAX];

int rs,n;

void update(int x, int val)
{
	for(; x<=n;  x += x^(x-1) & x){
		if(b[bit[x]] < b[val])
			bit[x]=val;
	}
}

int query(int x)
{
	int m=0;
	for(;x; x -= x^(x-1) & x)
	{
		if(b[bit[x]] > b[m])
			m=bit[x];
	}
	return m;
}
int h,i;
int main(void)
{
	FAST;
	clock_t t1, t2;
	t1=clock();
	cin>>n;
	for(i=1; i<=n; ++i){
		cin>>A[i];
		res[i] = norm[i] = A[i];
	}
	sort(norm+1, norm+1+n);

	for(h=1, i=2; i<=n; ++i)
		if(norm[i] != norm[h])
			norm[++h] = norm[i];

	for(i=1; i<=n; ++i)
		A[i] = lower_bound(norm+1, norm+h+1, A[i]) - norm;

	for(i=1; i<=n; ++i)
	{
		pre[i] = query(A[i]-1);
		b[i] = b[pre[i]]+1;
		update(A[i], i);
	}
	rs=0;
	for(i=1; i<=n; ++i)
		if(b[rs] < b[i])
			rs=i;

	cout<<b[rs]<<" \n";
	k=0;
	for(i=rs; i; i=pre[i])
		q[++k]=res[i];
	for(i=k; i; --i)
		cout<<q[i]<<" ";
	t2=clock();
	cout<<"\n";

	float diff = ((float)t2-(float)t1);
	//cout<<"\n"<<diff/CLOCKS_PER_SEC<<"\n";
	return 0;
}