#include <bits/stdc++.h>
typedef long double ld;
#define int long long
#define TC int t; cin >> t; for(int _=1; _<=t; _++)
#define bismillah ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pii pair<int, int>
#define pb push_back
#define mp make_pair
using namespace std;
const int N = 2e5+7, INF = 1e18;
int a[N];
int n, k, q;
map<int, int> mapp, frq;
signed main(){
bismillah;
cin >> n >> k >> q;
for(int i=1; i<=n; i++){
cin >> a[i];
frq[a[i]]++;
mapp[a[i]] = INF;
}
int done = k;
while(q--){
int x, c; cin >> x >> c;
mapp[x] = c; done--;
if(frq[x]<c){
cout << "impossible";
return 0;
}
}
map<int, int> now;
map<int, bool> flag;
int ans = n;
int l = 1; // l==r allowed
// find prefix first
int st = 1;
for(int i=1; i<=n; i++){
now[a[i]]++;
if(now[a[i]]>=mapp[a[i]] && !flag[a[i]]){
done++;
flag[a[i]] = 1;
}
if(done==k){
ans = min(ans, i);
st = i;
break;
}
}
int r;
for(r=st; r<=n; r++){
if(r>st){
now[a[r]]++;
if(now[a[r]]>=mapp[a[r]] && !flag[a[r]]){
done++;
flag[a[r]] = 1;
}
now[a[l]]--;
if(now[a[l]]<mapp[a[l]] && flag[a[l]]){
done--;
flag[a[l]] = 0;
}l++;
}
if(done==k){
while(l<r && (now[a[l]]>mapp[a[l]] || !flag[a[l]])){
now[a[l]]--;
l++;
}
ans = min(ans, r-l+1);
/* cout << l << " " << r << '\n';
break; */
}
}
cout << ans;
}
/*
18 4 3
0 3 0 1 3 1 2 0 0 1 1 2 0 0 1 3 1 2
0 2
1 2
2 1
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
860 KB |
Output is correct |
4 |
Correct |
4 ms |
1372 KB |
Output is correct |
5 |
Correct |
3 ms |
860 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
2200 KB |
Output is correct |
2 |
Correct |
12 ms |
2396 KB |
Output is correct |
3 |
Correct |
20 ms |
2436 KB |
Output is correct |
4 |
Correct |
14 ms |
2396 KB |
Output is correct |
5 |
Correct |
300 ms |
22956 KB |
Output is correct |
6 |
Correct |
14 ms |
2396 KB |
Output is correct |
7 |
Correct |
15 ms |
2528 KB |
Output is correct |
8 |
Correct |
547 ms |
50172 KB |
Output is correct |
9 |
Correct |
168 ms |
4944 KB |
Output is correct |
10 |
Correct |
18 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
3 ms |
860 KB |
Output is correct |
14 |
Correct |
4 ms |
1368 KB |
Output is correct |
15 |
Correct |
3 ms |
860 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
464 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
344 ms |
23612 KB |
Output is correct |
2 |
Correct |
313 ms |
18516 KB |
Output is correct |
3 |
Correct |
284 ms |
18256 KB |
Output is correct |
4 |
Correct |
12 ms |
2396 KB |
Output is correct |
5 |
Correct |
105 ms |
15904 KB |
Output is correct |
6 |
Correct |
618 ms |
48720 KB |
Output is correct |
7 |
Correct |
149 ms |
4944 KB |
Output is correct |
8 |
Correct |
222 ms |
8020 KB |
Output is correct |
9 |
Correct |
13 ms |
2392 KB |
Output is correct |
10 |
Correct |
12 ms |
2396 KB |
Output is correct |
11 |
Correct |
17 ms |
2396 KB |
Output is correct |
12 |
Correct |
14 ms |
2444 KB |
Output is correct |
13 |
Correct |
295 ms |
23124 KB |
Output is correct |
14 |
Correct |
14 ms |
2392 KB |
Output is correct |
15 |
Correct |
15 ms |
2396 KB |
Output is correct |
16 |
Correct |
564 ms |
50024 KB |
Output is correct |
17 |
Correct |
168 ms |
4944 KB |
Output is correct |
18 |
Correct |
19 ms |
2360 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
3 ms |
860 KB |
Output is correct |
22 |
Correct |
4 ms |
1372 KB |
Output is correct |
23 |
Correct |
5 ms |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |