Pagini recente » Cod sursa (job #1102356) | Cod sursa (job #1142056) | Cod sursa (job #308930) | Cod sursa (job #2274568) | Cod sursa (job #1683912)
#include <fstream>
#include <iostream>
#include <algorithm>
#define pb push_back
#define f first
#define s second
#define pii pair<int, int>
#define mp make_pair
using namespace std;
const string name = "secv",
in_file = name + ".in",
out_file = name + ".out";
ifstream fin(in_file);
ofstream fout(out_file);
const int MAX = 5e3 + 5;
pii old[MAX];
int v[MAX], poz[MAX][MAX], current[MAX], length[MAX];
int n;
int main() {
fin >> n;
for (int i = 1; i <= n; i++) {
fin >> old[i].f;
old[i].s = i;
}
sort(old + 1, old + n + 1);
int cnt = 1;
v[old[1].s] = cnt;
for (int i = 2; i <= n; i++) {
if (old[i].f != old[i - 1].f)
cnt++;
v[old[i].s] = cnt;
}
for (int i = 1; i <= cnt; i++)
current[i] = 1;
for (int i = 1; i <= n; i++)
poz[v[i]][++length[v[i]]] = i;
int sol = (1 << 30) - 1;
for (; current[1] <= length[1]; current[1]++) {
int j = 2;
for (; j <= cnt; j++) {
while (current[j] <= length[j] && poz[j][current[j]] < poz[j - 1][current[j - 1]]) {
//cout << "poz[" << j << "][" << current[j] << "] < poz[" << j - 1 << "][" << current[j - 1] << "]\n";
current[j]++;
}
if (current[j] > length[j]) {
fout << (sol == (1 << 30) - 1 ? -1: sol);
return 0;
}
}
sol = min(sol, poz[cnt][current[cnt]] - poz[1][current[1]] + 1);
//cout << poz[cnt][current[cnt]] << ' ' << poz[1][current[1]] << endl;
}
fout << (sol == (1 << 30) - 1 ? -1 : sol);
return 0;
}