Cod sursa(job #3343110)

Utilizator FistfullOfDollar059Andrei Marin Popa FistfullOfDollar059 Data 26 februarie 2026 14:49:01
Problema Secv Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.69 kb
#include <bits/stdc++.h>
using namespace std;
# define ll long long

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    ifstream fin("secv.in");
    ofstream cout("secv.out");

    int n;
    fin>>n;
    vector<int> a(n);
    vector<int> simple;
    vector<int> conv;

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



    /// CONVERSION TO SIMPLE NR

    simple = a;
    sort(simple.begin(),simple.end());
    conv = simple;
    conv[0] = 0;
    int cnt = 1;
    vector<int> sir;
    for(int i = 1; i < n; i++){
        if(simple[i] == simple[i-1]){
            conv[i] = conv[i-1];
        }else{
            conv[i] = cnt;
            cnt++;
        }
    }

    int len = cnt;

    for(int i = 0; i < n; i++){
        auto it = lower_bound(simple.begin(),simple.end(),a[i]);
        a[i] = conv[it-simple.begin()];
    }





    /// DP PART



    vector<vector<int>> dp(n,vector<int>(len,-1));
    vector<int> helpy(len,-1);

    for(int i = n-1; i >= 0; i--){
        helpy[a[i]] = i;
        dp[i] = helpy;
    }




       /// SEEING SMALLEST

    cnt = 0;

    int st, dr;
    int bestLen = 1000000000;
    for(int i = 0; i < n; i++){
        if(a[i] == 0){
            st = i;
            dr = i;
            cnt = i;
            for(int j = 1; j < len; j++){
                cnt = dp[cnt][j];
                if(cnt == -1){dr = 50000000;break;}
                dr = cnt;
            }
            bestLen = min(bestLen,dr-st+1);
        }
    }
    if(bestLen > n+1){
        cout<<"-1";
    }else{
        cout<<bestLen;
    }












    return 0;
}