Cod sursa(job #2385211)

Utilizator andreisontea01Andrei Sontea andreisontea01 Data 21 martie 2019 18:37:23
Problema Subsir crescator maximal Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 100005;

int v[MAXN], last[MAXN];

vector<int> dp, ans;
int len;

int main()
{
    ifstream fin("scmax.in");
    ofstream fout("scmax.out");
    int n;
    fin >> n;
    for(int i = 1; i <= n; ++i)
        fin >> v[i];
    dp.push_back(0);
    for(int i = 1; i <= n; ++i){
        if(v[i] > v[dp.back()]){
            last[i] = dp.back();
            dp.push_back(i);
            continue;
        }
        int rez = 0;
        for(int exp = 30; exp >= 0; --exp){
            int pos = rez + (1 << exp);
            if(pos < int(dp.size()) && v[dp[pos]] < v[i])
                rez += (1 << exp);
        }
        last[i] = dp[rez - 1];
        dp[rez] = i;
    }
    int curent = dp.back();
    while(curent){
        ans.push_back(v[curent]);
        curent = last[curent];
    }
    fout << ans.size() << "\n";
    reverse(ans.begin(), ans.end());
    for(auto x : ans)
        fout << x << " ";
    return 0;
}