Cod sursa(job #897881)

Utilizator whoasdas dasdas who Data 27 februarie 2013 22:48:38
Problema Subsir crescator maximal Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.67 kb
#include <fstream>
#include <list>

using namespace std;

int main()
{
	ifstream in("scmax.in");
	ofstream out("scmax.out");
	int n, lmax, imax;
	in>>n;
	int v[n], m[n], p[n];
	lmax = 1, imax = 0;	
	
	for (int i = 0; i < n; i++) {
		in>>v[i];
		m[i] = 1;
		p[i] = -1;
		for (int j = 0; j < i; j++) {
			if (v[j] < v[i])
				if (m[j] + 1 > m[i]) {
					m[i] = m[j] + 1
					p[i] = j;
				}
		}
		if (m[i] > lmax) {
			lmax = m[i];
			imax = i;
		}
	}
	
	out<<lmax<<endl;
	
	list<int> lst;
	while (imax != -1) {
		lst.push_front(v[imax]);
		imax = p[imax];
	}
	while (!lst.empty()) {
		out<<lst.front()<<" ";
		lst.pop_front();
	}
	return 0;
}