Pagini recente » Cod sursa (job #2566188) | Cod sursa (job #1261394) | Cod sursa (job #3187880) | Cod sursa (job #58061) | Cod sursa (job #1113119)
#include <algorithm>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("secv.in");
ofstream fout("secv.out");
int n, v[5005], ap[5005], res = 5005;
int norm[5005];
void normalizare() {
for (int i = 1; i <= n; ++i)
norm[i] = v[i];
sort(norm + 1, norm + n + 1);
for (int i = 1; i <= n; ) {
int j = i;
while (norm[i] == norm[j] and j <= n)
++j;
norm[++norm[0]] = norm[i];
i = j;
}
for (int i = 1; i <= n; ++i) {
int p = 0;
for (int cnt = 1 << 12; cnt; cnt >>= 1)
if (p + cnt <= norm[0] and norm[p + cnt] <= v[i])
p += cnt;
v[i] = p;
}
}
int lap[5005];
int main() {
fin >> n;
for (int i = 1; i <= n; ++i)
fin >> v[i];
if (n == 0 || n == 1) {
fout << n << '\n';
return 0;
}
normalizare();
for (int i = 1; i <= n; ++i) {
if (v[i] == 1)
lap[1] = i;
else
lap[v[i]] = lap[v[i] - 1];
if (v[i] == norm[0])
res = min(res, i - lap[v[i]] + 1);
}
if (res > n)
fout << "-1\n";
else
fout << res << '\n';
fin.close();
fout.close();
}