Mai intai trebuie sa te autentifici.
Cod sursa(job #1482856)
Utilizator | Data | 8 septembrie 2015 10:27:12 | |
---|---|---|---|
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,
i, i2, p, j;
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;
};
inline int max(int a, int b) {
if(a>b) return a;
else return b;
}
void solve() {
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);
}
i2 = p;
for(i = N; i >= 1; --i)
if(L[ i ] == p) {SOL[ p ] = V[ i ]; p--;}
fprintf(fout, "%d\n", i2);
for(i = 1; i <= i2; i++) fprintf(fout, "%d ", SOL[ i ]);
fclose( fout );
};
int main() {
read();
solve();
return(0);
};