Cod sursa(job #545319)

Utilizator Teodor94Teodor Plop Teodor94 Data 3 martie 2011 09:02:23
Problema Secv Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.22 kb
// se transforma vectorul cu valori de max 2 miliarde in vector cu valori de max 5000.
#include<cstdio>
#include<vector>
#include<algorithm>

using namespace std;

const int N=5005;

vector <pair < int,int > > a(N);
int n,x[N],pozi[N],upoz[N];

void citire()
{
	freopen("secv.in","r",stdin);
	freopen("secv.out","w",stdout);
	scanf("%d",&n);
	for (int i=1;i<=n;++i)
	{
		scanf("%d",&a[i].first);
		a[i].second=i;
	}
	sort(&a[1],&a[n+1]);
	x[a[1].second]=1;
	for (int i=2;i<=n;++i)
		if (a[i].first==a[i-1].first)
			x[a[i].second]=x[a[i-1].second];
		else
			x[a[i].second]=x[a[i-1].second]+1;
}

int aflumax(int a[])
{
	int max=a[1];
	for (int i=2;i<=n;++i)
		if (a[i]>max)
			max=a[i];
	return max;
}

void afis(int a[])
{
	for (int i=1;i<=n;++i)
		printf("%d ",a[i]);
	printf("\n");
}

void rez()
{
	int maxx=aflumax(x),min=N;
	for (int i=1;i<=n;++i)
	{
		if (x[i]==1)
		{
			upoz[1]=i;
			pozi[1]=i;
		}
		else
		{
			upoz[x[i]]=i;
			pozi[x[i]]=pozi[x[i]-1];
		}
		if (x[i]==maxx && pozi[maxx]!=0 && upoz[maxx]-pozi[maxx]+1<min)
			min=upoz[maxx]-pozi[maxx]+1;
	}
	if (min!=N)
		printf("%d\n",min);
	else
		printf("-1\n");
}

int main()
{
	citire();
	rez();
	return 0;
}