Cod sursa(job #3213657)

Utilizator TanasucaGeorgeAlexandruTanasucaGeorgeAlexandru TanasucaGeorgeAlexandru Data 13 martie 2024 12:34:14
Problema Subsir crescator maximal Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.15 kb
#include <bits/stdc++.h>
using namespace std;

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

int a[100005];
int s[100005];
int P[100005];
int sol[100005];
int n,st,dr,m,top,p;

int main()
{
    int i,x;
    fin >> n;
    for(i=1;i<=n;i++){
        fin >> a[i];
    }
    s[1]=a[1];
    P[1]=1;
    top=1;
    for(i=2;i<=n;i++){
        x=a[i];
        if(x<s[1]){
            s[1]=x;
            P[i]=1;
        }
        else if(x>s[top]){
            s[++top]=x;
            P[i]=top;
        }
        else{
            st=1;
            dr=top;
            p=top;
            while(st<=dr){
                m=(st+dr)/2;
                if(x<s[m]){
                    p=m;
                    dr=m-1;
                }
                else st=m+1;
            }
            s[p]=x;
            P[i]=p;
        }
    }
    fout << top << "\n";
    x=1e9;
    p=top;
    for(i=n;i>=1 && top>0;i--){
        ///cout << P[i] << " ";
        if(P[i]==top && a[i]<x){
            sol[top]=a[i];
            x=a[i];
            top--;
        }
    }
    for(i=1;i<=p;i++) fout << sol[i] << " ";
    return 0;
}