Cod sursa(job #803359)

Utilizator matei_cChristescu Matei matei_c Data 27 octombrie 2012 14:22:12
Problema Secv Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.87 kb
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;

#define maxn 5001
#define inf 2000000

int n ;
pair <int,int> a[maxn] ;
int sol = inf  ;
int v[maxn] ;
int poz[maxn] ;

int main()
{
	
	freopen("secv.in","r",stdin);
	freopen("secv.out","w",stdout);
	
	scanf("%d",&n);
	
	for( int i = 1 ; i <= n ; ++ i )
	{
		scanf("%d",&a[i].first);
		a[i].second = i ;
	}	
	
	sort ( &a[1] , &a[n+1] ) ;
	
	int val = 1 ;
	
	v[ a[1].second ] = 1 ;
	
	for( int i = 2 ; i <= n ; ++ i )
	{
		if( a[i].first != a[i-1].first )
			++ val ;
		v[ a[i].second ] = val ; 
	}	
	
	for( int i = 1 ; i <= n ; ++ i )
	{
		if( v[i] == 1 )
			poz[1] = i ;
		else
		{
			poz[ v[i] ] = poz[ v[i] - 1 ] ;
			if( v[i] == val && poz[val] > 0 )
				sol = min ( sol , i - poz[ v[i] ] + 1 ) ;
		}	
	}	
	
	printf("%d\n", sol); 
	
	return 0 ;
	
}