Cod sursa(job #2552627)

Utilizator CriviCriveanu Bogdan Crivi Data 21 februarie 2020 00:52:29
Problema Subsir crescator maximal Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include <bits/stdc++.h>

using namespace std;

ifstream in;
ofstream out;

vector <vector <int> > tree;

vector <int> gred;

int n;

int main()
{
	in.open("scmax.in");
	out.open("scmax.out");

	in >> n;
	int x, max1 = 1, poz = 1;
	in >> x;
	gred.push_back(x);
	tree.push_back({ x });
	for (int i = 2; i <= n; i++)
	{
		in >> x;
		auto it = upper_bound(gred.begin(), gred.end(), x);
		int poz1;
		poz1 = distance(gred.begin(), it);
		if (poz1 == 0)
		{
			gred.insert(gred.begin(), x);
			tree.insert(tree.begin(),{ x });
		}
		else
		{
			if (poz1 < gred.size())
			{
				gred[poz1 - 1] = x;
				tree[poz1 - 1].push_back(x);
				if (tree[poz1 - 1][tree[poz1 - 1].size() - 1] == tree[poz1 - 1][tree[poz1 - 1].size() - 2])
					tree[poz1 - 1].pop_back();
				if (tree[poz1 - 1].size() > max1)
				{
					max1 = tree[poz1 - 1].size();
					poz = poz1 - 1;
				}
			}
			else
			{
				gred.push_back(x);
				tree.push_back({ x });
			}
		}
		//out << poz1 << " ";

	}
	out << max1<<endl;
	for (int i = 0; i < tree[poz].size(); i++)
		out << tree[poz][i] << " ";
	//out << tree.size();
	return 0;
}