Pagini recente » Cod sursa (job #2308549) | Cod sursa (job #2725932) | Cod sursa (job #1406474) | Cod sursa (job #2058876) | Cod sursa (job #2015289)
#include <cstdio>
#include <algorithm>
using namespace std;
struct g
{
int x,o,r;
}v[5001];
bool comp(g a,g b)
{
if(a.x==b.x) return a.o<b.o;
return a.x<b.x;
}
int main()
{
int n,i,j,k,ct,pas,rez;
freopen("secv.in","r",stdin);
freopen("secv.out","w",stdout);
scanf("%d",&n);
for(i=1; i<=n; i++){
scanf("%d",&v[i].x);
v[i].o=i;
}
sort(v+1,v+n+1,comp);
if(v[1].x==v[n].x)
printf("1\n");
else
{
for(i=n; i>0;)
{
j=i-1;
while(j>0 && v[i].x==v[j].x)
j--;
if(i==n)
for(k=j+1; k<=n; k++)
v[i].r=n+1;
else
for(ct=j+1; ct<=i; ct++){
pas=1<<16;
k=i;
while(pas)
{
if(k+pas<=n && v[k+pas].x==v[i].x+1 && v[k+pas].o<v[ct].o)
k+=pas;
pas/=2;
}
k++;
if(v[k+pas].x==v[i].x+1 && v[k].o>v[ct].o)
v[ct].r=k;
else
v[ct].r=n+1;
}
i=j;
}
if(v[1].r==n+1)
printf("-1\n");
else
{
rez=n;
for(i=1; v[i].x==v[1].x; i++)
{
j=i;
while(v[j].r!=n+1)
j=v[j].r;
if(v[j].x==v[n].x && rez>v[j].o-v[i].o+1)
rez=v[j].o-v[i].o+1;
}
printf("%d\n",rez);
}
}
return 0;
}