Cod sursa(job #2158718)

Utilizator armandpredaPreda Armand armandpreda Data 10 martie 2018 15:40:13
Problema Secv Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <fstream>
#include <algorithm>

using namespace std;

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

const int LIM = 5005;
int n, v[LIM], tot, dif[LIM], minim = 5001;
int main() 
{
	cin >> n;
	for(int i = 1; i <= n; ++i)
	{
		cin >> v[i];
		dif[i] = v[i];
	}
	sort(dif + 1, dif + n+1);
	dif[0] = -1;
	for(int i = 1; i <= n; ++i)
		if(dif[i] != dif[i-1])
			dif[ ++tot ] = dif[i];
	if(tot == 1)
	{
		cout << "1\n";
		return 0;
	}
	for(int i = 1; i <= n-tot+1; ++i)
		if(v[i] == dif[1])
		{
			int id = 2;
			for(int j = i+1; j <= n; ++j)
			{
				if(v[j] == dif[id])
					id++;
				if(id == tot+1)
				{
					if(minim > j-i+1)
						minim = j-i+1;
					break;
				}
			}
		}
	if(minim <= 5000)
		cout << minim << '\n';
	else
		cout << "-1\n";
	return 0;
}