#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);
}
if(sz > n){cout << "impossible"; return 0;}
int l = 0, r = 0, ans = LLONG_MAX; bool stop = false;
while(l < n-sz){
if(l == 0 && r == n) break;
while(!nums.empty()){
if(l == 0 && r == n) stop = true;
int val = arr[r]; cnt[val]++;
if(req[val] != 0 && cnt[val] >= req[val])nums.erase(val);
if(!nums.empty()) r++;
}
if(stop) break;
ans = min(ans, r-l+1);
int val = arr[l]; cnt[val]--;
if(req[val] != 0 && cnt[val] < req[val]) nums.insert(val);
l++;
}
fill(cnt, cnt+n, 0);
for(int i = n-sz+1; i < n; i++) cnt[arr[i]]++;
bool ok = true;
for(int i = 0; i < n; i++){
if(req[i] > cnt[i]) ok = false;
}
if(ok) ans = min(ans, sz);
if(ans == LLONG_MAX){cout << "impossible"; return 0;}
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
14 ms |
4956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
34 ms |
8300 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |