Cod sursa(job #1116755)

Utilizator PatrikStepan Patrik Patrik Data 22 februarie 2014 19:51:06
Problema Subsir crescator maximal Scor 65
Compilator cpp Status done
Runda Arhiva educationala Marime 1.1 kb
    #include<cstdio>
    #include<vector>
    using namespace std;
    #define pb push_back
    #define MAX 100001
    int N , p[MAX] , v[MAX] , maxx , l[MAX] , k , poz;

    int caut(int x);

    int main()
    {
        freopen("scmax.in" , "r" , stdin );
        freopen("scmax.out" , "w" , stdout );
        scanf("%d" , &N );
        for(int i = 1 ; i <= N ; ++i )
            scanf("%d" , &v[i]);
        for(int i = 1 ; i<= N ; ++i )
        {
            poz = caut(v[i]);
            l[poz] = v[i];
            p[i] = poz;
        }
        printf("%d\n" , k);
        for(int i = N ; i ; i--)
            if(p[i] == maxx)
            l[maxx--] = v[i];
        for(int i = 1 ; i <= k ; ++i )
            printf("%d " , l[i]);
        return 0;
    }

    int caut(int x)
    {
        if(x > l[k])return ++k;
        int ls = 1 , ld = k , m;
        while(ls <= ld)
        {
            m = (ls+ld)/2;
            if(l[m] < x && l[m+1] >= x)return m+1;
            else
                if(l[m] < x)ls = m+1;
                else ld = m-1;
        }
        return ls;
    }