Cod sursa(job #2492495)

Utilizator Ykm911Ichim Stefan Ykm911 Data 14 noiembrie 2019 20:26:01
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.17 kb
#include <bits/stdc++.h>
#define NMax 100005

using namespace std;

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


int N, Res[NMax], v[NMax], lst[NMax], D[NMax], AIB[NMax], bst, up[NMax];

void update(int x, int ind)
{
    while(x <= N)
    {
        if(D[ind] > D[AIB[x]])
            AIB[x] = ind;
        x += x & (-x);
    }
}

int query(int x)
{
    int s = 0;
    while(x > 0)
    {
        if(D[AIB[x]] > D[s])
            s = AIB[x];
        x -= x & (-x);
    }
	return s;
}

int main(void)
{
	int i, h = 1;
	fin >> N;
	for (i = 1; i <= N; ++i)
	{
	    fin >> 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;
	fout << D[bst] << "\n";
	for (h = 0, i = bst; i; i = up[i])
		lst[++h] = Res[i];
	for (i = h; i; --i)
        fout << lst[i] <<" ";
    fin.close();
    fout.close();
	return 0;
}