Cod sursa(job #2489939)

Utilizator Seb0730Matei Sebastian Seb0730 Data 9 noiembrie 2019 13:44:07
Problema Subsir crescator maximal Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.19 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("scmax.in");
ofstream out("scmax.out");

int top=1, dp[100005], rezultat[100005], k;


int cautbin(int low, int high, int x){
    int mij;
    while(low<=high){

        mij=(low+high)/2;

        if(x==dp[mij]){

            return mij;
        }
        if(dp[mij]>x && dp[mij-1]<x){

            dp[mij]=x;
            return mij;

        }else if(dp[mij]==0 && dp[mij-1]<x){


            dp[mij]=x;
            top++;
            return mij;

        }else{
            if(dp[mij-1]>x){

                high=mij-1;

            }else{

                low=mij+1;
            }
        }
    }



}

int n, v[100005], x, a[100005];


int main()
{

    x=1;
    in>>n;

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

        a[i]=cautbin(0, top+1, v[i])+1;

    }
    top--;
    out<<top<<'\n';
    top++;
    for(int i=n;i>=1;i--){
        if(a[i]==top){
            k++;
            rezultat[k]=v[i];
            if(top==1){
                break;
            }
            top--;
        }
    }
    for(int i=k;i>=1;i--){
        out<<rezultat[i]<<" ";
    }

}