Cod sursa(job #3286423)

Utilizator PsyDuck1914Feraru Rares-Serban PsyDuck1914 Data 14 martie 2025 10:28:41
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.22 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f ("scmax.in");
ofstream g ("scmax.out");

const int NMAX = 1e5;
const int VALMAX = 2e9 + 1e4;

int n;
int best[NMAX + 1], v[NMAX + 1], last[NMAX + 1];

int cb(int val){
    
    int rez = 0;
    int st = 1;
    int dr = n;
    
    while(st <= dr){
        
        int mij = (st + dr) / 2;
        
        if(best[mij] != VALMAX and v[best[mij]] < val)
            rez = mij, st = mij + 1;
            
        else dr = mij - 1;
        
    }
    
    return rez;
    
}

int main()
{
    
    f >> n;
    
    for(int i=1; i<=n; i++)
        f >> v[i];
        
    for(int i=1; i<=n; i++)
        best[i] = VALMAX;
        
    for(int i=1; i<=n; i++){
        
        int poz = cb(v[i]);
        best[poz + 1] = i;
        
        last[i] = best[poz];
        
    }
    
    int cnt = 0;
    int ult = 0;
    
    for(int i=1; i<=n; i++)
        if(best[i] != VALMAX)
            cnt ++, ult = best[i];
            
    vector<int> rez;
    
    while(ult){
        
        rez.push_back(v[ult]);
        ult = last[ult];
        
    }
    
    g << rez.size() << "\n";
    
    reverse(rez.begin(), rez.end());
    for(int val : rez)
        g << val << ' ';
    

    return 0;
}