#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define elif else if
#define dbg(v)\
cout << "line(" << __LINE__ << ") -> " << #v << " = " << (v) << endl;
signed main() {
ios_base::sync_with_stdio(false); cin.tie(0);
int n, k, R; cin >> n >> k >> R;
int req[n], cnt[n], arr[n], sz = 0;
unordered_set<int> nums;
fill(req, req+n, 0); fill(cnt, cnt+n, 0);
for(int i = 0; i < n; i++) cin >> arr[i];
for(int i = 0; i < R; i++){
int b, q; cin >> b >> q; sz += q;
req[b] = q; nums.insert(b);
}
int l = 0, r = 0, ans = LLONG_MAX;
while(r < n){
while(!nums.empty() && r < n){
int val = arr[r]; cnt[val]++;
if(req[val] != 0 && cnt[val] >= req[val]) nums.erase(val);
r++;
}
if(r == n) break;
ans = min(ans, r-l);
int val = arr[l]; cnt[val]--;
if(cnt[val] < req[val]) nums.insert(val);
l++;
}
while(l <= n-sz && nums.empty()){
ans = min(ans, n-l);
int val = arr[l]; cnt[val]--;
if(cnt[val] < req[val]) break;
l++;
}
if(ans == LLONG_MAX){cout << "impossible"; return 0;}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 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 |
0 ms |
600 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
4956 KB |
Output is correct |
2 |
Correct |
14 ms |
5144 KB |
Output is correct |
3 |
Correct |
15 ms |
4956 KB |
Output is correct |
4 |
Correct |
13 ms |
4956 KB |
Output is correct |
5 |
Correct |
14 ms |
4952 KB |
Output is correct |
6 |
Correct |
10 ms |
4956 KB |
Output is correct |
7 |
Correct |
13 ms |
5648 KB |
Output is correct |
8 |
Correct |
16 ms |
5724 KB |
Output is correct |
9 |
Correct |
13 ms |
5920 KB |
Output is correct |
10 |
Correct |
11 ms |
5312 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 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 |
33 ms |
8300 KB |
Output is correct |
2 |
Correct |
30 ms |
7484 KB |
Output is correct |
3 |
Correct |
20 ms |
5788 KB |
Output is correct |
4 |
Correct |
15 ms |
5024 KB |
Output is correct |
5 |
Correct |
30 ms |
8300 KB |
Output is correct |
6 |
Correct |
51 ms |
9604 KB |
Output is correct |
7 |
Correct |
15 ms |
5464 KB |
Output is correct |
8 |
Correct |
18 ms |
5724 KB |
Output is correct |
9 |
Correct |
12 ms |
5144 KB |
Output is correct |
10 |
Correct |
14 ms |
4956 KB |
Output is correct |
11 |
Correct |
15 ms |
5148 KB |
Output is correct |
12 |
Correct |
14 ms |
4956 KB |
Output is correct |
13 |
Correct |
14 ms |
5156 KB |
Output is correct |
14 |
Correct |
10 ms |
4956 KB |
Output is correct |
15 |
Correct |
11 ms |
5588 KB |
Output is correct |
16 |
Correct |
16 ms |
5724 KB |
Output is correct |
17 |
Correct |
13 ms |
5724 KB |
Output is correct |
18 |
Correct |
11 ms |
5468 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 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 |
0 ms |
460 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
600 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 |