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 all(a) a.begin(),a.end()
#define pb push_back
#define vt vector
#define endl '\n'
typedef long long ll;
const ll mod=1e9+7;
const ll inf=1e9+7;
const int N=5e3+4;
int n,k,r;
int a[N];
map<int,int>mp,cur;
set<int>st[N];
multiset<int>mst;
void solve(){
cin>>n>>k>>r;
for(int i=1; i<=n; ++i){
cin>>a[i];
}
ll all=0;
for(int i=1; i<=r; ++i){
int tp,cn;
cin>>tp>>cn;
mp[tp]=cn;
for(int x=0; x<cn; ++x) {
mst.insert(tp);
++all;
// if(all>n){
// cout<<"impossible\n";
// return;
//}
}
}
int l=0,r=0;
int ans=inf;
while(1){
if(l==r && r==n) break;
//cout<<l<<' '<<r<<endl;
//cout<<mst.size()<<endl;
while(r+1<=n && mst.size()){
++r;
auto pos=mst.find(a[r]);
cur[a[r]]++;
//cout<<a[r]<<" "<<cur[a[r]]<<endl;
if(pos!=mst.end()) mst.erase(pos);
}
if(r==n && mst.size()) {
break;
}
ans=min(ans,r-l);
while(l+1<=r){
while(l+1<=r && mp[a[l+1]]==0) ++l;
ans=min(ans,r-l);
if(l+1>r) break;
++l;
cur[a[l]]--;
//cout<<a[l]<<' '<<cur[a[l]]<<endl;
// return;
if(cur[a[l]]<mp[a[l]]) {
mst.insert(a[l]);
break;
}
ans=min(ans,r-l);
}
}
if(ans==inf){
cout<<"impossible\n";
return;
}
cout<<ans<<endl;
}
int main(){
int tt=1;
//cin>>tt;
while(tt--) {
solve();
}
}
# | 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... |