Cod sursa(job #1768415)

Utilizator stefan_creastaStefan Creasta stefan_creasta Data 30 septembrie 2016 20:46:21
Problema Subsir crescator maximal Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1.05 kb
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 100005;
int v[N];
int poz[N];
int afis[N];
vector <int> vec;

int main()
{
    int i, n, x, k = 0, nr = 0;
    freopen("scmax.in","r",stdin);
    freopen("scmax.out","w",stdout);
    scanf("%d",&n);
    for(i = 1;i <= n; ++i){
        scanf("%d", &x);
        v[i] = x;
    }
    vec.push_back(v[1]);
    poz[1] = 1;
    for(i = 2;i <= n; ++i){
        vector <int>::iterator pozitie;
        pozitie = lower_bound(vec.begin() , vec.end() , v[i]);
        if(pozitie == vec.end()){
            vec.push_back(v[i]);
            poz[i] = vec.size() - 1;
        }
        else{
            poz[i] = pozitie - vec.begin();
            vec[poz[i]] = v[i];
        }
    }
    nr = vec.size() - 1;
    for(i = n;i > 0; --i){
        if(poz[i] == nr){
            afis[ ++k ] = v[i];
            --nr;
        }
    }
    printf("%d\n",k);
    for(i = k;i > 0; --i){
        printf("%d ",afis[i]);
    }
    printf("\n");
    return 0;
}