#include <iostream>
#include <climits>
#include <numeric>
#include <cassert>
#include <algorithm>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <vector>
#include <array>
#include <memory>
#define IN(a,b) (a.find(b) != a.end())
#define p(a,b) make_pair(a,b)
#define readVec(a) for (int __i = 0; __i<(int)a.size();__i++){cin>>a[__i];}
// jimjam
template<typename T>
void pMin(T &a, T b) {if (b<a){a=b;}}
template<typename T>
void pMax(T &a, T b) {if (b>a){a=b;}}
template<typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& c);
template<typename A, typename B>
std::ostream& operator<<(std::ostream& os, const std::pair<A,B>& c) {std::cout << "(" << c.first << ", " << c.second << ")";return os;}
template<typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& c) {
if (c.size() == 0) {os << "{}"; return os;}
os << "{" << c[0];
for (int i = 1; i < (int)c.size(); i++) {os <<", "<<c[i];}
os << "}";
return os;
}
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, k; cin >> n >> k;
string s; cin >> s;
vector<int> posJ, posO, posI;
for (int i = 0; i < n; i++) {
if (s[i]=='J') {
posJ.push_back(i);
} else if (s[i]=='O') {
posO.push_back(i);
} else if (s[i]=='I') {
posI.push_back(i);
} else {
assert(false);
}
}
int ans = INT_MAX;
for (int i = 0; i < posJ.size()-k+1; i++) {
int startPos = posJ[i], endPos = posJ[i+k-1];
int cnt = endPos-startPos-k+2-1;
int oStart = distance(posO.begin(), lower_bound(posO.begin(),posO.end(),endPos+1));
if (oStart+k-1 < posO.size()) {
int oEnd = posO[oStart+k-1];
cnt += (oEnd - posO[oStart] -k+2-1);
int iStart = distance(posI.begin(), lower_bound(posI.begin(),posI.end(), oEnd+1));
if (iStart+k-1 < posI.size()) {
int oEnd = posI[iStart+k-1];
cnt += (oEnd - posI[iStart]-k+2-1);
pMin(ans,cnt);
}
}
}
cout << (ans==INT_MAX?-1:ans) << endl;
return 0;
}
Compilation message
ho_t2.cpp: In function 'int main()':
ho_t2.cpp:58:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for (int i = 0; i < posJ.size()-k+1; i++) {
| ~~^~~~~~~~~~~~~~~~~
ho_t2.cpp:62:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | if (oStart+k-1 < posO.size()) {
| ~~~~~~~~~~~^~~~~~~~~~~~~
ho_t2.cpp:67:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | if (iStart+k-1 < posI.size()) {
| ~~~~~~~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |