Cod sursa(job #860224)

Utilizator fhandreiAndrei Hareza fhandrei Data 20 ianuarie 2013 12:33:05
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
// Include
#include <fstream>
#include <algorithm>
using namespace std;

// Functii
void route(int pos);

// Constante
const int sz = (int)1e5+1;

// Variabile
ifstream in("scmax.in");
ofstream out("scmax.out");

int num;
int elements[sz], best[sz], father[sz];

// Main
int main()
{
	in >> num;
	for(int i=1 ; i<=num ; ++i)
		in >> elements[i];
	
	best[1] = 1;
	
	for(int i=2 ; i<=num ; ++i)
	{
		int maxFound = 0, maxFoundPos = 0;
		for(int j=1 ; j<i ; ++j)
		{
			if(elements[j] < elements[i])
			{
				if(best[j] > maxFound)
				{
					maxFound = best[j];
					maxFoundPos = j;
				}
			}
			best[i] = maxFound + 1;
			father[i] = maxFoundPos;
		}
	}
	
	int pos = max_element(best+1, best+num+1) - best;
	
	out << best[pos] << '\n';
	route(pos);
	out << '\n';
	
	in.close();
	out.close();
	return 0;
}

void route(int pos)
{
	if(father[pos])
		route(father[pos]);
	out << elements[pos] << ' ';
}