Cod sursa(job #634848)

Utilizator nandoLicker Nandor nando Data 17 noiembrie 2011 15:46:20
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.56 kb
#include <iostream>
#include <algorithm>
#include <fstream>
#include <vector>
#include <map>
using namespace std;

fstream fin("scmax.in", ios::in);
fstream fout("scmax.out", ios::out);

#define MAXN 100100
#define zero(x) (x ^ (x - 1) & x)
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define min(a, b) (((a) < (b)) ? (a) : (b))

int n;
int a[MAXN], b[MAXN], aib[MAXN], len[MAXN], prev[MAXN];
vector<pair<int, int> > norm;

void normalize()
{
	sort (norm.begin() + 1, norm.end());
		
	for (int i = 1; i <= n; ++i) {
		int x = norm[i].first, v = norm[i].second, k = i;
		while (x == norm[i].first) {
			b[norm[i].second] = k;
			++i;
		}
		--i;
	}
}

int query (int pos)
{
	int v = 0;
	while (pos > 0) {
		if (len[v] < len[aib[pos]]) {
			v = aib[pos];	
		}
		pos -= zero(pos);	
	}
	
	return v;
}

void update (int value, int pos)
{
	while (pos <= n) {
		if (len[aib[pos]] < len[value]) {
			aib[pos] = value;
		}
		pos += zero(pos);	
	}
}

int main()
{
	fin >> n;
	
	norm.resize (n + 1);
	for (int i = 1; i <= n; ++i) {
		fin >> a[i];
		norm[i] = make_pair(a[i], i);
	}
	
	normalize ();
	
	int maxp = 1;
	for (int i = 1; i <= n; ++i) {
		int j = query(b[i] - 1);
		len[i] = len[j] + 1;
		prev[i] = j;
		update (i, b[i]);
		
		if (len[maxp] < len[i]) {
			maxp = i;	
		}
	}
	
	for (int i = 0, j = maxp; j; j = prev[j], ++i) {
		aib[i] = a[j];
	}
	
	fout << len[maxp] << endl;
	for (int i = len[maxp] - 1; i >=0; --i) {
		fout << aib[i] << " ";
	}
	fout << endl;
	
	fin.close();
	fout.close();
	return 0;   
}