Cod sursa(job #2152969)

Utilizator FunnyStockyMihnea Andreescu FunnyStocky Data 5 martie 2018 21:30:40
Problema Secv Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.03 kb
#include <fstream>
#include <algorithm>

using namespace std;

ifstream cin("secv.in");
ofstream cout("secv.out");

struct PPP
{
    int y,x,ind;
};
const int nmax=5000;
int n;
PPP v[nmax+5];
int dp[nmax+5],y,ans=nmax+5;
int NEC;
bool operator<(PPP a,PPP b)
{
    if(NEC==1)
        return a.y<b.y;
    return a.ind<b.ind;
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>v[i].y;
        v[i].ind=i;
    }
    NEC=1;sort(v+1,v+n+1);
    v[0].y=-1;
    for(int i=1;i<=n;i++)
    {
        if(v[i].y!=v[i-1].y)
        {
            v[i].x=v[i-1].x+1;
            y++;
        }
        else
            v[i].x=v[i-1].x;
    }
    NEC=2;sort(v+1,v+n+1);
    for(int i=1;i<=n;i++)
    {
        if(v[i].x==1)
            dp[1]=i;
        else
            if(dp[v[i].x-1]!=0)
                dp[v[i].x]=dp[v[i].x-1];
        if(i==y && dp[y]!=0)
            ans=min(ans,i-dp[y]+1);
    }
    if(ans<=n)
        cout<<ans;
    else
        cout<<-1;
    return 0;
}
/**

**/