Cod sursa(job #1131594)

Utilizator axnsanCristi Vijdea axnsan Data 28 februarie 2014 21:54:20
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.24 kb
#ifdef __INFOARENA_PROJ
#include "infoarena.h"
#endif

#include <fstream>
#include <algorithm>

#ifdef __INFOARENA_PROJ
namespace scmax {
#endif

const unsigned int maxN = 100000;
unsigned X[maxN + 1];

/*M[j] == pozitia celei mai mici valori din sirul X in care se termina
	un subsir crescator de lungime j*/
unsigned M[maxN + 1];

/*P[j] == predecesorul celui de-al j-lea lement din X (Xj) in cel mai lung
	subsir crescator care se termina in Xj*/
unsigned P[maxN + 1];

void print(unsigned mL, std::ostream& out)
{
	if (mL == 0)
		return;

	print(P[mL], out);
	out << X[mL] << ' ';
}

int main()
{
	std::ifstream in("scmax.in");
	std::ofstream out("scmax.out");
	unsigned N;
	in >> N;
	for (unsigned i = 1; i <= N; ++i)
	{
		in >> X[i];
	}
	unsigned L = 0;
	for (unsigned i = 1; i <= N; ++i)
	{
		unsigned st = 1, dr = L, j = 0;
		while (st < dr)
		{
			unsigned mij = (st + dr) / 2;
			if (X[M[mij]] < X[i])
				st = mij + 1;
			else dr = mij;
		}
		if (st == dr)
			j = st;
		if (X[M[j]] >= X[i] && j > 0)
			--j;

		P[i] = M[j];
		if (j == L || X[i] < X[M[j + 1]])
			M[j + 1] = i, L = std::max(L, j + 1);
	}
	out << L << '\n';
	print(M[L], out);
	return 0;
}


#ifdef __INFOARENA_PROJ
}
#endif