Cod sursa(job #1701653)

Utilizator Pley01Nitu Madalin Pley01 Data 13 mai 2016 19:31:37
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.75 kb
#include<stdio.h>
FILE*f = fopen("scmax.in", "r");
FILE*g = fopen("scmax.out", "w");
int best[100003], a[100003], max, sol = 0, poz[100003], 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;
}