Cod sursa(job #2479687)

Utilizator mircearoataMircea Roata Palade mircearoata Data 24 octombrie 2019 11:13:49
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.75 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream in("scmax.in");
ofstream out("scmax.out");

int n, m, maxx;
int v[100005];
int len[100005];
int index[100005], cnt;
int ans[100005], cnt1;

int main()
{
	in >> n;
	for (int i = 1; i <= n; i++)
		in >> v[i];
	for (int i = 1; i <= n; i++)
	{
		int pos = 0;
		for (int step = (1 << 30); step; step >>= 1)
			if (pos + step <= cnt && v[index[pos + step]] < v[i])
				pos += step;
		index[pos + 1] = i;
		if (pos + 1 > cnt)
			cnt++;
		len[i] = pos + 1;
		if (len[i] > maxx)
			maxx = len[i];
	}
	out << maxx << '\n';
	for (int i = n; i >= 1 && maxx; i--)
		if (len[i] == maxx)
		{
			ans[++cnt1] = v[i];
			maxx--;
		}
	while (cnt1)
		out << ans[cnt1--] << ' ';
}