Cod sursa(job #2110661)

Utilizator sebistetuCucolas Sebastian sebistetu Data 21 ianuarie 2018 01:20:41
Problema Subsir crescator maximal Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include<fstream>
using namespace std;
ifstream f("scmax.in");
ofstream g("scmax.out");
const int inf = 2147483647;
const int Nmax = 100005;
int v[Nmax], poz[Nmax], l[Nmax], n, lung_max;
void citire(){
    f >> n;
    for(int i = 1; i <= n; i++)
        f >> v[i];
}
void cautare(int x){
    lung_max++;
    poz[lung_max] = inf;
    int p, q, m;
    p = 1;
    q = lung_max;
    while(p != q){
        m = (p + q) / 2;
        if(x > poz[m])
            p = m + 1;
        else
            q = m;
    }
    poz[p] = x;
    if(poz[lung_max] == inf)
        lung_max--;
    else
        l[x] = lung_max;
}
void rezolvare(){
    lung_max++;
    poz[1] = v[1];
    l[1] = 1;
    for(int i = 2; i <= n; i++)
        cautare(v[i]);
}
int main(){
    citire();
    rezolvare();
    g << lung_max << '\n';
    for(int i = 1; i <= n; i++)
        if(l[i])
        g << v[i] << ' ';
    return 0;
}