Cod sursa(job #3185680)

Utilizator verde.cristian2005Verde Flaviu-Cristian verde.cristian2005 Data 19 decembrie 2023 20:25:09
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.44 kb
#include <bits/stdc++.h>
using namespace std;

#ifndef HOME
	ifstream in("scmax.in");
	ofstream out("scmax.out");
	#define cin in
	#define cout out
#endif

int best[100001];
int v[100001], vec[100001];

int n;

int cautbin(int val)
{
	int r = 0, pas = 1 << 16;
	while(pas)
	{
		if(r + pas <= n && vec[r + pas] <= val)
			r += pas;
		pas /= 2;
	}
	return r;
}

int aib[100001];

int lsb(int x)
{
	return x & -x;
}

void update(int poz, int max1)
{
	for(; poz <= n; poz += lsb(poz))
		aib[poz] = max(aib[poz], max1);
}

int query(int poz)
{
	int max1 = 0;
	for(; poz > 0; poz -= lsb(poz))
		max1 = max(max1, aib[poz]);
	return max1;
}

int afis[100001];
int rasp[100001];

int main()
{
#ifdef HOME
    freopen("test.in", "r", stdin);
    freopen("test.out", "w", stdout);
#endif // HOME
    int max1 = 0;
	cin >> n;
	for(int i = 1; i <= n; i++)
	{
		cin >> v[i];
		vec[i] = v[i];
	}
	sort(vec + 1, vec + n + 1);
	for(int i = 1; i <= n; i++)
		v[i] = cautbin(v[i]);
	for(int i = 1; i <= n; i++)
	{
		rasp[i] = query(v[i] - 1) + 1;
		best[v[i]] = max(best[v[i]], rasp[i]);
		max1 = max(max1, best[v[i]]);
		update(v[i], best[v[i]]);
	}
	cout << max1 << '\n';
	int cnt = 0, last = 1e9;
	for(int i = n; i >= 1; i--)
	{
		if(v[i] < last && rasp[i] == max1)
		{
			max1--;
			afis[++cnt] = vec[v[i]];
			last = v[i];
		}
	}
	for(int i = cnt; i >= 1; i--)
		cout << afis[i] << " ";
    return 0;
}