Cod sursa(job #2881151)

Utilizator QwertyDvorakQwerty Dvorak QwertyDvorak Data 30 martie 2022 12:03:09
Problema Subsir crescator maximal Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.93 kb
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define dbg(x) cout << #x <<": " << x << "\n";
#define sz(x) ((int)x.size())

using ll = long long;

const string fn = "scmax";
ifstream fin(fn + ".in");
ofstream fout(fn + ".out");

int n;
int a[100005];
int lis[100005], poz[100005];
int main(){

    fin >> n;
    for (int i = 1; i <= n; ++i)
        fin >> a[i];
    int p = 0;
    for (int i = 1; i <= n; ++i){
        if(a[i] > lis[p]){
            lis[++p] = a[i];
            poz[i] = p;
        }
        else{
            p = lower_bound(lis + 1, lis + p + 1, a[i]) - lis;
            lis[p] = a[i];
            poz[i] = p;
        }
    }

    fout << p << "\n";
    vector<int> ans;
    for (int i = n; i >= 1; --i){
        if(poz[i] == p)
            ans.pb(i), --p;
    }
    reverse(ans.begin(), ans.end());
    for(auto i : ans)
        fout << a[i] << " ";

    return 0;
}