Pagini recente » Cod sursa (job #1000589) | Cod sursa (job #918832) | Cod sursa (job #1305523) | Cod sursa (job #3142452) | Cod sursa (job #2635014)
#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;
}