#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
int n, k, R;
int niz[200005];
int treba[200005];
int br[200005];
bool check(int x){
for(int i=1; i<=k; i++){
br[i] = 0;
}
int cnt = 0;
for(int i=1; i<=x; i++){
br[niz[i]]++;
if(br[niz[i]] == treba[niz[i]]) cnt++;
}
if(cnt >= R) return 1;
for(int i=x+1; i<=n; i++){
br[niz[i]]++;
if(br[niz[i]] == treba[niz[i]]) cnt++;
br[niz[i-x]]--;
if(br[niz[i-x]] == treba[niz[i-x]]-1) cnt--;
if(cnt >= R) return 1;
}
return 0;
}
int main(){
ios_base::sync_with_stdio(false);
cout.precision(10);
cout<<fixed;
cin >> n >> k >> R;
for(int i=1; i<=n; i++){
cin >> niz[i];
niz[i]++;
}
for(int i=1; i<=k; i++){
treba[i] = n+500;
}
for(int i=1; i<=R; i++){
int a, b;
cin >> a >> b;
treba[a+1] = b;
}
int l = 1, r = n, res = n+1;
while(l <= r){
int mid = (l+r)/2;
if(check(mid)){
r = mid-1;
res = mid;
}
else l = mid+1;
}
if(res == n+1){
cout << "impossible\n";
}
else cout << res << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
1104 KB |
Output is correct |
2 |
Correct |
30 ms |
1056 KB |
Output is correct |
3 |
Correct |
21 ms |
1104 KB |
Output is correct |
4 |
Correct |
28 ms |
1108 KB |
Output is correct |
5 |
Correct |
39 ms |
1684 KB |
Output is correct |
6 |
Correct |
30 ms |
1496 KB |
Output is correct |
7 |
Correct |
28 ms |
1568 KB |
Output is correct |
8 |
Correct |
47 ms |
3916 KB |
Output is correct |
9 |
Correct |
34 ms |
2092 KB |
Output is correct |
10 |
Correct |
33 ms |
1488 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
320 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
0 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
0 ms |
204 KB |
Output is correct |
25 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
1712 KB |
Output is correct |
2 |
Correct |
52 ms |
3140 KB |
Output is correct |
3 |
Correct |
53 ms |
2752 KB |
Output is correct |
4 |
Correct |
16 ms |
1380 KB |
Output is correct |
5 |
Correct |
54 ms |
3992 KB |
Output is correct |
6 |
Correct |
71 ms |
4456 KB |
Output is correct |
7 |
Correct |
44 ms |
2160 KB |
Output is correct |
8 |
Correct |
49 ms |
2372 KB |
Output is correct |
9 |
Correct |
31 ms |
1432 KB |
Output is correct |
10 |
Correct |
30 ms |
1476 KB |
Output is correct |
11 |
Correct |
21 ms |
1476 KB |
Output is correct |
12 |
Correct |
32 ms |
1520 KB |
Output is correct |
13 |
Correct |
40 ms |
2832 KB |
Output is correct |
14 |
Correct |
27 ms |
1476 KB |
Output is correct |
15 |
Correct |
29 ms |
1644 KB |
Output is correct |
16 |
Correct |
47 ms |
3812 KB |
Output is correct |
17 |
Correct |
39 ms |
2028 KB |
Output is correct |
18 |
Correct |
33 ms |
1500 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
0 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
0 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |