Cod sursa(job #1190361)

Utilizator bogdanmarin69Bogdan Marin bogdanmarin69 Data 25 mai 2014 10:21:56
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.92 kb
#include <cstdio>
using namespace std;
#define MAX 100001
int v[MAX], len[MAX], pred[MAX], n, lg;
int main()
{
    int i, st, dr, mj, lgnou, k;
    freopen("scmax.in", "r", stdin);
    freopen("scmax.out", "w", stdout);
    scanf("%d", &n);

    for(i=1; i<=n; i++) scanf("%d", v+i);
    len[1] = 1; lg = 1;
    for(i=2; i<=n; i++){
        st = 1; dr = lg;
        while(st<=dr){
            mj = (st+dr)>>1;
            if(v[len[mj]]<v[i])
                st=mj+1;
            else
                dr=mj-1;
        }
        lgnou = st-1;
        pred[i] = len[lgnou];
        if(lgnou+1>lg){
            lg = lgnou+1;
            len[lg] = i;
        }
        if(v[i]<v[len[lgnou+1]]) len[lgnou+1]=i;

    }
    printf("%d\n", lg);
    k = len[lg];
    for(i=lg; i>=1; i--){
        len[i]=v[k];
        k=pred[k];
    }
    for(i=1; i<=lg; i++)
        printf("%d ", len[i]);
    return 0;
}