Cod sursa(job #2157927)

Utilizator armandpredaPreda Armand armandpreda Data 10 martie 2018 00:35:38
Problema Secv Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <fstream>
#include <algorithm>

using namespace std;

ifstream cin ("secv.in");
ofstream cout ("secv.out");

const int LIM = 5005;
int n, v[LIM], dp[LIM], tata[LIM];
int main() 
{
	int tot = 0;
	cin >> n;
	for(int i = 1; i <= n; ++i)
		cin >> v[i];
	for(int i = 1; i <= n; ++i)
	{
		bool dif = 1;
		int max_dp = 0, id = 0;
		for(int j = i-1; j >=1; --j)
		{
			if(v[i] == v[j])
				dif = 0;
			if(v[j] < v[i] and max_dp < dp[j])
			{
				max_dp = dp[j];
				id = j;
			}
		}
		if(dif)
			tot++;
		if(id == 0)
		{
			dp[i] = 1;
			tata[i] = 0;
		}
		else
		{
			dp[i] = dp[id]+1;
			tata[i] = id;
		}
	}
	int st = 0, dr = 50001;
	for(int i = 1; i <= n; ++i)
		if(dp[i] == tot)
		{
			int first = i;
			while(tata[first])
				first = tata[first];
			if(dr-st > i-first)
			{
				dr = i;
				st = first;
			}
		}
	cout << dr-st+1 << '\n';
	return 0;
}