Cod sursa(job #2938004)

Utilizator samyro14Samy Dragos samyro14 Data 11 noiembrie 2022 15:28:34
Problema Subsir crescator maximal Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
const int maxn = 1e5;
int n;
int a[maxn + 2];
int dp[maxn + 2];
int pos[maxn + 2];
int b[maxn + 2];
int k;
void read(){
    fin >> n;
    for(int i = 1; i <= n; ++i) fin >> a[i];
}
void solve(){
    dp[++k] = a[1];
    pos[1] = 1;
     for(int i = 2; i <= n; ++i){
        if(a[i] > dp[k]) dp[++k] = a[i], pos[i] = k;
        else{
            int st = 1, dr = k, p;
            while(st <= dr){
                int mid = (st + dr) / 2;
                if(dp[mid] > a[i]) p = mid, dr = mid - 1;
                else st = mid + 1;
            }
            dp[p] = a[i];
            pos[i] = p;
        }
    }
    fout << k << '\n';
    int j = n;
    for(int i = k; i >= 1; --i){
        while(pos[j] != i)
            j--;
        b[i] = j;
    }
    for(int i = 1; i <= k; ++i)
        fout << a[b[i]] << " ";
}
int main(){
    read();
    solve();
    return 0;
}
/*

*/