Cod sursa(job #461199)

Utilizator darrenRares Buhai darren Data 5 iunie 2010 21:27:34
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
#include<fstream>
#include<iterator>
using namespace std;

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

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

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