Pagini recente » Cod sursa (job #85736) | Cod sursa (job #74364) | Cod sursa (job #1787598) | Cod sursa (job #1794743) | Cod sursa (job #343714)
Cod sursa(job #343714)
#include <cstdio>
#include <algorithm>
using namespace std;
struct St { int a, b; } ;
int const maxn=5000;
St S[maxn];
int V[maxn];
int L[maxn];
void heap_push ( int i ) {
int t;
while ( 0 < i ) {
t=((i-1)>>1);
if (S[i].a<=S[t].a) { return; }
St e=S[i]; S[i]=S[t]; S[t]=e; i=t;
}
}
void heap_pop ( int n ) {
St e=S[n]; S[n]=S[0]; S[0]=e;
int i,t=0;
while ( n>(i=((1+t)<<1)-1) ) {
if ( (n>(1+i)) && (S[1+i].a>S[i].a) ) { ++i; }
if ( S[i].a<=S[t].a) { return; }
e=S[i]; S[i]=S[t]; S[t]=e; i=t;
}
}
void heap_sort ( int n ) {
int s ; for ( s=1; n>s; ++s ) { heap_push(s); }
for ( s=n-1; s>0; --s ) { heap_pop(s); }
}
int
main ( ) {
FILE * fs=fopen("secv.in","r"); int N,s,e,t=0,mn=-1,y; fscanf(fs,"%d",&N);
for ( s=0; N>s; ++s ) { fscanf(fs,"%d",&S[s].a); S[s].b=s; } fclose(fs);
heap_sort(N);
s=0; while (N>s) {
e=S[s].a;
while ( (N>s) && (S[s].a==e) ) { V[S[s].b]=t; ++s; }
++t;
} --t;
for ( s=0; N>s; ++s ) { L[s]=-1; }
for ( s=0; N>s; ++s ) { e=V[s];
if ( 0==e ) { L[e]=s; } else { L[e]=L[e-1]; }
if ( (t==e)&&(-1!=L[e]) ) { y=s-L[e]+1;
if ((-1==mn)||(y<mn)) { mn=y; }
}
}
fs=fopen("secv.out","w"); fprintf(fs,"%d\n",mn); fclose(fs);
return 0;
}