#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]];
}
}
}
}
int j = 0,cur = INT_MAX;
for(int i = 0;i<N;++i){
if(cntJ[i] < K) continue;
if(cntO[i]+K-2 >= ocnt) continue;
if(s[i] == 'O'){
while(j < i && ((cntJ[j] == 0 && K == 1) || (J[cntJ[j]+K-2] < i))){
if(DJ[j] != -1){
cur = min(cur,DJ[j]-J[cntJ[j]+K-2]);
}
++j;
}
if(cur != INT_MAX){
if(K == 1){
DO[i] = cur;
}
else{
DO[i] = O[cntO[i]+K-2]-(cntO[O[cntO[i]+K-2]]-cntO[i-1])+cur;
}
}
else{
DO[i] = -1;
}
}
}
j = 0,cur = INT_MAX;
for(int i = 0;i<N;++i){
if(cntO[i] < K) continue;
if(cntI[i]+K-2 >= icnt) continue;
if(s[i] == 'I'){
while(j < i && ((cntO[j] == 0 && K == 1) || (O[cntO[j]+K-2] < i))){
if(DO[j] != -1){
cur = min(cur,DO[j]-O[cntO[j]+K-2]);
}
++j;
}
if(cur != INT_MAX){
if(K == 1){
DI[i] = cur;
}
else{
DI[i] = I[cntI[i]+K-2]-(cntI[I[cntI[i]+K-2]]-cntI[i-1])+cur;
}
}
else{
DI[i] = -1;
}
if(ans == -1){
ans = DI[i];
}
else{
ans = min(ans,DI[i]);
}
}
}
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){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |