This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
int n, k;
char A[200005];
vector<int> v1, v2, v3;
int in, in3;
void solve(){
cin >> n >> k;
for(int i=1;i<=n;i++){
cin >> A[i];
if(A[i] == 'J')v1.push_back(i);
else if(A[i] == 'O')v2.push_back(i);
else v3.push_back(i);
}
if((int)v1.size() < k || (int)v2.size() < k || (int)v3.size() < k){
cout << -1;
return;
}
int mn = 1e9;
for(int i=k-1;i<(int)v2.size();i++){
int fst = v2[i-k+1], lst = v2[i];
int cur = (lst - fst + 1) - k;
while(in < (int)v1.size() && v1[in] < fst)in++;
cur += (in > k - 1 ? (fst - v1[in-k]) - k : 1e9);
while(in3 < (int)v3.size() && v3[in3] < lst)in3++;
cur += (in3 + k - 1 < (int)v3.size() ? (v3[in3 + k - 1] - lst) - k : 1e9);
mn = min(mn, cur);
}
cout << (mn == (int)1e9 ? -1 : mn);
}
main(){
ios::sync_with_stdio(0);cin.tie(0);
int tc = 1;
//cin >> tc;
for(int tc1=1;tc1<=tc;tc1++){
// cout << "Case #" << tc1 << ": ";
solve();
}
}
Compilation message (stderr)
ho_t2.cpp:44:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
44 | main(){
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |