Cod sursa(job #2721788)

Utilizator Cosmin2004_InfoMoldoveanu Cosmin Cosmin2004_Info Data 12 martie 2021 11:26:52
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.27 kb
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;
const int NMAX = 100000;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int n, h = 1, bst, bit[NMAX + 5], maxl[NMAX + 5], v[NMAX + 5], d[NMAX + 5], lst[NMAX + 5], res[NMAX + 5];
void update(int x, int ind) {
    for(; x <= n; x += x & (-x))
        if(maxl[ind] > maxl[bit[x]])
            bit[x] = ind;
}
int query(int x) {
    int mx = 0;
    for(; x; x &= x - 1)
        if(maxl[bit[x]] > maxl[mx])
            mx = bit[x];
    return mx;
}

int main()
{
    fin >> n;
    for(int i = 1; i <= n; i++) fin >> v[i], res[i] = lst[i] = v[i];
    sort(lst + 1, lst + n + 1);
    for(int i = 2; i <= n; i++)
        if(lst[i] != lst[h])
            lst[++h] = lst[i];
    for(int i = 1; i <= n; i++)
        v[i] = lower_bound(lst + 1, lst + h + 1, v[i]) - lst;
    for(int i = 1; i <= n; i++) {
        d[i] = query(v[i] - 1);
        maxl[i] = maxl[d[i]] + 1;
        update(v[i], i);
    }
    for(int i = 1; i <= n; i++)
        if(maxl[bst] < maxl[i])
            bst = i;
    fout << maxl[bst] << "\n";
    for(h = 0; bst; bst = d[bst])
        lst[++h] = res[bst];
    for(int i = h; i; i--)
        fout << lst[i] << " ";
    fout << "\n";
    return 0;
}