Pagini recente » Statistici Mitrofan Andrei (BladeRider) | Monitorul de evaluare | Cod sursa (job #2022698)
#include <algorithm>
#include <iostream>
#include <fstream>
#include <bitset>
using namespace std;
ifstream in("secv.in");
ofstream out("secv.out");
const int maxn = 500005;
pair <int, int> aux[maxn];
bitset <maxn> viz;
int v[maxn];
int mn = (1 << 30);
int nr;
int n;
void incearca(int st)
{
for(int i = 0; i < maxn; i++)
viz[i] = 0;
viz[0] = 1;
for(int i = st; i <= n; i++)
{
if(viz[v[i] - 1])
viz[v[i]] = 1;
if(viz[nr] == 1)
{
mn = min(mn, i - st + 1);
return;
}
}
//cerr << st << ":\n";
//for(int i = 1; i <= n; i++)
//cerr << viz[i] << " ";
//cerr << "\n";
return;
}
int main()
{
in >> n;
for(int i = 1; i <= n; i++)
{
in >> aux[i].first;
aux[i].second = i;
}
sort(aux + 1, aux + n + 1);
aux[0].first = -1;
for(int i = 1; i <= n; i++)
{
if(aux[i].first != aux[i - 1].first)
nr++;
v[aux[i].second] = nr;
}
for(int i = 1; i <= n; i++)
incearca(i);
if(mn == (1 << 30))
{
out << -1 << "\n";
return 0;
}
out << mn << "\n";
return 0;
}