This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;}
if(sz == n){
for(int i = 0; i < n; i++){
}
}
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);
if(!nums.empty() && r < n) r++;
}
if(r == n) 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++;
}
while(l < n-sz && nums.empty()){
ans = min(ans, r-l+1);
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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |