#include <bits/stdc++.h>
#define int long long
using namespace std;
int INF = 1LL<<30;
signed main() {
/*#ifdef ONLINE_JUDGE
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#endif*/ //fast IO
int n,k,c; cin >> n >> k >> c;
vector<int> ar(n); for (int i=0; i<n; i++) cin >> ar[i];
vector<int> freq(k,0);
vector<int> req(k,0);
for (int i=0; i<c; i++) {
int a,b; cin >> a >> b;
req[a] = b;
}
int unsatisfied = 0;
for (int i=0; i<k; i++) unsatisfied+=req[i]>0;
int r = -1;
int ans = INF;
for (int l=0; l<n; l++) {
while (unsatisfied>0 && r<n-1) {
r++;
freq[ar[r]]++;
unsatisfied-=freq[ar[r]]==req[ar[r]];
}
//cout << l <<" " << r <<" " << unsatisfied << endl;
if (unsatisfied==0) {
ans=min(ans,r-l+1);
}
unsatisfied+=freq[ar[l]]==req[ar[l]];
freq[ar[l]]--;
}
if (ans<INF) cout << ans << endl;
else cout << "impossible" << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
396 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 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 |
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 |
1 ms |
432 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
2396 KB |
Output is correct |
2 |
Correct |
16 ms |
2396 KB |
Output is correct |
3 |
Correct |
22 ms |
2396 KB |
Output is correct |
4 |
Correct |
18 ms |
2392 KB |
Output is correct |
5 |
Correct |
36 ms |
4180 KB |
Output is correct |
6 |
Correct |
20 ms |
2372 KB |
Output is correct |
7 |
Correct |
22 ms |
2392 KB |
Output is correct |
8 |
Correct |
34 ms |
6208 KB |
Output is correct |
9 |
Correct |
29 ms |
3012 KB |
Output is correct |
10 |
Correct |
31 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
436 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
4948 KB |
Output is correct |
2 |
Correct |
54 ms |
4292 KB |
Output is correct |
3 |
Correct |
39 ms |
4144 KB |
Output is correct |
4 |
Correct |
31 ms |
2392 KB |
Output is correct |
5 |
Correct |
61 ms |
5572 KB |
Output is correct |
6 |
Correct |
60 ms |
6740 KB |
Output is correct |
7 |
Correct |
38 ms |
2984 KB |
Output is correct |
8 |
Correct |
34 ms |
3416 KB |
Output is correct |
9 |
Correct |
17 ms |
2396 KB |
Output is correct |
10 |
Correct |
21 ms |
2408 KB |
Output is correct |
11 |
Correct |
19 ms |
2396 KB |
Output is correct |
12 |
Correct |
35 ms |
2396 KB |
Output is correct |
13 |
Correct |
49 ms |
4164 KB |
Output is correct |
14 |
Correct |
32 ms |
2140 KB |
Output is correct |
15 |
Correct |
21 ms |
2392 KB |
Output is correct |
16 |
Correct |
49 ms |
6384 KB |
Output is correct |
17 |
Correct |
33 ms |
3008 KB |
Output is correct |
18 |
Correct |
18 ms |
2392 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
2 ms |
348 KB |
Output is correct |
22 |
Correct |
2 ms |
344 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
420 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |