Pagini recente » Cod sursa (job #2324423) | Cod sursa (job #815353) | Borderou de evaluare (job #2008059) | Cod sursa (job #1654903) | Cod sursa (job #2627001)
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE *fin = fopen("scmax.in", "r");
FILE *fout = fopen("scmax.out", "w");
int n, a[100000], max, start;
fscanf(fin, "%d", &n);
for(int i = 0; i < n; i++)
fscanf(fin, "%d", &a[i]);
int *D = malloc(n * sizeof(int));
for(int i = 0; i < n; i++){
max = 0;
for(int j = 0; j < i; j++)
if(a[j] < a[i] && D[j] > max) max = D[j];
D[i] = max + 1;
}
max = 0;
for(int i = 0; i < n; i++)
if(D[i] > max){
max = D[i];
start = i;
}
int size = max;
int *ans = malloc(max * sizeof(int));
ans[--max] = a[start];
while(max){
for(int i = 0; i < start; i++)
if(D[i] == D[start] - 1 && a[i] < a[start]){
ans[--max] = a[i];
start = i;
break;
}
}
fprintf(fout, "%d\n", size);
for(int i = 0; i < size; i++)
fprintf(fout, "%d ", ans[i]);
fprintf(fout, "\n");
free(ans);
free(D);
fclose(fin);
fclose(fout);
}