#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define PB push_back
using pii = pair<int, int>;
const int MXN = 2e5+5;
int n, k, r;
int A[MXN], req[MXN], tot[MXN], cnt[MXN];
int satisfied = 0;
int main() {
cin >> n >> k >> r;
for(int i = 0; i < n; i++) cin >> A[i];
for(int i = 0; i < n; i++) tot[A[i]]++;
for(int i = 0; i < r; i++) {
int a, b; cin >> a >> b;
req[a] = b;
if(tot[a] < b) {
cout << "impossible\n";
return 0;
}
}
auto add = [](int i) {
cnt[A[i]]++;
if(cnt[A[i]] == req[A[i]]) satisfied++;
};
auto del = [](int i) {
if(cnt[A[i]] == req[A[i]]) satisfied--;
cnt[A[i]]--;
};
auto ok = []() {
return satisfied == r;
};
int lo = 1, hi = n;
while(lo < hi) {
int m = (lo+hi)/2;
satisfied = 0;
fill(cnt, cnt+k, 0);
int i;
for(i = 0; i < m; i++) add(i);
bool f = false;
if(ok()) f = true;
for(; i < n; i++) {
add(i), del(i-m);
if(ok()) f = true;
}
if(f) {
hi = m;
}
else {
lo = m+1;
}
}
cout << lo << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
308 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
324 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 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 |
304 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
308 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
1468 KB |
Output is correct |
2 |
Correct |
49 ms |
1468 KB |
Output is correct |
3 |
Correct |
53 ms |
1508 KB |
Output is correct |
4 |
Correct |
49 ms |
1492 KB |
Output is correct |
5 |
Correct |
72 ms |
2856 KB |
Output is correct |
6 |
Correct |
44 ms |
1468 KB |
Output is correct |
7 |
Correct |
33 ms |
1536 KB |
Output is correct |
8 |
Correct |
82 ms |
3840 KB |
Output is correct |
9 |
Correct |
71 ms |
2120 KB |
Output is correct |
10 |
Correct |
46 ms |
1468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
328 KB |
Output is correct |
15 |
Correct |
2 ms |
320 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
308 KB |
Output is correct |
20 |
Correct |
1 ms |
304 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
100 ms |
3684 KB |
Output is correct |
2 |
Correct |
100 ms |
3368 KB |
Output is correct |
3 |
Correct |
94 ms |
3076 KB |
Output is correct |
4 |
Correct |
49 ms |
1472 KB |
Output is correct |
5 |
Correct |
54 ms |
2804 KB |
Output is correct |
6 |
Correct |
140 ms |
5252 KB |
Output is correct |
7 |
Correct |
68 ms |
2096 KB |
Output is correct |
8 |
Correct |
78 ms |
2492 KB |
Output is correct |
9 |
Correct |
45 ms |
1416 KB |
Output is correct |
10 |
Correct |
50 ms |
1472 KB |
Output is correct |
11 |
Correct |
49 ms |
1508 KB |
Output is correct |
12 |
Correct |
53 ms |
1388 KB |
Output is correct |
13 |
Correct |
75 ms |
2844 KB |
Output is correct |
14 |
Correct |
37 ms |
1464 KB |
Output is correct |
15 |
Correct |
31 ms |
1644 KB |
Output is correct |
16 |
Correct |
84 ms |
3932 KB |
Output is correct |
17 |
Correct |
66 ms |
1996 KB |
Output is correct |
18 |
Correct |
51 ms |
1484 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
324 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |