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 fast ios_base::sync_with_stdio(false); cin.tie(NULL);
#define int long long
#define inf ((int)1e18)
#define N 200005
using namespace std;
int32_t main(){
fast
int n, k, it=0;
string s;
cin>>n>>k>>s;
int jc=0, oc=0, ic=0;
for(; it<n; it++){
if(s[it]=='J'){
jc++;
if(jc == k)break;
}
}
for(; it<n; it++){
if(s[it]=='O'){
oc++;
if(oc == k)break;
}
}
for(; it<n; it++){
if(s[it]=='I'){
ic++;
if(ic == k)break;
}
}
if(ic != k){
cout<<-1<<"\n";
return 0;
}
vector <int> js, os, is;
for(int i=0; i < n; i++){
if(s[i] == 'J'){
js.push_back(i);
}
if(s[i] == 'O'){
os.push_back(i);
}
if(s[i] == 'I'){
is.push_back(i);
}
}
int jl=0, jr=k-1, ol=0, orr=0, il=0, ir=0;
int ans=inf;
while(js[jr] > os[ol]){
ol++;
}
orr=ol+k-1;
while(os[orr] > is[il]){
il++;
}
ir=il+k-1;
os.push_back(inf);
is.push_back(inf);
while(jr < js.size() and orr < os.size() and ir < is.size()){
ans = min(ans, is[ir]-k*3-js[jl]+1);
//cout<<ol<<" ";
jr++;
jl++;
while(js[jr] > os[ol]){
ol++;
}
orr=ol+k-1;
while(os[orr] > is[il]){
il++;
}
ir=il+k-1;
}
cout<<ans<<"\n";
}
Compilation message (stderr)
ho_t2.cpp: In function 'int32_t main()':
ho_t2.cpp:60:11: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | while(jr < js.size() and orr < os.size() and ir < is.size()){
| ~~~^~~~~~~~~~~
ho_t2.cpp:60:31: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | while(jr < js.size() and orr < os.size() and ir < is.size()){
| ~~~~^~~~~~~~~~~
ho_t2.cpp:60:50: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | while(jr < js.size() and orr < os.size() and ir < is.size()){
| ~~~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |