Cod sursa(job #2635014)

Utilizator stefan.popescuPopescu Stefan stefan.popescu Data 12 iulie 2020 22:11:23
Problema Secv Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream in ("secv.in");
ofstream out("secv.out");
vector <int> a, b, dp;
int n, x, mini=2e9;
int main()
{
    in>>n;
    for(int i=1; i<=n; i++)
    {
        in>>x;
        a.push_back(x); b.push_back(x);
    }
    sort(b.begin(), b.end());
    b.erase(unique(b.begin(), b.end()), b.end());
    dp.resize(b.size(), -1);
    for(int i=0; i<a.size(); i++)
        a[i]=lower_bound(b.begin(), b.end(), a[i])-b.begin();
    for(int i=0; i<n; i++)
    {
        if(a[i]==0)
            dp[0]=i;
        else if(dp[a[i]-1]!=-1)
        {
            dp[a[i]]=dp[a[i]-1];
            if(a[i]==b.size()-1)
                mini=min(mini, i-dp[a[i]]+1);
        }
    }
    if(mini==2e9) mini=-1;
    out<<mini;
    return 0;
}