Cod sursa(job #461164)

Utilizator darrenRares Buhai darren Data 5 iunie 2010 19:57:12
Problema Subsir crescator maximal Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include<fstream>
#include<iterator>
using namespace std;

ofstream fout("scmax.out");
void reconst(int i);

int n;
int a[100001], s[100001], t[100001], sz = 0;
int main()
{
	ifstream fin("scmax.in");
	fin >> n;
	for (int i = 1; i <= n; ++i)
		fin >> a[i];
	
	s[++sz] = a[1];
	for (int i = 2; i <= n; ++i)
		if (a[i] > s[sz])
		{
			s[++sz] = a[i];
			t[a[i]] = s[sz - 1];
		}
		else
		{
			int step, j;
			for (step = 1; step << 1 <= sz; step <<= 1);
			for (j = 0; step; step >>= 1)
				if (j + step <= sz && s[j + step] > a[i])
					j += step;
			if (j != 0)
				s[j] = a[i],
				t[a[i]] = s[j - 1];
		}
	fout << sz << '\n';
	reconst(s[sz]);
	fout << s[sz] << ' ';
}

void reconst(int i)
{
	if (t[i] == 0)
		return;
	reconst(t[i]);
	fout << t[i] << ' ';
}