Cod sursa(job #235133)

Utilizator mist000000 mist Data 22 decembrie 2008 22:34:48
Problema Subsir crescator maximal Scor 65
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
	long N, *V;
	ifstream in("scmax.in");
	in >> N;
	V = new long[N];
	for (int 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 (int 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 (i > 0)
			P[i] = L[i - 1];
		else
			P[i] = -1;
		if (a == lmax)
			lmax++;
	}

	ofstream out("scmax.out");
	out << lmax << endl;
	for (int i = 0; i < lmax; i++)
		out << V[L[i]] << ' ';
	out << '\n';
	out.close();

	return 0;
}