This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 = -1;
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 && J[cntJ[j]+K-2] < i){
if(DJ[j] != -1){
if(cur == -1){
cur = DJ[j]-J[cntJ[j]+K-2];
}
else{
cur = min(cur,DJ[j]-J[cntJ[j]+K-2]);
}
}
++j;
}
if(cur != -1){
DO[i] = O[cntO[i]+K-2]-i+1-(cntO[O[cntO[i]+K-2]]-cntO[i-1])+cur+i-1;
}
else{
DO[i] = -1;
}
}
}
j = 0, cur = -1;
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 && O[cntO[j]+K-2] < i){
if(DO[j] != -1){
if(cur == -1){
cur = DO[j]-O[cntO[j]+K-2];
}
else{
cur = min(cur,DO[j]-O[cntO[j]+K-2]);
}
}
++j;
}
if(cur != -1){
DI[i] = I[cntI[i]+K-2]-i+1-(cntI[I[cntI[i]+K-2]]-cntI[i-1])+cur+i-1;
}
else{
DI[i] = -1;
}
if(ans == -1){
ans = DI[i];
}
else{
ans = min(ans,DI[i]);
}
}
}
cout << ans << "\n";
return 0;
}
Compilation message (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |