Cod sursa(job #2704991)

Utilizator tomaionutIDorando tomaionut Data 11 februarie 2021 18:51:58
Problema Secv Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.72 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("secv.in");
ofstream fout("secv.out");
int n,a[5005],b[5005];
int main()
{	
	int i,k=1,cnt=0,mi=INT_MAX,j;
	fin >> n;
	for (i = 1; i <= n; i++)
	{
		fin >> a[i];
		b[i] = a[i];
	}
	sort(b + 1, b + n + 1);
	for (i = 2; i <= n; i++)
		if (b[i] != b[k])
			b[++k] = b[i];
	if (k == 1)
	{
		fout << "1\n";
		return 0;
	}
	for (i=1; i<=n; i++)
		if (a[i] == b[1])
		{
			cnt = 2;
			for (j = i + 1; j <= n; j++)
			{
				if (a[j] == b[cnt])
					cnt++;
				if (j - i + 1 >= mi)
					break;
				if (cnt == k+1)
				{
					mi = j - i + 1;
					break;
				}
			}
		}
	if (mi==INT_MAX)
		fout << "-1\n";
	else fout << mi;


	return 0;
}