Cod sursa(job #3314371)

Utilizator postolacheepostolache postolachee Data 9 octombrie 2025 21:00:09
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.58 kb
/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/
#include <bits/stdc++.h>
#define int long long
#pragma GCC optimize ("O3,Ofast,unroll-loops")
#define pb push_back
#define f first
#define s second
using namespace std;

bool cmp(const pair<int,int>& p1, const pair<int,int>& p2){
    if(p1.f == p2.f) return p1.s > p2.s;
    return p1.f < p2.f;
}

signed main(){
    ifstream cin("scmax.in");
    ofstream cout("scmax.out");
    
    int n;cin >> n;
    vector<int> a(n + 1);
    for(int i=1;i <= n;i++)cin >> a[i];
    
    vector<int> dp, parent(n + 1, -1), pos;
    
    for(int i=1;i <= n;i++){
        int val=a[i];
        int k=lower_bound(dp.begin(), dp.end(), val) - dp.begin();
        
        if(k == dp.size()){
            dp.pb(val);
            if(k > 0)parent[i]=pos[k - 1];
            pos.pb(i);
        }else{
            dp[k]=val;
            if(k > 0)parent[i]=pos[k - 1];
            pos[k]=i;
        }
    }
    
    vector<int> ans;
    if(!pos.empty()){
        int x=pos.back();
        while(x != -1){
            ans.pb(a[x]);
            x=parent[x];
        }
    }
    reverse(ans.begin(), ans.end());
    
    cout << ans.size() << '\n';
    for(auto x : ans)cout << x << " ";
    cout << '\n';
    return 0;
}