Cod sursa(job #1242798)

Utilizator silidragosSilion Dragos silidragos Data 15 octombrie 2014 00:34:00
Problema Subsir crescator maximal Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.78 kb
#include<stdio.h>
#define MAX 100003

FILE *f = fopen("scmax.in","r");
FILE *g = fopen("scmax.out", "w");
int best[MAX], a[MAX], max, sol = 0, poz[MAX], p;
int n;
void read(){
	fscanf(f, "%d", &n);
	for (int i = 1; i <= n; i++) fscanf(f, "%d", &a[i]);
}

void dinamic(){
	int i, j;
	best[n] = 1;
	poz[n] = -1;
	max = 1; p = n;
	for (i = n - 1; i >= 1; --i){
		best[i] = 1;
		poz[i] = 1;
		for (j = i + 1; j <= n; ++j){
			if (a[i] < a[j] && best[i] < best[j] + 1){
				best[i] = best[j] + 1;
				poz[i] = j;
				if (best[i]>max)max = best[i], p = i;
			}

		}

	}
}
void constr(){
int i;
i = p;
while (i != -1){
			fprintf(g, "%d ", a[i]);
			i = poz[i];
}
}


	

int main(){
	read();
	dinamic();
	fprintf(g, "%d\n", max);
	constr();
	return 0;


}