Cod sursa(job #3369310)

Utilizator Vicentiu123Savu Vicentiu Dorian Vicentiu123 Data 19 octombrie 2026 16:40:00
Problema Subsir crescator maximal Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.3 kb
#include <bits/stdc++.h>
using namespace std;
int v[100001], aib[100001], l[100001], cv[100001];
int n;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
void normalizare() {
    int i, g=0;
    vector <pair<int,int> > aux(n);
    for (i=0;i<n;i++) {
        aux[i].first=cv[i];
        aux[i].second=i;
    }
    sort(aux.begin(),aux.end());
    v[aux[0].second] = ++g;
    for (i=1;i<n;i++) {
        if (aux[i].first>aux[i-1].first) {
            g++;
        }
        v[aux[i].second]=g;
    }

}
void update(int i, int x) {
    int l=i;
    while (l<=n) {
        aib[l]=max(aib[l],x);
        l+=l&(-l);
    }
}
int query(int i) {
    int mx=-1;
    while (i>0) {
        mx=max(mx,aib[i]);
        i-=i&(-i);
    }
    return mx;
}
void refsir(int lung, int lim, int poz) {
    if (lung==0) {
        return;
    }
    if (cv[poz]<lim&&l[poz]==lung) {
        refsir(lung-1,cv[poz],poz-1);
        fout<<cv[poz]<<' ';
    }
    else {
        refsir(lung,lim,poz-1);
    }
}
int main() {
    int i, poz=0;
    fin>>n;
    for (i=0; i<n; i++) {
        fin>>v[i];
        cv[i]=v[i];
    }
    normalizare();
    for (i=0; i<n; i++) {
        l[i]=query(v[i])+1;
        if (l[i]>l[poz]) {
            poz=i;
        }
        update(v[i],l[i]);
    }
    fout<<l[poz]<<'\n';
    refsir(l[poz],1e9,poz);
    return 0;
}