Cod sursa(job #2093447)

Utilizator laurageorgescuLaura Georgescu laurageorgescu Data 23 decembrie 2017 18:17:13
Problema Secv Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <fstream>
#include <algorithm>

using namespace std;

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

const int nmax = 5000;

int v[nmax + 1], a[nmax + 1];

int main () {
    int n;
    fin >> n;

    for (int i = 1; i <= n; ++ i) {
        fin >> v[ i ];
        a[ i ] = v[ i ];
    }

    sort(a + 1, a + n + 1);
    int m = unique(a + 1, a + n + 1) - a - 1;

    int ans = 1 << 30;
    for (int i = m; i <= n; ++ i) {
        int j = i;
        bool ok = 1;

        for (int k = m; k > 0; -- k) {
            while (j > 0 && v[ j ] != a[ k ])
                -- j;
            if (v[ j ] == a[ k ]) {
                -- j;
            } else {
                ok = 0;
            }
        }

        if (ok) {
            ans = min(ans, i - j);
        }
    }

    if (ans == 1 << 30)
        ans = -1;
    fout << ans << "\n";

    fin.close();
    fout.close();
    return 0;
}