/**
* Author: Amirrrr
* Created: 06.06.2023
* Why am I so stupid? :c
* Slishkom slab
**/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define all(x) x.begin(), x.end()
#define FF first
#define SS second
#define ent "\n"
#define int long long
const int N = 1e6 + 13;
const int MOD = 1e9 + 7;
const int INF = 1e18;
int n,k,pref[N],pref1[N],pref2[N];
string s;
void gogo (){
cin>>n>>k;
cin>>s;
s='+'+s;
int mn = INF;
for (int i = 1 ; i <= n ; i ++){
if (s[i] =='J')pref[i]++;
if(s[i]=='O')pref1[i]++;
if(s[i]=='I')pref2[i]++;
pref[i]+=pref[i-1],pref1[i]+=pref1[i-1],pref2[i]+=pref2[i-1];
}
for(int i = 1 ; i <= n ; i ++){
if(s[i]!='J') continue;
int l=i,r=n,ans=-1,ans1=-1,ans2=-1;
while(l<=r){
int mid=(l+r)>>1;
if(pref[mid] - pref[i-1]==k){
ans=mid;
r=mid-1;
}
else if (pref[mid] - pref[i-1] > k){
r = mid-1;
}
else l = mid + 1;
}
l=ans+1,r=n;
while(l<=r){
int mid=(l+r)>>1;
if(pref1[mid] - pref1[ans]==k){
ans1=mid;
r=mid-1;
}
else if (pref1[mid] - pref1[ans] > k){
r = mid-1;
}
else l = mid + 1;
}
l=ans1+1,r=n;
while(l<=r){
int mid=(l+r)>>1;
if(pref2[mid]-pref2[ans1]==k){
ans2=mid;
r=mid-1;
}
else if (pref2[mid]-pref2[ans1]>k){
r=mid-1;
}
else l=mid + 1;
}
if(ans2==-1 || ans==-1 || ans1==-1){
continue;
}
mn = min(mn, ans2 - i + 1 - 3 * k);
}
if(mn!=INF)cout<< mn;
else cout<<-1;
}
signed main (/*Amir Mirmanov*/){
ios_base::sync_with_stdio(0), cin.tie(0);
int Q = 1,test = 1;
//freopen("herding.in","r",stdin);
//freopen("herding.out","w",stdout);
//cin >> Q;
while (Q --){
gogo ();
}
return 0;
}
Compilation message
ho_t2.cpp: In function 'int main()':
ho_t2.cpp:86:15: warning: unused variable 'test' [-Wunused-variable]
86 | int Q = 1,test = 1;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
328 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 |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
328 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 |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
12 ms |
5144 KB |
Output is correct |
37 |
Correct |
12 ms |
5536 KB |
Output is correct |
38 |
Correct |
12 ms |
5552 KB |
Output is correct |
39 |
Correct |
19 ms |
5404 KB |
Output is correct |
40 |
Correct |
15 ms |
5532 KB |
Output is correct |
41 |
Correct |
17 ms |
5540 KB |
Output is correct |
42 |
Correct |
17 ms |
5532 KB |
Output is correct |
43 |
Correct |
8 ms |
3668 KB |
Output is correct |
44 |
Correct |
11 ms |
4424 KB |
Output is correct |
45 |
Correct |
15 ms |
5508 KB |
Output is correct |
46 |
Correct |
14 ms |
5532 KB |
Output is correct |
47 |
Correct |
14 ms |
5580 KB |
Output is correct |
48 |
Correct |
14 ms |
5532 KB |
Output is correct |
49 |
Correct |
9 ms |
3752 KB |
Output is correct |
50 |
Correct |
14 ms |
5536 KB |
Output is correct |
51 |
Correct |
17 ms |
5684 KB |
Output is correct |
52 |
Correct |
15 ms |
5284 KB |
Output is correct |
53 |
Correct |
14 ms |
5556 KB |
Output is correct |
54 |
Correct |
11 ms |
5536 KB |
Output is correct |
55 |
Correct |
11 ms |
5532 KB |
Output is correct |
56 |
Correct |
11 ms |
5536 KB |
Output is correct |