#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5, INF = 0x3f3f3f3f;
char v[maxn];
vector<int> vJ, vO, vI;
int cost[maxn][3];
int main(){
int n, k; cin >> n >> k;
for(int i = 0; i < n; i++){
cin >> v[i];
if(v[i]=='J') vJ.push_back(i);
if(v[i]=='O') vO.push_back(i);
if(v[i]=='I') vI.push_back(i);
}
memset(cost, INF, sizeof cost);
int beginO=INF, beginI=INF;
for(int i = 0, cnt=0; i < n; i++){
if(cnt < vJ.size() && i==vJ[cnt]) cnt++;
if(cnt==k && beginO==INF) beginO = i+1;
if(cnt>=k)
cost[i+1][0] = i - vJ[cnt-k] - (k-1);
else cost[i+1][0] = INF;
}
int invalidO=0;
for(; invalidO < vO.size(); invalidO++) if(vO[invalidO]>=beginO) break;
for(int i = beginO, cnt=0; i < n; i++){
if(cnt+invalidO < vO.size() && i==vO[cnt+invalidO]) cnt++;
if(cnt==k && beginI==INF) beginI = i+1;
if(cnt>=k)
cost[i+1][1] = cost[vO[cnt-k+invalidO]][0] + i - vO[cnt-k+invalidO] - (k-1);
else cost[i+1][1] = INF;
}
int invalidI=0;
for(; invalidI < vI.size(); invalidI++) if(vI[invalidI]>=beginI) break;
for(int i = beginI, cnt=0; i < n; i++){
if(cnt+invalidI < vI.size() && i==vI[cnt+invalidI]) cnt++;
if(cnt>=k)
cost[i+1][2] = cost[vI[cnt-k+invalidI]][1] + i - vI[cnt-k+invalidI] - (k-1);
else cost[i+1][2] = INF;
}
int ans = INF;
for(int i = beginI; i <= n; i++) ans = min(ans, cost[i][2]);
cout << (ans==INF ? -1 : ans) << '\n';
}
Compilation message
ho_t2.cpp: In function 'int main()':
ho_t2.cpp:21:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | if(cnt < vJ.size() && i==vJ[cnt]) cnt++;
| ~~~~^~~~~~~~~~~
ho_t2.cpp:29:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | for(; invalidO < vO.size(); invalidO++) if(vO[invalidO]>=beginO) break;
| ~~~~~~~~~^~~~~~~~~~~
ho_t2.cpp:31:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | if(cnt+invalidO < vO.size() && i==vO[cnt+invalidO]) cnt++;
| ~~~~~~~~~~~~~^~~~~~~~~~~
ho_t2.cpp:39:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(; invalidI < vI.size(); invalidI++) if(vI[invalidI]>=beginI) break;
| ~~~~~~~~~^~~~~~~~~~~
ho_t2.cpp:41:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | if(cnt+invalidI < vI.size() && i==vI[cnt+invalidI]) cnt++;
| ~~~~~~~~~~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2612 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
1 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
1 ms |
2620 KB |
Output is correct |
12 |
Correct |
1 ms |
2644 KB |
Output is correct |
13 |
Correct |
1 ms |
2644 KB |
Output is correct |
14 |
Correct |
1 ms |
2616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2612 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
1 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
1 ms |
2620 KB |
Output is correct |
12 |
Correct |
1 ms |
2644 KB |
Output is correct |
13 |
Correct |
1 ms |
2644 KB |
Output is correct |
14 |
Correct |
1 ms |
2616 KB |
Output is correct |
15 |
Correct |
1 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2644 KB |
Output is correct |
20 |
Correct |
2 ms |
2612 KB |
Output is correct |
21 |
Correct |
2 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
1 ms |
2644 KB |
Output is correct |
24 |
Correct |
2 ms |
2644 KB |
Output is correct |
25 |
Correct |
2 ms |
2644 KB |
Output is correct |
26 |
Correct |
2 ms |
2612 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
1 ms |
2644 KB |
Output is correct |
29 |
Correct |
2 ms |
2644 KB |
Output is correct |
30 |
Correct |
2 ms |
2644 KB |
Output is correct |
31 |
Correct |
2 ms |
2644 KB |
Output is correct |
32 |
Correct |
2 ms |
2644 KB |
Output is correct |
33 |
Correct |
2 ms |
2644 KB |
Output is correct |
34 |
Correct |
2 ms |
2608 KB |
Output is correct |
35 |
Correct |
2 ms |
2616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2612 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
1 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
1 ms |
2620 KB |
Output is correct |
12 |
Correct |
1 ms |
2644 KB |
Output is correct |
13 |
Correct |
1 ms |
2644 KB |
Output is correct |
14 |
Correct |
1 ms |
2616 KB |
Output is correct |
15 |
Correct |
1 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2644 KB |
Output is correct |
20 |
Correct |
2 ms |
2612 KB |
Output is correct |
21 |
Correct |
2 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
1 ms |
2644 KB |
Output is correct |
24 |
Correct |
2 ms |
2644 KB |
Output is correct |
25 |
Correct |
2 ms |
2644 KB |
Output is correct |
26 |
Correct |
2 ms |
2612 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
1 ms |
2644 KB |
Output is correct |
29 |
Correct |
2 ms |
2644 KB |
Output is correct |
30 |
Correct |
2 ms |
2644 KB |
Output is correct |
31 |
Correct |
2 ms |
2644 KB |
Output is correct |
32 |
Correct |
2 ms |
2644 KB |
Output is correct |
33 |
Correct |
2 ms |
2644 KB |
Output is correct |
34 |
Correct |
2 ms |
2608 KB |
Output is correct |
35 |
Correct |
2 ms |
2616 KB |
Output is correct |
36 |
Correct |
17 ms |
3820 KB |
Output is correct |
37 |
Correct |
19 ms |
3956 KB |
Output is correct |
38 |
Correct |
19 ms |
4032 KB |
Output is correct |
39 |
Correct |
18 ms |
4064 KB |
Output is correct |
40 |
Correct |
17 ms |
3900 KB |
Output is correct |
41 |
Correct |
19 ms |
3912 KB |
Output is correct |
42 |
Correct |
19 ms |
3968 KB |
Output is correct |
43 |
Correct |
11 ms |
3412 KB |
Output is correct |
44 |
Correct |
13 ms |
3632 KB |
Output is correct |
45 |
Correct |
17 ms |
4008 KB |
Output is correct |
46 |
Correct |
18 ms |
3908 KB |
Output is correct |
47 |
Correct |
17 ms |
3856 KB |
Output is correct |
48 |
Correct |
17 ms |
4076 KB |
Output is correct |
49 |
Correct |
12 ms |
3588 KB |
Output is correct |
50 |
Correct |
17 ms |
3916 KB |
Output is correct |
51 |
Correct |
17 ms |
3912 KB |
Output is correct |
52 |
Correct |
15 ms |
3904 KB |
Output is correct |
53 |
Correct |
16 ms |
3960 KB |
Output is correct |
54 |
Correct |
15 ms |
3924 KB |
Output is correct |
55 |
Correct |
14 ms |
3876 KB |
Output is correct |
56 |
Correct |
14 ms |
3904 KB |
Output is correct |