Cod sursa(job #1482849)

Utilizator thinkphpAdrian Statescu thinkphp Data 8 septembrie 2015 10:21:34
Problema Subsir crescator maximal Scor 0
Compilator c Status done
Runda Arhiva educationala Marime 1.23 kb
#include <stdio.h>
#define FIN "scmax.in"
#define FOUT "scmax.out"
#define DIMN 100010
 
FILE *fin,
     *fout;
 
int V[ DIMN ], L[ DIMN ], SOL[ DIMN ],
    N;
 
void read() {
 
     int i;
 
     fin = fopen(FIN, "r");
 
     fscanf(fin,"%d",&N);
 
     for(i = 1; i <= N; ++i ){
 
         fscanf(fin,"%d",&V[ i ]);
     }

     fclose( fin );
};

int bsearch(int lo, int hi, int x) {
    int m;
    while(lo<=hi) {
          m = (lo+hi)>>1;
          if(SOL[m] < x) lo = m + 1;
                else     hi = m - 1; 
    }
    return lo; 
};

int max(int a, int b) {
    if(a>b) return a;
       else return b;
}
 
void solve() {

     int i, i2, p, j;

     fout = fopen(FOUT, "w");

     int L[ DIMN ];     

     SOL[ 1 ] = V[ 1 ];

     p = 1; L[ 1 ] = 1;

     for(i = 2; i <= N; i++) {

         j = bsearch(1, p, V[ i ]); L[ i ] = j;

         SOL[ j ] = V[ i ]; p = max(p, j);
     }

     fprintf(fout, "%d\n", p);

     i2 = p; 

     for(i = N; i >= 1; --i)

         if(L[ i ] == p) {SOL[ p ] = V[ i ]; p--;}          

     for(i = 1; i <= i2; i++) fprintf(fout, "%d ", SOL[ i ]);

     fclose( fout );
 
};
 
int main() {
 
    read();
    solve();
 
 return(0);
};