#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define int long long
const int inf=1e18;
signed main()
{
int n,k;cin>>n>>k;
string s;cin>>s;
int a[n+1],pref[4][n+1];
memset(pref,0,sizeof(pref));
vector<int>b[4];
for(int i=1;i<=n;i++)
{
if(s[i-1]=='J') a[i]=1;
if(s[i-1]=='O') a[i]=2;
if(s[i-1]=='I') a[i]=3;
for(int j=1;j<=3;j++)
{
pref[j][i]=pref[j][i-1];
if(j==a[i]) pref[j][i]++;
}
b[a[i]].pb(i);
}
bool bul=true;
for(int j=1;j<=3;j++)
{
if(pref[j][n]<k) bul=false;
}
if(!bul)
{
cout<<"-1\n";
return 0;
}
int I[n+1]={0};
for(int i=1,j=0;i<=n;i++)
{
if(a[i]!=1 && pref[1][i]>0) I[i]=I[i-1]+1;
else
{
I[i]=I[i-1];
if(pref[1][i]>k)
{
I[i]-=b[1][j+1]-b[1][j]-1;
j++;
}
}
}
int J[n+50]={0};
for(int i=n,j=b[3].size()-1;i>=1;i--)
{
if(a[i]!=3 && pref[3][n]-pref[3][i-1]>0) J[i]=J[i+1]+1;
else
{
J[i]=J[i+1];
if(pref[3][n]-pref[3][i-1]>k)
{
J[i]-=b[3][j]-b[3][j-1]-1;
j--;
}
}
}
/*for(int i=1;i<=n;i++) cout<<I[i]<<" ";
cout<<endl;
for(int i=1;i<=n;i++) cout<<J[i]<<" ";
cout<<endl;*/
int res=inf,sum=0;
for(int i=1;i<=k-1;i++)
{
sum+=b[2][i]-b[2][i-1]-1;
}
for(int j=k-1;j<b[2].size();j++)
{
int x=sum;
x+=I[b[2][j-(k-1)]-1];
x+=J[b[2][j]+1];
if(pref[1][b[2][j-(k-1)]-1]<k || pref[3][n]-pref[3][b[2][j]]<k) x=inf;
res=min(res,x);
if(j+1<b[2].size())sum+=b[2][j+1]-b[2][j]-1;
sum-=b[2][j-(k-1)+1]-b[2][j-(k-1)]-1;
}
if(res>=inf) res=-1;
cout<<res<<endl;
/*for(int i=1;i<=n;i++)
{
bool bul=true;
for(int j=1;j<=3;j++) if(pref[j][i]<k) bul=false;
if(!bul) continue;
int ind=inf;
for(int j=1;j<=3;j++)
{
int lb=lower_bound(pref[j],pref[j]+n+1,pref[j][i]-k)-pref[j];
ind=min(ind,lb);
}
int x=0;
for(int j=1;j<=3;j++)
{
x+=pref[j][i]-pref[j][ind]-k;
}
res=min(res,x);
cout<<i<<" "<<ind<<endl;
}*/
return 0;
}
Compilation message
ho_t2.cpp: In function 'int main()':
ho_t2.cpp:72:17: 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]
72 | for(int j=k-1;j<b[2].size();j++)
| ~^~~~~~~~~~~~
ho_t2.cpp:79:9: 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]
79 | if(j+1<b[2].size())sum+=b[2][j+1]-b[2][j]-1;
| ~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
436 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
436 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
0 ms |
600 KB |
Output is correct |
17 |
Correct |
0 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
0 ms |
604 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
0 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
600 KB |
Output is correct |
30 |
Correct |
0 ms |
604 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
600 KB |
Output is correct |
34 |
Correct |
0 ms |
604 KB |
Output is correct |
35 |
Correct |
0 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
436 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
0 ms |
600 KB |
Output is correct |
17 |
Correct |
0 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
0 ms |
604 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
0 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
600 KB |
Output is correct |
30 |
Correct |
0 ms |
604 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
600 KB |
Output is correct |
34 |
Correct |
0 ms |
604 KB |
Output is correct |
35 |
Correct |
0 ms |
604 KB |
Output is correct |
36 |
Correct |
13 ms |
12492 KB |
Output is correct |
37 |
Correct |
13 ms |
13256 KB |
Output is correct |
38 |
Correct |
16 ms |
13356 KB |
Output is correct |
39 |
Correct |
14 ms |
13380 KB |
Output is correct |
40 |
Correct |
14 ms |
13584 KB |
Output is correct |
41 |
Correct |
13 ms |
13516 KB |
Output is correct |
42 |
Correct |
14 ms |
13260 KB |
Output is correct |
43 |
Correct |
8 ms |
8660 KB |
Output is correct |
44 |
Correct |
12 ms |
10580 KB |
Output is correct |
45 |
Correct |
14 ms |
13456 KB |
Output is correct |
46 |
Correct |
14 ms |
13316 KB |
Output is correct |
47 |
Correct |
13 ms |
13516 KB |
Output is correct |
48 |
Correct |
13 ms |
13484 KB |
Output is correct |
49 |
Correct |
9 ms |
9112 KB |
Output is correct |
50 |
Correct |
14 ms |
13488 KB |
Output is correct |
51 |
Correct |
14 ms |
13436 KB |
Output is correct |
52 |
Correct |
13 ms |
13004 KB |
Output is correct |
53 |
Correct |
14 ms |
13328 KB |
Output is correct |
54 |
Correct |
11 ms |
13416 KB |
Output is correct |
55 |
Correct |
12 ms |
13416 KB |
Output is correct |
56 |
Correct |
11 ms |
13416 KB |
Output is correct |