Cod sursa(job #2637021)

Utilizator OvidRata Ovidiu Ovid Data 20 iulie 2020 22:11:26
Problema Secv Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.64 kb
#include<bits/stdc++.h>
using namespace std;
#define INIT  ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
#define int ll

int n, a[5010], b[5010];
int sir[5010];


ifstream fin("secv.in"); ofstream fout("secv.out");
#define cin fin
#define cout fout



int submax(int f, int s){

int ln=0;
for(int i=f; i<=s; i++){
        if(ln==0){
            ln=1; sir[ln]=a[i]; continue;
        }
    int r=ln, l=1, m=(l+r)/2;
    while(l<r){
        m=(l+r)/2;
        if(sir[m]>=a[i]){
            r=m;
        }
        else{
            l=m+1;
        }
        m=(l+r)/2;
    }

    if(sir[m]<a[i]){
        ln++; m++;
    }
    sir[m]=a[i];
}



return ln;
}





int32_t main(){
INIT
cin>>n;
for(int i=1; i<=n; i++){
    cin>>a[i]; b[i]=a[i];
}
sort(b+1, b+1+n);
int cnt=0;
int ac=-1;
for(int i=1; i<=n; i++){
    if(b[i]!=ac){ac=b[i]; cnt++;}
}



int res=n;
int lnm=0;
for(int i=1; i<=n; i++){

    int l=i, r=n, m=(l+r)/2;
    while(l<r){
        m=(l+r)/2;
        int ln=submax(i, m);
        //cout<<i<<" "<<m<<" "<<ln<<"\n";
        if(ln>=cnt){
            r=m;
        }
        else{
            l=m+1;
        }
        m=(l+r)/2;
    }

    int ln=submax(i, m);
    lnm=max(lnm, ln);
        if(ln>=cnt){
            res=min(res, m-i+1);
        }

}
if(lnm<cnt){cout<<-1; return 0;}
cout<<res;






return 0;
}