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;
int ar[200005];
int mn[200005];
int temp[200005];
int n,k,r;
bool check(int m){
int cnt=0;
for(int i=0;i<k;i++)temp[i]=0;
for(int i=0;i<k;i++)if(mn[i]==0)cnt++;
for(int i=1;i<=m;i++){
temp[ar[i]]++;
if(temp[ar[i]]==mn[ar[i]])cnt++;
}
if(cnt==k)return true;
//cerr<<m<<" "<<cnt<<"\n";
for(int i=m+1;i<=n;i++){
temp[ar[i]]++;
if(temp[ar[i]]==mn[ar[i]])cnt++;
temp[ar[i-m]]--;
if(temp[ar[i-m]]==mn[ar[i-m]]-1)cnt--;
if(cnt==k)return true;
}
return false;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>k>>r;
for(int i=1;i<=n;i++)cin>>ar[i];
for(int i=0;i<r;i++){
int b;cin>>b;
cin>>mn[b];
}
int st=1,en=n,ans=-1;
while(st<=en){
int m=(st+en)/2;
if(check(m)){
en=m-1;
ans=m;
}else{
st=m+1;
}
}
if(ans==-1)cout<<"impossible";
else cout<<ans;
}
# | 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... |