Cod sursa(job #1317495)

Utilizator taigi100Cazacu Robert taigi100 Data 14 ianuarie 2015 22:24:36
Problema Subsir crescator maximal Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.28 kb
/*
	Keep It Simple!
*/

#include <fstream>
#include <iostream>

using namespace std;

const int const kMax_N = 100005;

int value[kMax_N], smallest[kMax_N], precedent[kMax_N],longest[kMax_N];
int maxlength,n;

void ReadData()
{
	ifstream fin("scmax.in");
	fin >> n;
	for (int i = 1; i <= n; ++i)
		fin >> value[i];
	fin.close();
}

int binary_length_search(int poz)
{
	int lo = 1;
	int hi = maxlength;
	while (lo <= hi)
	{
		int mid = (lo + hi) / 2;
		if (value[smallest[mid]] < value[poz])
			lo = mid + 1;
		else
			hi = mid - 1;
	}
	return lo;
}

void Computelis_length()
{
	smallest[1] = precedent[1] = 1;
	int newl = 0;
	for (int i = 2; i <= n; ++i)
	{
		newl = binary_length_search(i);

		precedent[i] = smallest[newl - 1];
		smallest[newl] = i;

		if (newl > maxlength)
			maxlength = newl;
	}
}

void Computelis()
{
	int k = smallest[maxlength];
	for (int i = maxlength; i >= 1; i--)
	{
		longest[i] = value[k];
		k = precedent[k];
	}
}

void PrintResult()
{
	ofstream fout("scmax.out");
	fout << maxlength << '\n';
	for (int i = 1; i <= maxlength; ++i)
		fout << longest[i] << ' ';
	fout.close();
}

void Solve()
{
	ReadData();
	Computelis_length();
	Computelis();
	PrintResult();
}

int main()
{
	Solve();
	return 0;
}