Cod sursa(job #228070)

Utilizator rethosPaicu Alexandru rethos Data 6 decembrie 2008 13:02:48
Problema Secv Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1 kb
#include <stdio.h>
#include <algorithm>
#define NM 5001
#define INF 5001
using namespace std;
int n,m;
int k;
int c[NM];
int x[NM];
int rez=INF;
int main()
{freopen("secv.in","r",stdin);
 freopen("secv.out","w",stdout);
 scanf("%d",&n);
 int i,j;
 for (i=1;i<=n;i++)
        {scanf("%d",&x[i]);
         c[i]=x[i];
        }
 sort(c+1,c+n+1);
 for (i=n;i>=2;i--)
        if (c[i]==c[i-1]) c[i]=INF;
 sort(c+1,c+n+1);
 m=n;
 while (c[m]==INF) m--;
 if (m==1) {printf("1");return 0;} 
 int sw;
 for (i=n;i>=1;i--)
        if (x[i]==c[m])
                {sw=0;
                 k=m-1;
                 for (j=i-1;j>=1;j--)
                        {if (x[j]==c[k]) k--;
                         if (k==0)
                                {sw=1;
                                 break;
                                }
                        }
                 if (sw&&(i-j+1<rez)) rez=i-j+1;
                }
 if (rez==INF) printf("-1");
        else printf("%d",rez);
 return 0;
}