#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=5e5+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<<"i mpossible\n";
return;
}
}
}
int l=1,r=1;
int ans=inf;
while(1){
//cout<<l<<' '<<r<<endl;
//cout<<mst.size()<<endl;
while(r<=n && mst.size()){
auto pos=mst.find(a[r]);
cur[a[r]]++;
//cout<<a[r]<<" "<<cur[a[r]]<<endl;
if(pos!=mst.end()) mst.erase(pos);
++r;
}
if(r>n) break;
ans=min(ans,r-l+1);
while(l<r){
while(l<r && mp[a[l]]==0) ++l;
ans=min(ans,r-l);
cur[a[l]]--;
//cout<<a[l]<<' '<<cur[a[l]]<<endl;
// return;
if(cur[a[l]]<mp[a[l]]) {
mst.insert(a[l]);
++l;
break;
}
++l;
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();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
25176 KB |
Output is correct |
2 |
Correct |
5 ms |
25180 KB |
Output is correct |
3 |
Incorrect |
5 ms |
25180 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
25180 KB |
Output is correct |
2 |
Correct |
6 ms |
25180 KB |
Output is correct |
3 |
Correct |
6 ms |
25236 KB |
Output is correct |
4 |
Correct |
7 ms |
25436 KB |
Output is correct |
5 |
Correct |
6 ms |
25180 KB |
Output is correct |
6 |
Correct |
5 ms |
25180 KB |
Output is correct |
7 |
Correct |
5 ms |
25180 KB |
Output is correct |
8 |
Correct |
5 ms |
25080 KB |
Output is correct |
9 |
Incorrect |
5 ms |
25208 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
29436 KB |
Output is correct |
2 |
Correct |
44 ms |
26964 KB |
Output is correct |
3 |
Correct |
47 ms |
25176 KB |
Output is correct |
4 |
Correct |
46 ms |
27996 KB |
Output is correct |
5 |
Correct |
120 ms |
33164 KB |
Output is correct |
6 |
Incorrect |
72 ms |
34388 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
259 ms |
32728 KB |
Output is correct |
2 |
Correct |
201 ms |
31060 KB |
Output is correct |
3 |
Correct |
141 ms |
30800 KB |
Output is correct |
4 |
Correct |
44 ms |
25180 KB |
Output is correct |
5 |
Incorrect |
72 ms |
34404 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |