#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define fi first
#define se second
const int mod=998244353, maxn=2e5+5;
ll n, k, r, a[maxn], tipe, butuh[maxn], ki, ka, mid, res, unik, cukup, cnt[maxn], x;
bool penting[maxn], bisa;
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> n >> k >> r;
for(int i=1; i<=n; i++) cin >> a[i];
for(int i=1; i<=r; i++) {
cin >> tipe >> x;
penting[tipe]=true;
butuh[tipe]=x;
}
ki=1, ka=n, res=-1;
while(ki<=ka) {
mid=(ki+ka)/2, bisa=false, unik=cukup=0;
for(int i=0; i<k; i++) cnt[i]=0;
for(int i=1; i<=n; i++) {
if(i>mid) {
cnt[a[i-mid]]--;
if(cnt[a[i-mid]]==0) unik--;
if(penting[a[i-mid]]&&(cnt[a[i-mid]]+1==butuh[a[i-mid]])) cukup--;
}
cnt[a[i]]++;
if(cnt[a[i]]==1) unik++;
if(penting[a[i]]&&(cnt[a[i]]==butuh[a[i]])) cukup++;
if(cukup==r) bisa=true;
// cout << i << ": " << unik << ' ' << cukup << '\n';
}
if(bisa) {
res=mid;
ka=mid-1;
} else ki=mid+1;
}
if(res==-1) cout << "impossible";
else cout << res;
cout << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 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 |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 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 |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
324 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
1912 KB |
Output is correct |
2 |
Correct |
33 ms |
1896 KB |
Output is correct |
3 |
Correct |
39 ms |
1896 KB |
Output is correct |
4 |
Correct |
35 ms |
1876 KB |
Output is correct |
5 |
Correct |
34 ms |
2580 KB |
Output is correct |
6 |
Correct |
22 ms |
2264 KB |
Output is correct |
7 |
Correct |
22 ms |
2400 KB |
Output is correct |
8 |
Correct |
40 ms |
4684 KB |
Output is correct |
9 |
Correct |
34 ms |
2892 KB |
Output is correct |
10 |
Correct |
26 ms |
2276 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
3248 KB |
Output is correct |
2 |
Correct |
50 ms |
4488 KB |
Output is correct |
3 |
Correct |
53 ms |
4188 KB |
Output is correct |
4 |
Correct |
30 ms |
2276 KB |
Output is correct |
5 |
Correct |
50 ms |
5716 KB |
Output is correct |
6 |
Correct |
58 ms |
6956 KB |
Output is correct |
7 |
Correct |
30 ms |
3028 KB |
Output is correct |
8 |
Correct |
50 ms |
3400 KB |
Output is correct |
9 |
Correct |
27 ms |
2260 KB |
Output is correct |
10 |
Correct |
34 ms |
2276 KB |
Output is correct |
11 |
Correct |
39 ms |
2312 KB |
Output is correct |
12 |
Correct |
29 ms |
2204 KB |
Output is correct |
13 |
Correct |
40 ms |
3656 KB |
Output is correct |
14 |
Correct |
20 ms |
2264 KB |
Output is correct |
15 |
Correct |
22 ms |
2456 KB |
Output is correct |
16 |
Correct |
41 ms |
4784 KB |
Output is correct |
17 |
Correct |
32 ms |
2900 KB |
Output is correct |
18 |
Correct |
33 ms |
2256 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
368 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
328 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
328 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |