#include <bits/stdc++.h>
#define N 400005
#define int long long
using namespace std;
void ck(string s,int k){
int cnt = 0;
for(auto u:s){
if(u == 'J' && cnt < k)cnt++;
if(u == 'O' && cnt >= k && cnt < 2*k)cnt++;
if(u == 'I' && cnt >= 2*k && cnt < 3*k)cnt++;
}
if(cnt != 3*k){
cout << -1 << endl;
exit(0);
}
}
int J[N],O[N],I[N];
void solve(){
int n,k;
cin >> n >> k;
string s;
cin >> s;
ck(s,k);
s = "."+s;
for(int i=0;i<N;i++){
J[i] = O[i] = I[i] = N-1;
}
queue<int> J_J,O_O,I_I;
for(int i=n;i>=1;i--){
if(s[i] == 'J')J_J.push(i);
if(s[i] == 'O')O_O.push(i);
if(s[i] == 'I')I_I.push(i);
if(J_J.size() > k)J_J.pop();
if(O_O.size() > k)O_O.pop();
if(I_I.size() > k)I_I.pop();
if(J_J.size() == k)J[i] = J_J.front() + 1;
if(O_O.size() == k)O[i] = O_O.front() + 1;
if(I_I.size() == k)I[i] = I_I.front() + 1;
}
int maxi = 0;
for(int i=1;i<=n;i++){
maxi = max(maxi,i+n-I[O[J[i]]]);
}
cout << n - 3*k - maxi;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef Local
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t=1;
//cin>>t;
while(t--){
solve();
}
#ifdef Local
cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
#endif
}
Compilation message
ho_t2.cpp: In function 'void solve()':
ho_t2.cpp:33:23: 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]
33 | if(J_J.size() > k)J_J.pop();
| ~~~~~~~~~~~^~~
ho_t2.cpp:34:23: 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]
34 | if(O_O.size() > k)O_O.pop();
| ~~~~~~~~~~~^~~
ho_t2.cpp:35:23: 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(I_I.size() > k)I_I.pop();
| ~~~~~~~~~~~^~~
ho_t2.cpp:36:23: 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]
36 | if(J_J.size() == k)J[i] = J_J.front() + 1;
| ~~~~~~~~~~~^~~~
ho_t2.cpp:37:23: 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]
37 | if(O_O.size() == k)O[i] = O_O.front() + 1;
| ~~~~~~~~~~~^~~~
ho_t2.cpp:38:23: 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]
38 | if(I_I.size() == k)I[i] = I_I.front() + 1;
| ~~~~~~~~~~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9676 KB |
Output is correct |
2 |
Correct |
5 ms |
9676 KB |
Output is correct |
3 |
Correct |
5 ms |
9676 KB |
Output is correct |
4 |
Correct |
5 ms |
9676 KB |
Output is correct |
5 |
Correct |
5 ms |
9676 KB |
Output is correct |
6 |
Correct |
5 ms |
9664 KB |
Output is correct |
7 |
Correct |
6 ms |
9676 KB |
Output is correct |
8 |
Correct |
6 ms |
9676 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
5 ms |
9616 KB |
Output is correct |
13 |
Correct |
5 ms |
9676 KB |
Output is correct |
14 |
Correct |
5 ms |
9676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9676 KB |
Output is correct |
2 |
Correct |
5 ms |
9676 KB |
Output is correct |
3 |
Correct |
5 ms |
9676 KB |
Output is correct |
4 |
Correct |
5 ms |
9676 KB |
Output is correct |
5 |
Correct |
5 ms |
9676 KB |
Output is correct |
6 |
Correct |
5 ms |
9664 KB |
Output is correct |
7 |
Correct |
6 ms |
9676 KB |
Output is correct |
8 |
Correct |
6 ms |
9676 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
5 ms |
9616 KB |
Output is correct |
13 |
Correct |
5 ms |
9676 KB |
Output is correct |
14 |
Correct |
5 ms |
9676 KB |
Output is correct |
15 |
Correct |
5 ms |
9676 KB |
Output is correct |
16 |
Correct |
5 ms |
9600 KB |
Output is correct |
17 |
Correct |
5 ms |
9676 KB |
Output is correct |
18 |
Correct |
5 ms |
9676 KB |
Output is correct |
19 |
Correct |
6 ms |
9676 KB |
Output is correct |
20 |
Correct |
5 ms |
9676 KB |
Output is correct |
21 |
Correct |
5 ms |
9676 KB |
Output is correct |
22 |
Correct |
5 ms |
9676 KB |
Output is correct |
23 |
Correct |
5 ms |
9676 KB |
Output is correct |
24 |
Correct |
5 ms |
9676 KB |
Output is correct |
25 |
Correct |
6 ms |
9676 KB |
Output is correct |
26 |
Correct |
5 ms |
9676 KB |
Output is correct |
27 |
Correct |
5 ms |
9676 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
0 ms |
204 KB |
Output is correct |
33 |
Correct |
6 ms |
9676 KB |
Output is correct |
34 |
Correct |
5 ms |
9612 KB |
Output is correct |
35 |
Correct |
6 ms |
9676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9676 KB |
Output is correct |
2 |
Correct |
5 ms |
9676 KB |
Output is correct |
3 |
Correct |
5 ms |
9676 KB |
Output is correct |
4 |
Correct |
5 ms |
9676 KB |
Output is correct |
5 |
Correct |
5 ms |
9676 KB |
Output is correct |
6 |
Correct |
5 ms |
9664 KB |
Output is correct |
7 |
Correct |
6 ms |
9676 KB |
Output is correct |
8 |
Correct |
6 ms |
9676 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
5 ms |
9616 KB |
Output is correct |
13 |
Correct |
5 ms |
9676 KB |
Output is correct |
14 |
Correct |
5 ms |
9676 KB |
Output is correct |
15 |
Correct |
5 ms |
9676 KB |
Output is correct |
16 |
Correct |
5 ms |
9600 KB |
Output is correct |
17 |
Correct |
5 ms |
9676 KB |
Output is correct |
18 |
Correct |
5 ms |
9676 KB |
Output is correct |
19 |
Correct |
6 ms |
9676 KB |
Output is correct |
20 |
Correct |
5 ms |
9676 KB |
Output is correct |
21 |
Correct |
5 ms |
9676 KB |
Output is correct |
22 |
Correct |
5 ms |
9676 KB |
Output is correct |
23 |
Correct |
5 ms |
9676 KB |
Output is correct |
24 |
Correct |
5 ms |
9676 KB |
Output is correct |
25 |
Correct |
6 ms |
9676 KB |
Output is correct |
26 |
Correct |
5 ms |
9676 KB |
Output is correct |
27 |
Correct |
5 ms |
9676 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
0 ms |
204 KB |
Output is correct |
33 |
Correct |
6 ms |
9676 KB |
Output is correct |
34 |
Correct |
5 ms |
9612 KB |
Output is correct |
35 |
Correct |
6 ms |
9676 KB |
Output is correct |
36 |
Correct |
11 ms |
9832 KB |
Output is correct |
37 |
Correct |
12 ms |
9808 KB |
Output is correct |
38 |
Correct |
12 ms |
9924 KB |
Output is correct |
39 |
Correct |
13 ms |
9936 KB |
Output is correct |
40 |
Correct |
13 ms |
10448 KB |
Output is correct |
41 |
Correct |
12 ms |
10340 KB |
Output is correct |
42 |
Correct |
13 ms |
10136 KB |
Output is correct |
43 |
Correct |
10 ms |
10188 KB |
Output is correct |
44 |
Correct |
11 ms |
10408 KB |
Output is correct |
45 |
Correct |
12 ms |
10704 KB |
Output is correct |
46 |
Correct |
13 ms |
10704 KB |
Output is correct |
47 |
Correct |
13 ms |
10576 KB |
Output is correct |
48 |
Correct |
12 ms |
10576 KB |
Output is correct |
49 |
Correct |
2 ms |
728 KB |
Output is correct |
50 |
Correct |
3 ms |
984 KB |
Output is correct |
51 |
Correct |
3 ms |
984 KB |
Output is correct |
52 |
Correct |
3 ms |
984 KB |
Output is correct |
53 |
Correct |
3 ms |
984 KB |
Output is correct |
54 |
Correct |
9 ms |
10064 KB |
Output is correct |
55 |
Correct |
10 ms |
11212 KB |
Output is correct |
56 |
Correct |
10 ms |
11728 KB |
Output is correct |