Cod sursa(job #1605517)

Utilizator qwertyuiTudor-Stefan Berbinschi qwertyui Data 19 februarie 2016 09:00:54
Problema Subsir crescator maximal Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.87 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <utility>

using namespace std;

ifstream fin ("scmax.in");
ofstream fout ("scmax.out");

#define MAXN 100050

vector <int> x, best, previous;
int N;

int main()
{
    fin >> N;
    x.resize(N+1);
    best.resize(N+1);
    previous.resize(N+1);

    best[N] = 1;
    previous[N] = -1;
    int _max = 0;
    int position = N;

	for (int i = N; i >= 1; --i)
	{
		best[i] = 1;
		previous[i] = -1;
		for (int j = i+1; j <= N; ++j)
			if (x[i] < x[j] && best[i] < best[j] + 1)
			{
				best[i] = best[j] + 1;
                previous[i] = j;
                if (best[i] > _max)
				{
					_max = best[i];
					position = i;
				}
			}
	}
	fout <<_max <<'\n';
	int i = position;
	while (i != -1)
	{
		fout <<x[i] <<' ';
		i = previous[i];
	}

	fout <<'\n';

    return 0;
}