#include <bits/stdc++.h>
using namespace std;
const int maxn = 200000; // R, K <= N
const int INF = 2e9;
int n, // length of real DNA
k, // alphabet size
r; // number of minimum quantities
int min_quant[maxn];
int cur_quant[maxn];
int dna[maxn];
int current_satisfied;
int ans = INF;
int main() {
// freopen("main.in", "r", stdin);
cin >> n >> k >> r;
for (int i = 0; i < n; i++) cin >> dna[i];
for (int i = 0; i < r; i++) {
int a; cin >> a;
cin >> min_quant[a];
}
for (int i = 0; i < k; i++)
current_satisfied += cur_quant[i] >= min_quant[i];
int l = 0;
for (int i = 0; i < n; i++) { // [l, i] inclusive
// add new
cur_quant[dna[i]]++;
if (cur_quant[dna[i]] == min_quant[dna[i]])
current_satisfied++;
// remove as many as possible
while (current_satisfied == k && l <= i) {
ans = min(ans, i - l + 1);
if (cur_quant[dna[l]] == min_quant[dna[l]]) current_satisfied--;
cur_quant[dna[l]]--;
l++;
}
}
if (ans == INF) { cout << "impossible" << endl; return 0; }
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
308 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
312 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
324 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
312 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
312 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
1384 KB |
Output is correct |
2 |
Correct |
31 ms |
1456 KB |
Output is correct |
3 |
Correct |
30 ms |
1484 KB |
Output is correct |
4 |
Correct |
30 ms |
1460 KB |
Output is correct |
5 |
Correct |
52 ms |
2668 KB |
Output is correct |
6 |
Correct |
33 ms |
1356 KB |
Output is correct |
7 |
Correct |
32 ms |
1608 KB |
Output is correct |
8 |
Correct |
64 ms |
3028 KB |
Output is correct |
9 |
Correct |
48 ms |
2004 KB |
Output is correct |
10 |
Correct |
28 ms |
1364 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
308 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
312 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
308 KB |
Output is correct |
23 |
Correct |
1 ms |
312 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
3400 KB |
Output is correct |
2 |
Correct |
72 ms |
3040 KB |
Output is correct |
3 |
Correct |
66 ms |
2756 KB |
Output is correct |
4 |
Correct |
27 ms |
1460 KB |
Output is correct |
5 |
Correct |
115 ms |
4020 KB |
Output is correct |
6 |
Correct |
103 ms |
4524 KB |
Output is correct |
7 |
Correct |
46 ms |
2116 KB |
Output is correct |
8 |
Correct |
52 ms |
2368 KB |
Output is correct |
9 |
Correct |
26 ms |
1436 KB |
Output is correct |
10 |
Correct |
32 ms |
1356 KB |
Output is correct |
11 |
Correct |
29 ms |
1468 KB |
Output is correct |
12 |
Correct |
30 ms |
1372 KB |
Output is correct |
13 |
Correct |
49 ms |
2548 KB |
Output is correct |
14 |
Correct |
30 ms |
1452 KB |
Output is correct |
15 |
Correct |
31 ms |
1628 KB |
Output is correct |
16 |
Correct |
54 ms |
3024 KB |
Output is correct |
17 |
Correct |
45 ms |
2072 KB |
Output is correct |
18 |
Correct |
26 ms |
1360 KB |
Output is correct |
19 |
Correct |
1 ms |
316 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
324 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
320 KB |
Output is correct |
24 |
Correct |
1 ms |
316 KB |
Output is correct |
25 |
Correct |
0 ms |
312 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
312 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |