This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = (int) 2e5 + 7;
const int INF = (int) 1e9 + 7;
int n;
int k;
int r;
int d[N];
vector<int> positions[N];
int min_quantity[N];
int rgh[N];
void upd(int l, int r) {
rgh[l] = max(rgh[l], r);
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// freopen ("input.txt", "r", stdin);
cin >> n >> k >> r;
for (int i = 0; i < n; i++) {
cin >> d[i];
positions[d[i]].push_back(i);
}
for (int i = 0; i < r; i++) {
int base, quantity;
cin >> base >> quantity;
min_quantity[base] = max(min_quantity[base], quantity);
}
for (int i = 0; i < n; i++) {
rgh[i] = i;
}
for (int base = 0; base < k; base++) {
if (min_quantity[base] == 0) {
continue;
}
int sz = (int) positions[base].size();
int need = min_quantity[base];
if (sz < need) {
upd(0, n);
continue;
}
/// sz >= min_quantity[base]
upd(0, positions[base][need - 1]);
for (int i = 0; i < sz; i++) {
if (i + need < sz) {
upd(positions[base][i] + 1, positions[base][i + need]);
} else {
upd(positions[base][i] + 1, n);
}
}
}
for (int l = 0; l < n; l++) {
rgh[l + 1] = max(rgh[l + 1], rgh[l]);
}
int best_dif = INF;
for (int l = 0; l < n; l++) {
if (rgh[l] != n) {
best_dif = min(best_dif, rgh[l] - l + 1);
}
}
if (best_dif == INF) {
cout << "impossible\n";
return 0;
}
cout << best_dif << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |