#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define int long long
#define inf LLONG_MAX
#define pb push_back
#define lc 2*pos+1
#define rc 2*pos+2
#define halleluya ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
signed main(){
halleluya
int n,k,re;cin>>n>>k>>re;
int num[n];
map<int,int>req;
map<int,int>freq;
int tot=re;
map<int,int>ad;
for (int i=0;i<n;i++)cin>>num[i];
while (re--){
int x,y;cin>>x>>y;
ad[x]=1;
req[x]=y;
}
// for (int i=0;i<n;i++){
// for (auto x:jum) {
// if (0<i) freq[{i,x}]=freq[{i-1,x}];
// if (x==num[i]) freq[{i,x}]++;
// }
// }
int l=0,r=-1;
map<int,bool>vis;
int ben=0;
int ans=inf;
while(l<n){
while (ben<tot && r+1<n){
r++;
freq[num[r]]++;
// cout<<r<<" "<<num[r]<<" "<<freq[num[r]]<<" "<<req[num[r]] <<endl;
if (ad[num[r]]){
if (freq[num[r]]>=req[num[r]] && vis[num[r]]==0 ){
vis[num[r]]=1;
// cout<<"namben "<<num[r]<<" di "<<r<<endl;
ben++;
}
}
}
// cout<<l<<" "<<r<<endl;
if (ben>=tot) ans=min(ans,r-l+1);
freq[num[l]]--;
if (ad[num[l]]){
if (freq[num[l]]<req[num[l]] && vis[num[l]]){
vis[num[l]]=0;
ben--;
}
// cout<<ben<<endl;
}
l++;
}
if (ans==inf)cout<<"impossible";
else cout<<ans;
}
// impossible -> jumlah kurang atau emang gaada substringnya
// harus consecutive
// constraint r
// cuman perlu shortest
// sliding window?
// cuma save jumlah k
// n*k
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
716 KB |
Output is correct |
4 |
Correct |
2 ms |
860 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
2392 KB |
Output is correct |
2 |
Correct |
11 ms |
2396 KB |
Output is correct |
3 |
Correct |
16 ms |
2452 KB |
Output is correct |
4 |
Correct |
17 ms |
2396 KB |
Output is correct |
5 |
Correct |
149 ms |
13164 KB |
Output is correct |
6 |
Correct |
19 ms |
2392 KB |
Output is correct |
7 |
Correct |
28 ms |
2396 KB |
Output is correct |
8 |
Correct |
334 ms |
28088 KB |
Output is correct |
9 |
Correct |
108 ms |
3948 KB |
Output is correct |
10 |
Correct |
24 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
744 KB |
Output is correct |
14 |
Correct |
2 ms |
860 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
432 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
478 ms |
23740 KB |
Output is correct |
2 |
Correct |
388 ms |
18492 KB |
Output is correct |
3 |
Correct |
246 ms |
13132 KB |
Output is correct |
4 |
Correct |
12 ms |
2256 KB |
Output is correct |
5 |
Correct |
557 ms |
26608 KB |
Output is correct |
6 |
Correct |
731 ms |
39252 KB |
Output is correct |
7 |
Correct |
215 ms |
4836 KB |
Output is correct |
8 |
Correct |
244 ms |
7508 KB |
Output is correct |
9 |
Correct |
18 ms |
2260 KB |
Output is correct |
10 |
Correct |
15 ms |
2412 KB |
Output is correct |
11 |
Correct |
17 ms |
2388 KB |
Output is correct |
12 |
Correct |
17 ms |
2432 KB |
Output is correct |
13 |
Correct |
164 ms |
12924 KB |
Output is correct |
14 |
Correct |
24 ms |
2404 KB |
Output is correct |
15 |
Correct |
27 ms |
2516 KB |
Output is correct |
16 |
Correct |
269 ms |
28220 KB |
Output is correct |
17 |
Correct |
103 ms |
3920 KB |
Output is correct |
18 |
Correct |
25 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
604 KB |
Output is correct |
22 |
Correct |
2 ms |
1000 KB |
Output is correct |
23 |
Correct |
2 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
344 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
600 KB |
Output is correct |
31 |
Correct |
1 ms |
352 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |