Cod sursa(job #2443004)

Utilizator urweakurweak urweak Data 26 iulie 2019 10:34:47
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.75 kb
#include <fstream>
#define MAX 100003

using namespace std;
ifstream in("scmax.in");
ofstream out("scmax.out");

int N, v[MAX], best[MAX], poz[MAX], p, maxx;

void read()
{
	in >> N;
	for(int i = 1; i<=N; i++)
		in >> v[i];
}

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

void constr()
{
	int i = p;
	while(i != -1)
	{
		out << v[i] <<' ';
		i = poz[i];
	}
}

int main()
{
	read();
	dinamic();
	out << maxx <<"\n";
	constr();
}