Cod sursa(job #617470)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 14 octombrie 2011 21:55:33
Problema Secv Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.44 kb
/*#include <iostream>
#include <cstdio>
#include <set>

#define NMax 5005

using namespace std;

int N, DP[NMax], V[NMax], LMax, F[NMax], S=NMax;
set <int> Sequence;

inline int Min (int a, int b)
{
    if (a<b)
    {
        return a;
    }
    return b;
}

bool Valid ()
{
    int iMax=N;
    for (int i=N; i>0; --i)
    {
        if (DP[i]==LMax)
        {
            iMax=i;
            break;
        }
    }
    for (int i=iMax; i>0; i=F[i])
    {
        Sequence.insert (V[i]);
    }
    for (int i=1; i<=N; ++i)
    {
        if (Sequence.find (V[i])==Sequence.end ())
        {
            return false;
        }
    }
    return true;
}

void Read ()
{
    freopen ("secv.in", "r", stdin);
    scanf ("%d", &N);
    for (int i=1; i<=N; ++i)
    {
        scanf ("%d", &V[i]);
    }
}

void Print ()
{
    freopen ("secv.out", "w", stdout);
    printf ("%d\n", S);
}

void LIS ()
{
    for (int i=1; i<=N; ++i)
    {
        DP[i]=1;
        for (int j=i-1; j>0; --j)
        {
            if (V[j]<V[i])
            {
                if (DP[j]+1>DP[i])
                {
                    DP[i]=DP[j]+1;
                    F[i]=j;
                }
            }
        }
        if (DP[i]>LMax)
        {
            LMax=DP[i];
        }
    }
}

int Length (int i)
{
    if (DP[i]==1)
    {
        return 1;
    }
    return Length (F[i])+i-F[i];
}

int main()
{
    Read ();
    LIS ();
    if (!Valid ())
    {
        S=-1;
        Print ();
        return 0;
    }
    for (int i=N; i>0; --i)
    {
        if (DP[i]==LMax)
        {
            S=Min (S, Length (i));
        }
    }
    Print ();
    return 0;
}
*/

#include <fstream>
#include <vector>
using namespace std;

ifstream fin("secv.in");
ofstream fout("secv.out");

const int maxn=5005;
int i,j,N,Min;
vector<int> A,B;
int C[maxn];

int main() {
	fin >> N; int x;
	if(N==0) {
		fout << 0;
		return 0;
	}
	for(i=1;i<=N;i++) {
		fin >> x;
		A.push_back(x);
	}
	B=A;
	sort(B.begin(),B.end());
	C[++C[0]]=B[0];
	for(i=1;i<N;i++)
		if(B[i]!=B[i-1])
			C[++C[0]]=B[i];
	Min=N+1;
	for(i=0;i<N-C[0]+1;i++) {
		if(A[i]==C[1]) {
			int k=2;
			for(j=i+1;j<N;j++) {
				if(k>C[0]) break;
				if(A[j]==C[k]) k++;
			}
			if(k<C[0]) continue;
			if(j==N && C[C[0]]!=A[N-1]) continue;
			Min=min(Min,j-i);
		}
	}
	if(Min==N+1) fout << -1;
	else fout << Min;
}