Cod sursa(job #2518614)

Utilizator Dragos1226Dragos Chileban Dragos1226 Data 6 ianuarie 2020 00:49:45
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.29 kb
#include<fstream>
#include<iostream>
#include<algorithm>
using namespace std;
ifstream in("scmax.in");
ofstream out("scmax.out");
const int NMax = 100005;
int n, arr[NMax], cpy[NMax], lst[NMax], DP[NMax], AIB[NMax], sol, up[NMax];

void update(int x, int ind)
{
	for (int i = x; i <= n; i += (i&(-i)))
		if (DP[ind] > DP[AIB[i]])
			AIB[i] = ind;
}

int query(int x)
{
	int mx = 0;

    while (x) {
        if (DP[AIB[x]] > DP[mx])
            mx = AIB[x];
        x -= (x&(-x));
    }

	return mx;
}


int main() {
    in >> n;
    for (int i = 1; i <= n; i++) {
        in >> arr[i];
        cpy[i] = lst[i] = arr[i];
    }

    sort(lst+1,lst+1+n);

    int h = 1;
    for (int i = 2; i <= n; i++)
        if (lst[i] != lst[h])
            lst[++h] = lst[i];

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

    for (int i = 1; i <= n; i++) {
        up[i] = query(arr[i]-1);
        DP[i] = DP[up[i]]+1;
        update(arr[i], i);
    }

    for (int i = 1; i <= n; i++)
        if (DP[i] > DP[sol])
            sol = i;
    out << DP[sol] << '\n';
    int i = sol;
    h = 0;
    while (i) {
        lst[++h] = cpy[i];
        i = up[i];
    }

    for (i = h; i; --i)
        out << lst[i] << " ";
}