#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define endl '\n'
#define INF 1e9+7
#define all(x) x.begin(),x.end()
using namespace std;
using namespace __gnu_pbds;
using ll=long long;
using pii=pair<int,int>;
using ppi=pair<int,pii>;
using oset=tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>;
template<typename T>
void _print(vector<T> x) {for(auto e:x) cerr<<e<<",";}
template<typename T>
void _print(T x) {cerr<<x;}
void dbg() {cerr<<endl;}
template<typename Head,typename... Tail>
void dbg(Head H,Tail... T) {
_print(H);
if(sizeof...(T)) cerr<<",";
else cerr<<"\"]";
dbg(T...);
}
#define debug(...) cerr<<"["<<#__VA_ARGS__<<"]:[\"",dbg(__VA_ARGS__)
int n,k;
string s;
int main () {
ios::sync_with_stdio(false); cin.tie(0);
cin>>n>>k>>s;
vector<int> J,O,I;
for(int i=0;i<n;i++) {
if(s[i]=='J') J.push_back(i);
else if(s[i]=='O') O.push_back(i);
else I.push_back(i);
}
int j_len=J.size(),o_len=O.size(),i_len=I.size();
int ans=INF;
for(int i=0;i<o_len-k+1;i++) {
int total=O[i+k-1]-O[i]-k+1;
int r=lower_bound(all(J),O[i])-begin(J)-1;
//debug(i,r);
if(r<k-1 or r<0) continue;
total+=J[r]-J[r-k+1]-k+1;
int l=lower_bound(all(I),O[i+k-1])-begin(I);
//debug(i,l);
if(l+k>i_len) continue;
total+=I[l+k-1]-I[l]-k+1;
ans=min(ans,total);
}
if(ans==INF) cout<<"-1"<<endl;
else cout<<ans<<endl;
}
Compilation message
ho_t2.cpp: In function 'int main()':
ho_t2.cpp:41:9: warning: unused variable 'j_len' [-Wunused-variable]
41 | int j_len=J.size(),o_len=O.size(),i_len=I.size();
| ^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
0 ms |
364 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
0 ms |
364 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
0 ms |
364 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |