#include<bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(),x.end()
int INF=1e18;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n,k;cin>>n>>k;
string s;cin>>s;
vector<int> pre(n);
pre[0]=(s[0]=='O');
for(int i=1;i<n;i++){
pre[i]=pre[i-1]+(s[i]=='O');
}
auto f=[&](int a,int b)->int{
if(a>b){
return 0;
}
if(a==0){
return pre[b];
}
return pre[b]-pre[a-1];
};
vector<int> dp1(n),dp2(n);
int cnt=0;
queue<int> q;
for(int i=0;i<n;i++){
if(s[i]=='J'){
q.push(i);
}
if(q.size()>k){
q.pop();
}
if(q.size()<k){
dp1[i]=-INF;
}
else{
dp1[i]=q.front();
}
}
while(q.size()){
q.pop();
}
for(int i=n-1;i>=0;i--){
if(s[i]=='I'){
q.push(i);
}
if(q.size()>k){
q.pop();
}
if(q.size()<k){
dp2[i]=INF;
}
else{
dp2[i]=q.front();
}
}
int j=n-1;
int ans=INF;
for(int i=n-2;i>=0;i--){
while(f(i+1,j-2)>=k){
j--;
}
if(f(i+1,j-1)>=k){
ans=min(ans,dp2[j]-dp1[i]+1-k*3);
}
}
if(ans>=n){
cout<<-1<<endl;
}
else{
cout<<ans<<endl;
}
return 0;
}
Compilation message
ho_t2.cpp: In function 'int main()':
ho_t2.cpp:32:20: warning: comparison of integer expressions of different signedness: 'std::queue<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
32 | if(q.size()>k){
| ~~~~~~~~^~
ho_t2.cpp:35:20: warning: comparison of integer expressions of different signedness: 'std::queue<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
35 | if(q.size()<k){
| ~~~~~~~~^~
ho_t2.cpp:49:20: warning: comparison of integer expressions of different signedness: 'std::queue<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
49 | if(q.size()>k){
| ~~~~~~~~^~
ho_t2.cpp:52:20: warning: comparison of integer expressions of different signedness: 'std::queue<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
52 | if(q.size()<k){
| ~~~~~~~~^~
ho_t2.cpp:26:9: warning: unused variable 'cnt' [-Wunused-variable]
26 | int cnt=0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
316 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
324 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
316 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
324 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
6 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
320 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
320 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
316 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
324 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
6 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
320 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
320 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
8 ms |
5088 KB |
Output is correct |
37 |
Correct |
9 ms |
5464 KB |
Output is correct |
38 |
Correct |
8 ms |
5472 KB |
Output is correct |
39 |
Correct |
9 ms |
5460 KB |
Output is correct |
40 |
Correct |
9 ms |
5616 KB |
Output is correct |
41 |
Correct |
8 ms |
5480 KB |
Output is correct |
42 |
Correct |
10 ms |
5552 KB |
Output is correct |
43 |
Correct |
6 ms |
3512 KB |
Output is correct |
44 |
Correct |
7 ms |
4348 KB |
Output is correct |
45 |
Correct |
8 ms |
5572 KB |
Output is correct |
46 |
Correct |
8 ms |
5600 KB |
Output is correct |
47 |
Correct |
9 ms |
5600 KB |
Output is correct |
48 |
Correct |
8 ms |
5572 KB |
Output is correct |
49 |
Correct |
6 ms |
3856 KB |
Output is correct |
50 |
Correct |
10 ms |
5600 KB |
Output is correct |
51 |
Correct |
9 ms |
5596 KB |
Output is correct |
52 |
Correct |
8 ms |
5600 KB |
Output is correct |
53 |
Correct |
7 ms |
5888 KB |
Output is correct |
54 |
Correct |
7 ms |
5572 KB |
Output is correct |
55 |
Correct |
6 ms |
5808 KB |
Output is correct |
56 |
Correct |
6 ms |
5976 KB |
Output is correct |