Cod sursa(job #1677257)

Utilizator remus.ionitaIonita Remus remus.ionita Data 6 aprilie 2016 14:08:06
Problema Subsir crescator maximal Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.97 kb
#include <iostream>
#include <vector>
#include <fstream>

#define N_MAX 100000
std::fstream fin ("scmax.in", std::ios::in);
std::fstream fout ("scmax.out", std::ios::out);

int n;
std::vector <long long> v;
int d[N_MAX];

void dinamic () {
	int max = 0;
	d[0] = 1;
	for (int i = 1; i < n; i++) {
		max = 0;
		for (int j = 0; j < i; j++) {
			if (v[i] > v[j] && max < d[j]) {
				max = d[j];
			}
		}
		d[i] = max + 1;
	}
	fout << d[n-1] << '\n';
}
void solutie (int, int);
void get_sol () {
	int max = 0;
	int pmax = -1;
	for (int i = 0; i < n; i++) {
		if (d[i] > max) {
			max = d[i];
			pmax = i;
		}
	}
	solutie (pmax, max);
}
void solutie (int pmax, int max) {
	if (pmax >= 0) {
		if (d[pmax] == max) {
			solutie (pmax - 1, max - 1);
			fout << v[pmax] << " ";
		} else
			solutie (pmax - 1, max);
	}
}

int main (void) {
	fin >> n;
	long temp_l;
	for (int i = 0; i < n; i++) {
		fin >> temp_l;
		v.push_back (temp_l);
	}
	dinamic ();
	get_sol ();


	fin.close();
	fout.close();
	return 0;
}