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 fori(i,a,b) for(int i=a;i<=b;i++)
#define int long long
#define ii pair<int,int>
#define fi first
#define se second
#define pb push_back
using namespace std;
const int mod=1e9+7;
const int M=2e5+10;
const int N=1e3+10;
int n,k;
string s;
int sum[4][M];
char ch[4]={'J','O','I'};
bool check(int l,int r)
{
fori(i,0,2) if(sum[i][r]-sum[i][l-1]<k) return false;
return true;
}
int id(char ch2)
{
fori(i,0,2) if(ch2==ch[i]) return i;
}
vector<int> v[4];
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#define task "1"
if(fopen(task".inp","r"))
{
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
cin >> n >> k;
cin >> s;
s=' '+s;
fori(i,1,n) v[id(s[i])].pb(i);
fori(i,0,n+1)
{
sum[0][i]=-1e9;
sum[2][i]=1e9;
}
int kq=1e9;
for(int i=k-1;i<=v[0].size()-1;i++) sum[0][v[0][i]]=v[0][i-k+1];
fori(i,1,n) sum[0][i]=max(sum[0][i-1],sum[0][i]);
for(int i=v[2].size()-k;i>=0;i--) sum[2][v[2][i]]=v[2][i+k-1];
for(int i=n;i!=0;i--) sum[2][i]=min(sum[2][i],sum[2][i+1]);
if(v[1].size())
for(int i=k-1;i<=v[1].size()-1;i++)
{
kq=min(kq,sum[2][v[1][i]]-sum[0][v[1][i-k+1]]+1);
//cout << sum[2][v[1][i]] << " " << sum[0][v[1][i-k+1]] << "\n";
}
cout << (kq>n ? -1 :kq-3*k);
}
Compilation message (stderr)
ho_t2.cpp: In function 'int32_t main()':
ho_t2.cpp:47: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]
47 | for(int i=k-1;i<=v[0].size()-1;i++) sum[0][v[0][i]]=v[0][i-k+1];
| ~^~~~~~~~~~~~~~~
ho_t2.cpp:52: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]
52 | for(int i=k-1;i<=v[1].size()-1;i++)
| ~^~~~~~~~~~~~~~~
ho_t2.cpp: In function 'long long int id(char)':
ho_t2.cpp:24:1: warning: control reaches end of non-void function [-Wreturn-type]
24 | }
| ^
ho_t2.cpp: In function 'int32_t main()':
ho_t2.cpp:34:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
ho_t2.cpp:35:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |