Cod sursa(job #3376681)

Utilizator alex.iovita.23@gmail.comIovita Alexandru [email protected] Data 18 noiembrie 2026 22:19:22
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.92 kb
#include<bits/stdc++.h>

using namespace std;

ifstream fin("scmax.in");
ofstream fout("scmax.out");

int n , v[100001] , dp[100001] , poz[100001] , tata[100001];
int lmax = 0;

int main(){
    fin >> n;
    for(int i = 1 ; i <= n ; i++) fin >> v[i];
    for(int i = 1 ; i <= n ; i++){
        int st = 1 , dr = lmax , sol = 0;
        while(st <= dr){
            int mid = (st + dr) / 2;
            if(dp[mid] < v[i]){
                sol = mid;
                st = mid + 1;
            }
            else dr = mid - 1;
        }
        int lnou = sol + 1;
        dp[lnou] = v[i];
        poz[lnou] = i;
        tata[i] = poz[sol];
        lmax = max(lmax , lnou);
    }
    fout << lmax << '\n';
    vector<int> sol;
    int cur = poz[lmax];
    while(cur){
        sol.push_back(v[cur]);
        cur = tata[cur];
    }
    reverse(sol.begin() , sol.end());
    for(auto i : sol) fout << i << " ";
}