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>
#define int long long
using namespace std;
int req[200005];
int freq[200005];
int a[200005];
signed main() {
ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
int n,k,rq; cin>>n>>k>>rq;
for(int i=0; i<n; i++) cin>>a[i];
for(int i=0; i<rq; i++) {
int x,c; cin>>x>>c;
req[x] = c;
}
int l = 1, r = n, ans = -1;
while(l<=r) {
int mid = (l+r)/2;
int cnt = 0;
for(int i=0; i<mid; i++) {
freq[a[i]]++;
if(req[a[i]] && freq[a[i]]==req[a[i]]) cnt++;
}
bool f = 0;
int ind = 0;
for(int i=mid; i<n; i++) {
if(cnt==rq) { f = 1; break; }
if(req[a[ind]] && freq[a[ind]]==req[a[ind]]) cnt--;
freq[a[ind]]--, freq[a[i]]++;
if(req[a[i]] && freq[a[i]]==req[a[i]]) cnt++;
ind++;
}
if(cnt==rq) f = 1;
if(f) { r = mid-1; ans = mid; }
else l = mid+1;
for(int i=0; i<=k; i++) freq[i]=0;
}
if(ans==-1) cout<<"impossible";
else 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... |