Cod sursa(job #2048846)

Utilizator EdyOnuEdy Onu EdyOnu Data 26 octombrie 2017 17:03:32
Problema Secv Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.29 kb
// secv.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <vector>
#include <queue>
#include <fstream>
#include <unordered_set>
#include <iostream>
#define NMAX 5005

using namespace std;

vector < int > v;
unordered_set < int > set;
int length[NMAX], choice[NMAX];

int N;

void readData() {
	ifstream f{ "secv.in" };

	f >> N;

	while (N--) {
		int x; f >> x;
		v.push_back(x); set.insert(x);
	}
	
	f.close();
}

template <typename T>class cmp {
public:
	bool operator()(const T a, const T b) const  {
		return a > b;
	}
};

priority_queue < int, vector < int >, cmp<int> > Q;


inline int answer() {

	for (int j = v.size() - 1; j >= 0; --j) {
		length[j] = 1, choice[j] = j;

		for (int i = j + 1; i < v.size(); ++i) {

			if (v[j] >= v[i] || length[j] > length[i] + 1)continue;

			if (length[j] == length[i] + 1) {
				choice[j] = min(choice[j], choice[i]); continue;
			}

			length[j] = length[i] + 1, choice[j] = choice[i];
		}

		if (length[j] == set.size())Q.push(choice[j] - j + 1);
	}

	return Q.empty() ? -1 : Q.top();

}

inline void writeAnswer(int a) {
	ofstream g{ "secv.out" };
	g << a << '\n';
	g.close();
}


int main(){
	readData();
	writeAnswer(answer());
    return 0;
}