#include <bits/stdc++.h>
using namespace std;
int n, k, J[200001], O[200001], I[200001];
string wzor, word;
int min_dlug(int ind, int ind_pomoc){
int dlugosc = 0, druga_dlugosc = 200001;
while(ind_pomoc < k * 3){
if(wzor[ind_pomoc] == 'J'){
dlugosc += J[ind] - ind - 1;
if(ind_pomoc == k){
druga_dlugosc = min_dlug(ind, 1);
}
}else if(wzor[ind_pomoc] == 'O'){
dlugosc += O[ind] - ind - 1;
}else if(wzor[ind_pomoc] == 'I'){
dlugosc += I[ind] - ind - 1;
}
if(ind_pomoc + 1 == k * 3){
ind = 200001;
}else if(ind_pomoc + 1 > 2 * k){
ind = I[ind];
}else if(ind_pomoc + 1 > k){
ind = O[ind];
}else{
ind = J[ind];
}
if(ind == 0){
return -1;
}
ind_pomoc++;
}
if(druga_dlugosc == -1){
druga_dlugosc = 200001;
}
return min(dlugosc, druga_dlugosc);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
cin >> word;
for(int i = 0; i < k; i++){
wzor += "J";
}
for(int i = 0; i < k; i++){
wzor += "O";
}
for(int i = 0; i < k; i++){
wzor += "I";
}
int index_1 = 200001;
for(int i = n - 1; i >= 0; i--){
J[i - 1] = J[i];
O[i - 1] = O[i];
I[i - 1] = I[i];
if(word[i] == 'J'){
index_1 = min(index_1, i);
J[i - 1] = i;
}
if(word[i] == 'O'){
O[i - 1] = i;
}
if(word[i] == 'I'){
I[i - 1] = i;
}
}
if(word[0] == 'J'){
index_1 = 0;
}
cout << min_dlug(index_1, 1);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
504 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
504 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
504 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |