Cod sursa(job #2376419)

Utilizator nicolaefilatNicolae Filat nicolaefilat Data 8 martie 2019 15:31:50
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.08 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

const int MAXN = 100000 + 5;

using namespace std;

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

int n,v[MAXN],last[MAXN],aux[MAXN];
int index = 0;

int cautbinar_up(int x,int maxindex){
    int pas = 1<<28, r = 0;
    while(pas){
        if(r + pas <= maxindex and aux[r + pas] < x)
            r += pas;
        pas /= 2;
    }
    return r + 1;
}

void afis(){
    for(int i = 1; i <= index; i++)
        cout<<aux[i]<<" ";
    cout<<endl;
}

int main()
{
    in>>n;
    for(int i = 1; i <= n; i++)
        in>>v[i];
    for(int i = 1; i <= n; i++){
        last[i] = cautbinar_up(v[i],index);
        index = max(index,last[i]);
        aux[last[i]] = v[i];
    }
    out<<index<<"\n";
    vector<int>ans;
    int curent = index;
    for(int i = n; i >= 1; i--){
        if(last[i] == curent){
            ans.push_back(v[i]);
            curent--;
        }
    }
    reverse(ans.begin(),ans.end());
    for(auto x : ans)
        out<<x<<" ";
    return 0;
}