#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 |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5016 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
5028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5028 KB |
Output is correct |
2 |
Correct |
4 ms |
5076 KB |
Output is correct |
3 |
Correct |
4 ms |
5168 KB |
Output is correct |
4 |
Correct |
4 ms |
5204 KB |
Output is correct |
5 |
Correct |
4 ms |
5076 KB |
Output is correct |
6 |
Correct |
4 ms |
5032 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
4 ms |
4948 KB |
Output is correct |
10 |
Correct |
4 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
5028 KB |
Output is correct |
13 |
Correct |
3 ms |
5028 KB |
Output is correct |
14 |
Correct |
3 ms |
5024 KB |
Output is correct |
15 |
Correct |
3 ms |
5024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
8268 KB |
Output is correct |
2 |
Correct |
15 ms |
7932 KB |
Output is correct |
3 |
Correct |
15 ms |
7980 KB |
Output is correct |
4 |
Correct |
16 ms |
8244 KB |
Output is correct |
5 |
Correct |
40 ms |
10316 KB |
Output is correct |
6 |
Correct |
14 ms |
8148 KB |
Output is correct |
7 |
Correct |
16 ms |
8068 KB |
Output is correct |
8 |
Correct |
51 ms |
14136 KB |
Output is correct |
9 |
Correct |
23 ms |
8916 KB |
Output is correct |
10 |
Correct |
21 ms |
7980 KB |
Output is correct |
11 |
Correct |
3 ms |
5028 KB |
Output is correct |
12 |
Correct |
3 ms |
5032 KB |
Output is correct |
13 |
Correct |
4 ms |
5076 KB |
Output is correct |
14 |
Correct |
4 ms |
5112 KB |
Output is correct |
15 |
Correct |
4 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
5028 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
5028 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
22 |
Correct |
3 ms |
5020 KB |
Output is correct |
23 |
Correct |
4 ms |
5080 KB |
Output is correct |
24 |
Correct |
3 ms |
5032 KB |
Output is correct |
25 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
11136 KB |
Output is correct |
2 |
Correct |
42 ms |
10452 KB |
Output is correct |
3 |
Correct |
33 ms |
10060 KB |
Output is correct |
4 |
Correct |
22 ms |
7820 KB |
Output is correct |
5 |
Correct |
54 ms |
12192 KB |
Output is correct |
6 |
Correct |
78 ms |
14964 KB |
Output is correct |
7 |
Correct |
25 ms |
8840 KB |
Output is correct |
8 |
Correct |
31 ms |
9476 KB |
Output is correct |
9 |
Correct |
14 ms |
8284 KB |
Output is correct |
10 |
Correct |
15 ms |
7948 KB |
Output is correct |
11 |
Correct |
17 ms |
7996 KB |
Output is correct |
12 |
Correct |
15 ms |
8284 KB |
Output is correct |
13 |
Correct |
31 ms |
10408 KB |
Output is correct |
14 |
Correct |
19 ms |
8116 KB |
Output is correct |
15 |
Correct |
17 ms |
8148 KB |
Output is correct |
16 |
Correct |
38 ms |
14088 KB |
Output is correct |
17 |
Correct |
23 ms |
8888 KB |
Output is correct |
18 |
Correct |
15 ms |
8080 KB |
Output is correct |
19 |
Correct |
3 ms |
5076 KB |
Output is correct |
20 |
Correct |
3 ms |
5076 KB |
Output is correct |
21 |
Correct |
4 ms |
5076 KB |
Output is correct |
22 |
Correct |
4 ms |
5108 KB |
Output is correct |
23 |
Correct |
4 ms |
5092 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
25 |
Correct |
3 ms |
4932 KB |
Output is correct |
26 |
Correct |
3 ms |
4948 KB |
Output is correct |
27 |
Correct |
3 ms |
5024 KB |
Output is correct |
28 |
Correct |
3 ms |
5024 KB |
Output is correct |
29 |
Correct |
3 ms |
5024 KB |
Output is correct |
30 |
Correct |
3 ms |
4948 KB |
Output is correct |
31 |
Correct |
3 ms |
4948 KB |
Output is correct |
32 |
Correct |
2 ms |
4948 KB |
Output is correct |
33 |
Correct |
2 ms |
4948 KB |
Output is correct |