#include<bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(),x.end()
int INF=1e18;
int mod=998244353;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;cin>>n;
int k;cin>>k;
int q;cin>>q;
vector<int> v(n);
for(int i=0;i<n;i++){
cin>>v[i];
}
vector<int> need(k);
for(int i=0;i<q;i++){
int a,b;cin>>a>>b;
need[a]=max(need[a],b);
}
int ans=INF;
int l=0,r=0;
vector<int> cnt(k);
int sat=0;
for(int i=0;i<k;i++){
if(need[i]==0){
sat++;
}
}
function<void(void)> add=[&](){
cnt[v[r]]++;
if(cnt[v[r]]==need[v[r]]){
sat++;
}
r++;
};
function<void(void)> del=[&](){
cnt[v[l]]--;
if(cnt[v[l]]==need[v[l]]-1){
sat--;
}
l++;
};
while(1){
if(r==n){
cout<<"impossible"<<endl;
return 0;
}
add();
if(sat==k){
break;
}
}
ans=r-l;
for(int i=1;i<n;i++){
del();
while(sat<k&&r<n){
add();
}
if(sat==k){
ans=min(ans,r-l);
}
}
cout<<ans<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
380 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
368 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
2292 KB |
Output is correct |
2 |
Correct |
16 ms |
2268 KB |
Output is correct |
3 |
Correct |
16 ms |
2384 KB |
Output is correct |
4 |
Correct |
17 ms |
2260 KB |
Output is correct |
5 |
Correct |
25 ms |
4324 KB |
Output is correct |
6 |
Correct |
18 ms |
2272 KB |
Output is correct |
7 |
Correct |
12 ms |
2388 KB |
Output is correct |
8 |
Correct |
23 ms |
6236 KB |
Output is correct |
9 |
Correct |
17 ms |
2908 KB |
Output is correct |
10 |
Correct |
14 ms |
2288 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
320 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
324 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
4808 KB |
Output is correct |
2 |
Correct |
27 ms |
4424 KB |
Output is correct |
3 |
Correct |
25 ms |
4056 KB |
Output is correct |
4 |
Correct |
14 ms |
2268 KB |
Output is correct |
5 |
Correct |
36 ms |
5600 KB |
Output is correct |
6 |
Correct |
38 ms |
6740 KB |
Output is correct |
7 |
Correct |
19 ms |
2888 KB |
Output is correct |
8 |
Correct |
21 ms |
3276 KB |
Output is correct |
9 |
Correct |
14 ms |
2272 KB |
Output is correct |
10 |
Correct |
15 ms |
2252 KB |
Output is correct |
11 |
Correct |
16 ms |
2268 KB |
Output is correct |
12 |
Correct |
15 ms |
2316 KB |
Output is correct |
13 |
Correct |
19 ms |
4308 KB |
Output is correct |
14 |
Correct |
13 ms |
2292 KB |
Output is correct |
15 |
Correct |
13 ms |
2396 KB |
Output is correct |
16 |
Correct |
23 ms |
6272 KB |
Output is correct |
17 |
Correct |
17 ms |
2900 KB |
Output is correct |
18 |
Correct |
14 ms |
2276 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
380 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
324 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
328 KB |
Output is correct |
30 |
Correct |
1 ms |
320 KB |
Output is correct |
31 |
Correct |
1 ms |
328 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |