Cod sursa(job #1274111)

Utilizator thewildnathNathan Wildenberg thewildnath Data 23 noiembrie 2014 12:51:37
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.3 kb
#include<stdio.h>

const int NMAX = 100000;
const int INF = 2000000000;

int v[NMAX+1], minim[NMAX+1], minpoz[NMAX+1], t[NMAX+1];
int nrs, sol[NMAX+1];

inline int cautb(int x)
{
       int st=0, dr=NMAX, med;
       
       while(st<=dr)
       {
                    med=(st+dr)>>1;
                    
                    if(minim[med]>=x)
                                     dr=med-1;
                    else
                                     st=med+1;
       }
       
       return st;
}

void afis(int poz)
{
     if(t[poz]!=0)
                  afis(t[poz]);
     
     printf("%d ", v[poz]);
}

int main()
{
    freopen("scmax.in", "r", stdin);
    freopen("scmax.out", "w", stdout);
    
    int n, lmax=0, pmax, poz=0;
    
    scanf("%d", &n);
    
    for(int i=1; i<NMAX; ++i)
            minim[i]=INF;

    for(int i=1; i<=n; ++i)
    {
            scanf("%d", &v[i]);
            
            poz=cautb(v[i]);
    
            t[i]=minpoz[poz-1];
            minim[poz]=v[i];
            minpoz[poz]=i;
            
            if(poz>lmax)
            {
                        lmax=poz;
                        pmax=i;
            }
    }
    
    printf("%d\n", lmax);
    
    afis(pmax);
    
    printf("\n");
    
    return 0;
}