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 ll long long
#define MAXN 200010
#define llinf 1000000000
using namespace std;
vector<ll> posj,poso,posi,mino,mini;
int main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
ll n,k;
string s;
cin >> n >> k >> s;
for (ll i=0;i<n;i++)
{
if (s[i]=='J')
posj.push_back(i);
else if (s[i]=='O')
poso.push_back(i);
else
posi.push_back(i);
}
mino.resize(poso.size()+1,llinf);
mini.resize(posi.size()+1,llinf);
for (ll i=posi.size()-1;i>=0;i--)
{
if (i+k-1>=posi.size())
continue;
if (i+1<posi.size())
mini[i]=min(mini[i+1]+posi[i+1]-posi[i],posi[i+k-1]-posi[i]-k+1);
else
mini[i]=posi[i+k-1]-posi[i]-k+1;
}
for (ll i=poso.size()-1;i>=0;i--)
{
if (i+k-1>=poso.size())
continue;
auto it=lower_bound(posi.begin(),posi.end(),poso[i+k-1]);
if (it==posi.end())
continue;
if (i+1<poso.size())
mino[i]=min(mino[i+1]+poso[i+1]-poso[i],mini[it-posi.begin()]+posi[it-posi.begin()]-poso[i]-k);
else
mino[i]=mini[it-posi.begin()]+posi[it-posi.begin()]-poso[i]-k;
}
ll ans=llinf;
for (ll i=0;i+k-1<posj.size();i++)
{
auto it=lower_bound(poso.begin(),poso.end(),posj[i+k-1]);
if (it==poso.end())
break;
ans=min(ans,mino[it-poso.begin()]+poso[it-poso.begin()]-posj[i]-k);
}
cout << (ans>=llinf?-1 : ans);
return 0;
}
Compilation message (stderr)
ho_t2.cpp: In function 'int main()':
ho_t2.cpp:26:18: 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]
26 | if (i+k-1>=posi.size())
| ~~~~~^~~~~~~~~~~~~
ho_t2.cpp:28:16: 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]
28 | if (i+1<posi.size())
| ~~~^~~~~~~~~~~~
ho_t2.cpp:35:18: 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]
35 | if (i+k-1>=poso.size())
| ~~~~~^~~~~~~~~~~~~
ho_t2.cpp:40:16: 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]
40 | if (i+1<poso.size())
| ~~~^~~~~~~~~~~~
ho_t2.cpp:46:22: 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]
46 | for (ll i=0;i+k-1<posj.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... |