Cod sursa(job #1327129)

Utilizator dariusdariusMarian Darius dariusdarius Data 26 ianuarie 2015 13:30:01
Problema Secv Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
const int MAX_N = 5005;

int best[MAX_N];
int a[MAX_N];
int poz[MAX_N];

int main() {
    ifstream fin("secv.in");
    ofstream fout("secv.out");
    int n;
    fin >> n; vector<int> q;
    for (int i = 1; i <= n; ++ i) {
        fin >> a[i];
        q.push_back(a[i]);
    }
    sort(q.begin(), q.end());
    q.erase(unique(q.begin(), q.end()), q.end());
    int k = q.size(), answer = n + 1;
    for (int i = 1; i <= n; ++ i) {
        a[i] = upper_bound(q.begin(), q.end(), a[i]) - q.begin();
        poz[a[i]] = i;
        if (a[i] == 1) {
            best[i] = i;
        } else if (poz[a[i] - 1]) {
            best[i] = best[poz[a[i] - 1]];
            if (a[i] == k && best[i]) {
                answer = min(answer, i - best[i] + 1);
            }
        }
    }
    fout << answer << "\n";

    return 0;
}