Cod sursa(job #2937650)

Utilizator MrPuzzleDespa Fabian Stefan MrPuzzle Data 10 noiembrie 2022 19:27:29
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.51 kb
#include<fstream>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<climits>
#include<iomanip>
#include<cstring>
#include<bitset>

#define MAX 100000

using namespace std;

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

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

int v[MAX+5],t[MAX+5],poz[MAX+5],best[MAX+5];
int pozLen = 0,n;
int maxi=-1,maxiPoz;

void afis(int x){

    //cout<<x<<" ("<<t[x]<<")"<<'\n';

    if(t[x] != 0){
        afis(t[x]);
    }

    g<<v[x]<<" ";
}

int main(){

    f>>n;
    for(int i=1;i<=n;i++){
        f>>v[i];
    }

    for(int i=1;i<=n;i++){

        int st=1,dr = pozLen,sol = pozLen+1;
        while(st<=dr){
            int mid = (st+dr)/2;
            if(v[poz[mid]]>=v[i]){
                sol = mid;
                dr = mid-1;
            }else{
                st = mid+1;
            }
        }

        if(sol>pozLen){
            pozLen = sol;
        }
        t[i] = poz[sol-1];
        poz[sol] = i;

        best[i] = best[t[i]] + 1;

        if(best[i]>maxi){
            maxi = best[i];
            maxiPoz = i;
        }

        /*cout<<i<<": "<<sol<<" "<<pozLen<<" "<<t[i]<<" "<<best[i]<<'\n';
        for(int j=1;j<=pozLen;j++){
            cout<<poz[j]<<" ";
        }
        cout<<'\n';
        for(int j=1;j<=i;j++){
            cout<<best[j]<<" ";
        }
        cout<<'\n'<<'\n';*/
    }

    g<<maxi<<'\n';
    afis(maxiPoz);

    f.close();
    g.close();
    return 0;
}