Cod sursa(job #343717)

Utilizator mgntMarius B mgnt Data 26 august 2009 22:48:15
Problema Secv Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <cstdio>
#include <algorithm>
using namespace std;

struct St { int a, b; } ;
bool operator < ( St const & x, St const & y ) { return x.a<y.a; }

int const maxn=5000;
 St S[maxn];
int V[maxn];
int L[maxn];

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);
  make_heap(&S[0], &S[N]);
  sort_heap(&S[0], &S[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; // t<N
  for ( s=0; t>=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;
}