#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int N,K; cin >> N >> K;
string s; cin >> s;
vector<int> J(N,-1),O(N,-1),I(N,-1); int jcnt = 0, ocnt = 0, icnt = 0;
vector<int> cntJ(N,0),cntO(N,0),cntI(N,0);
for(int i = 0;i<s.size();++i){
if(s[i] == 'J'){
J[jcnt] = i;
if(i > 0){
cntJ[i] = cntJ[i-1]+1;
cntO[i] = cntO[i-1]; cntI[i] = cntI[i-1];
}
else{
cntJ[i] = 1;
}
++jcnt;
}
else if(s[i] == 'O'){
O[ocnt] = i;
if(i > 0){
cntJ[i] = cntJ[i-1];
cntO[i] = cntO[i-1]+1; cntI[i] = cntI[i-1];
}
else{
cntO[i] = 1;
}
++ocnt;
}
else{
I[icnt] = i;
if(i > 0){
cntJ[i] = cntJ[i-1];
cntO[i] = cntO[i-1]; cntI[i] = cntI[i-1]+1;
}
else{
cntI[i] = 1;
}
++icnt;
}
}
vector<int> DJ(N,-1),DO(N,-1),DI(N,-1);
int ans = -1;
for(int i = 0;i<N;++i){
if(s[i] == 'J'){
if(cntJ[i]+K-2 < jcnt){
if(i != 0){
DJ[i] = J[cntJ[i]+K-2]-i+1-(cntJ[J[cntJ[i]+K-2]]-cntJ[i-1]);
}
else{
DJ[i] = J[cntJ[i]+K-2]-i+1-cntJ[J[cntJ[i]+K-2]];
}
}
}
else if(s[i] == 'O'){
if(cntJ[i] < K) continue;
if(cntO[i]+K-2 < ocnt){
DO[i] = O[cntO[i]+K-2]-i+1-(cntO[O[cntO[i]+K-2]]-cntO[i-1]);
int cur = -1;
for(int j = 0;j<i;++j){
if(DJ[j] != -1 && J[cntJ[j]+K-2] < i){
if(cur == -1){
cur = DJ[j]+i-J[cntJ[j]+K-2]-1;
}
else{
cur = min(cur,DJ[j]+i-J[cntJ[j]+K-2]-1);
}
}
}
if(cur != -1){
DO[i] += cur;
}
else{
DO[i] = -1;
}
}
}
else{
if(cntO[i] < K) continue;
if(cntI[i]+K-2 < icnt){
DI[i] = I[cntI[i]+K-2]-i+1-(cntI[I[cntI[i]+K-2]]-cntI[i-1]);
int cur = -1;
for(int j = 0;j<i;++j){
if(DO[j] != -1 && O[cntO[j]+K-2] < i){
if(cur == -1){
cur = DO[j]+i-O[cntO[j]+K-2]-1;
}
else{
cur = min(cur,DO[j]+i-O[cntO[j]+K-2]-1);
}
}
}
if(cur != -1){
DI[i] += cur;
if(ans == -1){
ans = DI[i];
}
else{
ans = min(ans,DI[i]);
}
}
else{
DI[i] = -1;
}
}
}
}
cout << ans << "\n";
return 0;
}
Compilation message
ho_t2.cpp: In function 'int main()':
ho_t2.cpp:11:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
11 | for(int i = 0;i<s.size();++i){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
456 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
452 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
456 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
452 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
2 ms |
344 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
404 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
3 ms |
348 KB |
Output is correct |
34 |
Correct |
2 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
456 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
452 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
2 ms |
344 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
404 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
3 ms |
348 KB |
Output is correct |
34 |
Correct |
2 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Execution timed out |
2039 ms |
7400 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |