Cod sursa(job #235142)

Utilizator mist000000 mist Data 22 decembrie 2008 22:50:06
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 kb
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
	long N, *V;
	ifstream in("scmax.in");
	in >> N;
	V = new long[N];
	for (long i = 0; i < N; i++)
		in >> V[i];
	in.close();

	// P holds parent's index
	// L[i] holds last index in longest sequence of length i
	long *P = new long[N], *L = new long[N], lmax = 0;

	for (long i = 0; i < N; i++) {
		long number = V[i];
		// find position where V[i] should be inserted
		// that is find lowest number higher than V[i]

		// binary search
		long a = 0, b = lmax - 1;
		while (a <= b) {
			long m = (a + b) / 2;
			if (number < V[L[m]])
				b = m - 1;
			else if (number > V[L[m]])
				a = m + 1;
			else {
				a = m;
				b = m - 1;
			}
		}

		// result is at position a
		L[a] = i;
		if (a > 0)
			P[i] = L[a - 1];
		else
			P[i] = -1;
		if (a == lmax)
			lmax++;
	}

	ofstream out("scmax.out");
	out << lmax << '\n';
	if (lmax > 0) {
		long *SC = new long[lmax];
		long pos = lmax - 1;
		long ind = L[lmax - 1];
		do {
			SC[pos--] = V[ind];
			ind = P[ind];
		} while (ind > -1);
		for (pos = 0; pos < lmax; pos++)
			out << SC[pos] << ' ';
		out << '\n';
	}
	out.close();

	return 0;
}