Cod sursa(job #2682044)

Utilizator Alex_tz307Lorintz Alexandru Alex_tz307 Data 7 decembrie 2020 17:49:21
Problema Secv Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f

using namespace std;

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

int main() {
    int N;
    fin >> N;
    vector<int> a(N);
    map<int,bool> ap;
    for(int &x : a) {
        fin >> x;
        ap[x] = true;
    }
    vector<int> b;
    for(auto x : ap)
        b.emplace_back(x.first);
    int M = b.size();
    if(M == 1) {
        fout << '1';
        return 0;
    }
    int ans = INF;
    for(int i = 0; i < N; ++i)
        if(a[i] == b[0]) {
            int p = 1;
            for(int j = i + 1; j < N; ++j)
                if(a[j] == b[p]) {
                    ++p;
                    if(j - i + 1 >= ans)
                        break;
                    if(p == M) {
                        ans = j - i + 1;
                        break;
                    }
                }
        }
    if(ans == INF)
        ans = -1;
    fout << ans;
}