Cod sursa(job #1168115)

Utilizator gerd13David Gergely gerd13 Data 6 aprilie 2014 22:47:01
Problema Secv Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <fstream>
#include <algorithm>
#include <vector>
#include <map>

using namespace std ;

const int NMAX =  5005 ;
const int INF = 0x3f3f3f3f ;

    ifstream cin("secv.in") ;
    ofstream cout("secv.out") ;

int N, V[NMAX], M, sol = INF, act;
vector <int> D ;
map <int, bool> Q;

inline int min(int a, int b) { if(a > b) return b ; else return a ;}

int main()
{

    cin >> N ;
    for(int i = 0 ;i < N ; ++ i)
        {cin >> V [i] ;
        Q[V[i]] = 1 ;
        }
        int j ;

    for(map<int, bool> :: iterator it = Q.begin() ; it != Q.end(); ++ it)
        D.push_back(it -> first) ;
    sort(D.begin(), D.end()) ;

    for(int i = 0 ; i < N ; ++ i)
        if(V[i] == D[0])
        {
            act = 1 ;
            j = i + 1 ;
            for( ; act < D.size() && j < N ; ++ j)
                if(D[act] == V[j])

                ++ act ;
                if( act ==  D.size())
                    sol = min (sol, j - i) ;


        }

        if(sol == INF)
            cout << -1 << '\n' ;
        else cout << sol << '\n' ;

    cin.close() ;
    cout.close() ;
    return 0 ;
}