Cod sursa(job #2415769)

Utilizator andreisontea01Andrei Sontea andreisontea01 Data 26 aprilie 2019 15:02:07
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.02 kb
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 100005;

int v[MAXN], last[MAXN];
vector<int> dp, ans;

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 pas = 1 << 30, rez = -1;
        while(pas){
            if(rez + pas < int(dp.size()) && v[dp[pas + rez]] < v[i])
                rez += pas;
            pas /= 2;
        }
        rez++;
        last[i] = dp[rez - 1];
        dp[rez] = i;
    }
    int current = dp.back();
    while(current){
        ans.push_back(v[current]);
        current = last[current];
    }
    fout << ans.size() << "\n";
    reverse(ans.begin(), ans.end());
    for(int i = 0; i < int(ans.size()); ++i)
        fout << ans[i] << " ";
    return 0;
}