#include <bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
const int N = 3e5;
const int INF = 1e9;
string s;
int n, l;
vector<int> pos[3];
int nxt[3][N], cnt[3];
int get(int i, int res = 0) {
int nxt_i = nxt[0][i + l];
if (nxt_i == 0) return INF;
res += nxt_i - pos[0][i] + 1 - l;
int j = upper_bound(pos[1].begin(), pos[1].end(), nxt_i) - pos[1].begin();
if (j == pos[1].size()) return INF;
res += pos[1][j] - nxt_i - 1;
int nxt_j = nxt[1][j + l];
if (nxt_j == 0) return INF;
res += nxt_j - pos[1][j] + 1 - l;
int k = upper_bound(pos[2].begin(), pos[2].end(), nxt_j) - pos[2].begin();
if (k == pos[2].size()) return INF;
res += pos[2][k] - nxt_j - 1;
int nxt_k = nxt[2][k + l];
if (nxt_k == 0) return INF;
res += nxt_k - pos[2][k] + 1 - l;
return res;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//freopen(".in", "r", stdin);
//freopen(".out", "w", stdout);
cin >> n >> l;
cin >> s;
for (int i = 0; i < n; i++) {
char c = s[i];
if (c == 'J') {
pos[0].pb(i);
nxt[0][++cnt[0]] = i;
}
else if (c == 'O') {
pos[1].pb(i);
nxt[1][++cnt[1]] = i;
}
else {
pos[2].pb(i);
nxt[2][++cnt[2]] = i;
}
}
int ans = INF;
for (int i = 0; i < pos[0].size(); i++) {
ans = min(ans, get(i));
}
cout << (ans == INF ? -1 : ans);
return 0;
}
Compilation message
ho_t2.cpp: In function 'int get(int, int)':
ho_t2.cpp:22:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | if (j == pos[1].size()) return INF;
| ~~^~~~~~~~~~~~~~~~
ho_t2.cpp:28:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | if (k == pos[2].size()) return INF;
| ~~^~~~~~~~~~~~~~~~
ho_t2.cpp: In function 'int main()':
ho_t2.cpp:59:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for (int i = 0; i < pos[0].size(); i++) {
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |