#include <bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
const int N = 5e5;
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 == -1) 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 == -1) 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 == -1) 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);
for (int i = 0; i < 3; i++) fill(nxt[i], nxt[i] + N, -1);
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:60:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for (int i = 0; i < pos[0].size(); i++) {
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6252 KB |
Output is correct |
2 |
Correct |
4 ms |
6252 KB |
Output is correct |
3 |
Correct |
4 ms |
6252 KB |
Output is correct |
4 |
Correct |
6 ms |
6252 KB |
Output is correct |
5 |
Correct |
6 ms |
6252 KB |
Output is correct |
6 |
Correct |
4 ms |
6252 KB |
Output is correct |
7 |
Correct |
4 ms |
6252 KB |
Output is correct |
8 |
Correct |
4 ms |
6252 KB |
Output is correct |
9 |
Correct |
4 ms |
6252 KB |
Output is correct |
10 |
Correct |
4 ms |
6252 KB |
Output is correct |
11 |
Correct |
4 ms |
6252 KB |
Output is correct |
12 |
Correct |
4 ms |
6252 KB |
Output is correct |
13 |
Correct |
4 ms |
6272 KB |
Output is correct |
14 |
Correct |
4 ms |
6252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6252 KB |
Output is correct |
2 |
Correct |
4 ms |
6252 KB |
Output is correct |
3 |
Correct |
4 ms |
6252 KB |
Output is correct |
4 |
Correct |
6 ms |
6252 KB |
Output is correct |
5 |
Correct |
6 ms |
6252 KB |
Output is correct |
6 |
Correct |
4 ms |
6252 KB |
Output is correct |
7 |
Correct |
4 ms |
6252 KB |
Output is correct |
8 |
Correct |
4 ms |
6252 KB |
Output is correct |
9 |
Correct |
4 ms |
6252 KB |
Output is correct |
10 |
Correct |
4 ms |
6252 KB |
Output is correct |
11 |
Correct |
4 ms |
6252 KB |
Output is correct |
12 |
Correct |
4 ms |
6252 KB |
Output is correct |
13 |
Correct |
4 ms |
6272 KB |
Output is correct |
14 |
Correct |
4 ms |
6252 KB |
Output is correct |
15 |
Correct |
4 ms |
6272 KB |
Output is correct |
16 |
Correct |
4 ms |
6252 KB |
Output is correct |
17 |
Correct |
4 ms |
6252 KB |
Output is correct |
18 |
Correct |
4 ms |
6252 KB |
Output is correct |
19 |
Correct |
5 ms |
6252 KB |
Output is correct |
20 |
Correct |
4 ms |
6252 KB |
Output is correct |
21 |
Correct |
4 ms |
6252 KB |
Output is correct |
22 |
Correct |
4 ms |
6252 KB |
Output is correct |
23 |
Correct |
4 ms |
6252 KB |
Output is correct |
24 |
Correct |
4 ms |
6252 KB |
Output is correct |
25 |
Correct |
4 ms |
6272 KB |
Output is correct |
26 |
Correct |
5 ms |
6252 KB |
Output is correct |
27 |
Correct |
4 ms |
6252 KB |
Output is correct |
28 |
Correct |
4 ms |
6252 KB |
Output is correct |
29 |
Correct |
4 ms |
6252 KB |
Output is correct |
30 |
Correct |
4 ms |
6252 KB |
Output is correct |
31 |
Correct |
4 ms |
6252 KB |
Output is correct |
32 |
Correct |
4 ms |
6272 KB |
Output is correct |
33 |
Correct |
4 ms |
6252 KB |
Output is correct |
34 |
Correct |
4 ms |
6252 KB |
Output is correct |
35 |
Correct |
4 ms |
6252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6252 KB |
Output is correct |
2 |
Correct |
4 ms |
6252 KB |
Output is correct |
3 |
Correct |
4 ms |
6252 KB |
Output is correct |
4 |
Correct |
6 ms |
6252 KB |
Output is correct |
5 |
Correct |
6 ms |
6252 KB |
Output is correct |
6 |
Correct |
4 ms |
6252 KB |
Output is correct |
7 |
Correct |
4 ms |
6252 KB |
Output is correct |
8 |
Correct |
4 ms |
6252 KB |
Output is correct |
9 |
Correct |
4 ms |
6252 KB |
Output is correct |
10 |
Correct |
4 ms |
6252 KB |
Output is correct |
11 |
Correct |
4 ms |
6252 KB |
Output is correct |
12 |
Correct |
4 ms |
6252 KB |
Output is correct |
13 |
Correct |
4 ms |
6272 KB |
Output is correct |
14 |
Correct |
4 ms |
6252 KB |
Output is correct |
15 |
Correct |
4 ms |
6272 KB |
Output is correct |
16 |
Correct |
4 ms |
6252 KB |
Output is correct |
17 |
Correct |
4 ms |
6252 KB |
Output is correct |
18 |
Correct |
4 ms |
6252 KB |
Output is correct |
19 |
Correct |
5 ms |
6252 KB |
Output is correct |
20 |
Correct |
4 ms |
6252 KB |
Output is correct |
21 |
Correct |
4 ms |
6252 KB |
Output is correct |
22 |
Correct |
4 ms |
6252 KB |
Output is correct |
23 |
Correct |
4 ms |
6252 KB |
Output is correct |
24 |
Correct |
4 ms |
6252 KB |
Output is correct |
25 |
Correct |
4 ms |
6272 KB |
Output is correct |
26 |
Correct |
5 ms |
6252 KB |
Output is correct |
27 |
Correct |
4 ms |
6252 KB |
Output is correct |
28 |
Correct |
4 ms |
6252 KB |
Output is correct |
29 |
Correct |
4 ms |
6252 KB |
Output is correct |
30 |
Correct |
4 ms |
6252 KB |
Output is correct |
31 |
Correct |
4 ms |
6252 KB |
Output is correct |
32 |
Correct |
4 ms |
6272 KB |
Output is correct |
33 |
Correct |
4 ms |
6252 KB |
Output is correct |
34 |
Correct |
4 ms |
6252 KB |
Output is correct |
35 |
Correct |
4 ms |
6252 KB |
Output is correct |
36 |
Correct |
16 ms |
7528 KB |
Output is correct |
37 |
Correct |
16 ms |
7736 KB |
Output is correct |
38 |
Correct |
15 ms |
7876 KB |
Output is correct |
39 |
Correct |
16 ms |
7920 KB |
Output is correct |
40 |
Correct |
13 ms |
7920 KB |
Output is correct |
41 |
Correct |
14 ms |
7860 KB |
Output is correct |
42 |
Correct |
15 ms |
7736 KB |
Output is correct |
43 |
Correct |
9 ms |
7180 KB |
Output is correct |
44 |
Correct |
10 ms |
7408 KB |
Output is correct |
45 |
Correct |
12 ms |
7920 KB |
Output is correct |
46 |
Correct |
12 ms |
7912 KB |
Output is correct |
47 |
Correct |
12 ms |
7920 KB |
Output is correct |
48 |
Correct |
12 ms |
7920 KB |
Output is correct |
49 |
Correct |
9 ms |
7248 KB |
Output is correct |
50 |
Correct |
12 ms |
7908 KB |
Output is correct |
51 |
Correct |
12 ms |
7920 KB |
Output is correct |
52 |
Correct |
9 ms |
7408 KB |
Output is correct |
53 |
Correct |
10 ms |
7652 KB |
Output is correct |
54 |
Correct |
11 ms |
7924 KB |
Output is correct |
55 |
Correct |
8 ms |
7924 KB |
Output is correct |
56 |
Correct |
7 ms |
7944 KB |
Output is correct |