Cod sursa(job #1701652)

Utilizator Pley01Nitu Madalin Pley01 Data 13 mai 2016 19:30:56
Problema Subsir crescator maximal Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.11 kb
#include<iostream>
#include<fstream>
#include<algorithm>

using namespace std;
#define NMax 100005

int n, res[NMax], v[NMax], lst[NMax], d[NMax], aib[NMax], bst, up[NMax];

void update(int x, int ind)
{
	for (; x <= n; x += x ^ (x-1)&x)
	if (d[ind] > d[aib[x]])
		aib[x] = ind;
}

int query(int x)
{
	int mx = 0;
	for (; x; x -= x ^ (x - 1)&x)
		mx = aib[x];
	return mx;
}

int main(void)
{
	int i, h = 1;
	ifstream fin("scmax.in");
	ofstream fout("scmax.out");

	cin >> n;
	for (i = 1; i <= n; ++i)
	{
		cin >> v[i];
		res[i] = lst[i] = v[i];
	}
	sort(lst + 1, lst + n + 1);
	for (i = 2; i <= n;++i)
	if (lst[i] != lst[h])
		lst[++h] = lst[i];

	for (i = 1; i <= n; ++i)
		v[i] = lower_bound(lst + 1, lst + h + 1, v[i]) - lst;

	for (i = 1; i <= n; ++i)
	{
		up[i] = query(v[i] - 1);
		d[i] = d[up[i]] + 1;
		update(v[i], i);
	}
	for (i = 1; i <= n;++i)
	if (d[bst] < d[i])
		bst = i;
	cout << d[bst];
	for (h = 0, i = bst; i; i = up[i])
		lst[++h] = res[i];
	for (i = h; i; --i)
		cout << lst[i];
	cout << endl;
	fin.close();
	fout.close();

	system("pause");
	return 0;
}