#include <bits/stdc++.h>
#define int long long int
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define pi pair<int, int>
#define INF 1e13
using namespace std;
int N, K, ans = INF;
string S;
vector<int> js, os, is, jd, od, id;
int jdget(int idx) {
if (idx < 0) return 0;
return jd[idx];
}
int odget(int idx) {
if (idx < 0) return 0;
return od[idx];
}
int idget(int idx) {
if (idx < 0) return 0;
return id[idx];
}
int32_t main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> N >> K >> S;
jd.pb(0); od.pb(0); id.pb(0);
for (int i = 0; i < N; i++) {
if (S[i] == 'J') js.pb(i);
if (S[i] == 'O') os.pb(i);
if (S[i] == 'I') is.pb(i);
if (S[i] == 'J' and js.size()) jd.pb(jd.back() + i - js.back());
if (S[i] == 'O' and os.size()) od.pb(od.back() + i - os.back());
if (S[i] == 'I' and is.size()) id.pb(id.back() + i - is.back());
}
if (js.size() < K or os.size() < K or is.size() < K) {
cout << -1 << "\n";
return 0;
}
for (int i = 0; i <= js.size() - K; i++) {
int total = 0;
int curI = js[i];
total += jdget(i + K - 1) - jdget(i - 1) + 1 - K;
curI = js[i + K - 1];
// for (int j = 0; j < K - 1; j++) {
// total += js[i + j + 1] - js[i + j] - 1;
// curI = js[i + j + 1];
// }
// cout << total << endl;
int off = upper_bound(os.begin(), os.end(), curI) - os.begin();
if (off >= os.size() or os.size() - off < K) continue;
total += os[off] - curI - 1;
curI = os[off];
total += odget(off + K - 1) - odget(off - 1) + 1 - K;
curI = os[off + K - 1];
// for (int j = 0; j < K - 1; j++) {
// total += os[off + j + 1] - os[off + j] - 1;
// curI = os[off + j + 1];
// }
int off2 = upper_bound(is.begin(), is.end(), curI) - is.begin();
if (off2 >= is.size() or is.size() - off2 < K) continue;
total += is[off2] - curI - 1;
curI = is[off2];
total += idget(off2 + K - 1) - idget(off2 - 1) + 1 - K;
curI = is[off2 + K - 1];
// for (int j = 0; j < K - 1; j++) {
// total += is[off2 + j + 1] - is[off2 + j] - 1;
// curI = is[off2 + j + 1];
// }
ans = min(ans, total);
}
cout << (ans == INF ? -1 : ans) << "\n";
return 0;
}
Compilation message
ho_t2.cpp: In function 'int32_t main()':
ho_t2.cpp:49:16: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
49 | if (js.size() < K or os.size() < K or is.size() < K) {
| ~~~~~~~~~~^~~
ho_t2.cpp:49:33: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
49 | if (js.size() < K or os.size() < K or is.size() < K) {
| ~~~~~~~~~~^~~
ho_t2.cpp:49:50: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
49 | if (js.size() < K or os.size() < K or is.size() < K) {
| ~~~~~~~~~~^~~
ho_t2.cpp:54:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'long long unsigned int' [-Wsign-compare]
54 | for (int i = 0; i <= js.size() - K; i++) {
| ~~^~~~~~~~~~~~~~~~
ho_t2.cpp:69:11: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | if (off >= os.size() or os.size() - off < K) continue;
| ~~~~^~~~~~~~~~~~
ho_t2.cpp:69:43: warning: comparison of integer expressions of different signedness: 'long long unsigned int' and 'long long int' [-Wsign-compare]
69 | if (off >= os.size() or os.size() - off < K) continue;
| ~~~~~~~~~~~~~~~~^~~
ho_t2.cpp:84:12: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | if (off2 >= is.size() or is.size() - off2 < K) continue;
| ~~~~~^~~~~~~~~~~~
ho_t2.cpp:84:45: warning: comparison of integer expressions of different signedness: 'long long unsigned int' and 'long long int' [-Wsign-compare]
84 | if (off2 >= is.size() or is.size() - off2 < K) continue;
| ~~~~~~~~~~~~~~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |