Cod sursa(job #2538904)

Utilizator victorzarzuZarzu Victor victorzarzu Data 5 februarie 2020 12:28:58
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.24 kb
#include <bits/stdc++.h>
#define zeros(x) ((x ^ (x - 1)) & x)
#define NMax 100005
using namespace std;
ifstream f("scmax.in");
ofstream g("scmax.out");
int n,Res[NMax],v[NMax],lst[NMax],D[NMax],AIB[NMax],bst,up[NMax];
void Read()
{
    f>>n;
    for(int i = 1;i <= n;++i)
    {
        f>>v[i];
        Res[i] = lst[i] = v[i];
    }
}

void update(int x,int ind)
{
    for(;x <= n;x += zeros(x))
        if(D[ind] > D[AIB[x]])
            AIB[x] = ind;
}

int query(int x)
{
    int m = 0;
    for(;x;x -= zeros(x))
        if(D[AIB[x]] > D[m])
            m = AIB[x];
    return m;
}

void Solve()
{
    int h = 1,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;
    g<<D[bst]<<'\n';
    for(h = 0,i = bst; i ;i = up[i])
        lst[++h] = Res[i];

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

int main()
{
    Read();
    Solve();
    return 0;
}