Mai intai trebuie sa te autentifici.

Cod sursa(job #528083)

Utilizator Catah15Catalin Haidau Catah15 Data 1 februarie 2011 21:41:45
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.81 kb
// Subsir crescator maximal - O(n^2) - 70 pct

#include <fstream>
#include <vector>
#define MAXN 100010
using namespace std;

int n, best[MAXN], ant[MAXN], maxx, poz;
long long x[MAXN];

vector <int> sol;

int main()
{
	ifstream f("scmax.in");
	ofstream g("scmax.out");
	
	f >> n;
	
	for(int i = 1; i <= n; ++i)
	{
		f >> x[i];
		
		int max = 0;
		
		best[i] = 0;
		ant[i] = 0;
		
		for(int j = 1; j < i; ++j)
			if(x[j] < x[i] && best[j] > max)
			{
				max = best[j];
				ant[i] = j;
			}
			
		best[i] = ++max;
		
		if(best[i] > maxx)
		{	
			maxx =  best[i];
			poz = i;
		}
		
	}
	
	g << maxx << '\n';
	
	for(int i = poz; i ; i = ant[i])
		sol.push_back(x[i]);
	
	for(int i = sol.size() - 1; i >= 0; --i)
		g << sol[i] << " ";
	
	f.close();
	g.close();
	
	return 0;
}