Cod sursa(job #744752)

Utilizator andrey13Letcanu Andrei andrey13 Data 9 mai 2012 16:25:52
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include<cstdio>
#define inf 2000000001

using namespace std;

int L,a[100010],q[100010],p[100010],i,n,Max,k;
int bin(int x,int st,int dr){
    int m,poz;
     poz=0;
  while (st<=dr && poz==0){
    m=(st+dr)/2;
    if (q[m]==x) poz=m;
    else if (q[m]>x) dr = m-1;
         else st = m+1;
  }
    if (poz==0) poz=st;
    if  (st>L) L++;
    q[poz]=x;
    return poz;
}

void scrie ( int i, int x, int Max){

  if(i>0){
    if (a[i]<x && p[i]==Max) {
       scrie (i-1, a[i], Max-1);
       printf("%d ", a[i]);
    }
    else scrie (i-1, x, Max);
    }
}


int main(){
   freopen("scmax.in","r",stdin);
   freopen("scmax.out","w",stdout);
   scanf("%d",&n);
   for(i=1;i<=n;i++)scanf("%d",&a[i]);
   for(i=1;i<=n;i++){
      p[i]=bin(a[i],1,L);
      if(p[i]>Max){
        Max=p[i];
        k=i;}}
    printf("%d\n",L);
scrie(k,inf,Max);
return 0;
   }