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>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define pii pair<int,int>
#define ff first
#define ss second
#define pb push_back
#define ins insert
#define mp make_pair
using namespace std;
void debug(){
cout << "\n";
}
template<class T, class ...U>
void debug(T a, U ...b){
cout << a << " "; debug(b...);
}
const int maxN = 200005;
int main(){
int n, k;
string s;
cin >> n >> k >> s;
deque<int> dqJ, dqO, dqI;
int prevJ[maxN], sufI[maxN];
loop(i,0,n){
prevJ[i] = -1;
sufI[i] = -1;
}
int ans = 2147483647;
// J
loop(i,0,n){
if(dqJ.size() < k){
if(s[i] == 'J'){
dqJ.pb(i);
}
}
else{
prevJ[i] = dqJ.front();
if(s[i] == 'J'){
dqJ.pop_front();
dqJ.pb(i);
}
}
}
// I
for(int i = n-1; i >= 0; i--){
if(dqI.size() < k){
if(s[i] == 'I'){
dqI.push_front(i);
}
}
else{
sufI[i] = dqI.back();
if(s[i] == 'I'){
dqI.pop_back();
dqI.push_front(i);
}
}
}
// O
loop(i,0,n){
if(dqO.size() < k){
if(s[i] == 'O'){
dqO.pb(i);
}
}
else{
if(s[i] == 'O'){
dqO.pop_front();
dqO.pb(i);
}
if(prevJ[dqO.front()] != -1 && sufI[dqO.back()] != -1){
ans = min(ans, sufI[dqO.back()] - prevJ[dqO.front()] + 1 - k * 3);
}
}
}
cout << ans << "\n";
}
Compilation message (stderr)
ho_t2.cpp: In function 'int main()':
ho_t2.cpp:34:23: warning: comparison of integer expressions of different signedness: 'std::deque<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
34 | if(dqJ.size() < k){
| ~~~~~~~~~~~^~~
ho_t2.cpp:49:23: warning: comparison of integer expressions of different signedness: 'std::deque<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
49 | if(dqI.size() < k){
| ~~~~~~~~~~~^~~
ho_t2.cpp:64:23: warning: comparison of integer expressions of different signedness: 'std::deque<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
64 | if(dqO.size() < k){
| ~~~~~~~~~~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |