#include <bits/stdc++.h>
#define Kuanyshh ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
//#define s second
//#define f first
#pragma GCC optimize( "Ofast", "O3" , "unroll-loops")
#pragma GCC target ("sse,sse2")
using namespace std;
using ll = long long;
const int inf = 1e18+77;
const int mod = 1e9+7;
const int mx = 1e5+79;
int cntj[mx], cnto[mx], cnti[mx];
void shift(){
int n, k;
string s;
cin >> n >> k >> s;
int res = inf, index, kj = 0, ko = 0, ki = 0, ko2=0, ki2=0;
vector<int> o, ii, j;
for(int i = 0; i < s.size(); i++){
if(s[i] == 'J'){
j.push_back(i);
cntj[j.size()] = i;
}
if(s[i] == 'O'){
o.push_back(i);
cnto[o.size()] = i;
}
if(s[i] == 'I'){
ii.push_back(i);
cnti[ii.size()] = i;
}
}
kj = j.size();
ko = o.size();
ki = ii.size();
for(int i = 1; i <= kj; i++){
if(i + k - 1 > kj){
break;
}else{
index = cntj[i + k - 1];
}
while(cnto[ko2] <= index){
ko2++;
if(ko2 > ko) break;
}
if(ko2 + k - 1 > ko){
break;
}else{
index = cnto[ko2 + k - 1];
}
while(cnti[ki2] < index){
ki2++;
if(ki2 > ki){
break;
}
}
if(ki2 + k - 1 > ki){
break;
}else{
index = cnti[ki2 + k - 1];
}
int ans = index - cntj[i] + 1 - 3 * k;
res = min(res, ans);
}
if(res == inf || res < 0) cout << -1;
else cout << res;
}
signed main(){
Kuanyshh;
int tt = 1;
// cin >> tt;
while(tt--){
shift();
}
}
Compilation message
ho_t2.cpp:12:21: warning: overflow in conversion from 'double' to 'int' changes value from '1.0000000000000001e+18' to '2147483647' [-Woverflow]
12 | const int inf = 1e18+77;
| ~~~~^~~
ho_t2.cpp: In function 'void shift()':
ho_t2.cpp:24:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for(int i = 0; i < s.size(); i++){
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
0 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 |
6 ms |
2128 KB |
Output is correct |
37 |
Correct |
6 ms |
2468 KB |
Output is correct |
38 |
Correct |
6 ms |
2476 KB |
Output is correct |
39 |
Correct |
6 ms |
2500 KB |
Output is correct |
40 |
Correct |
5 ms |
2520 KB |
Output is correct |
41 |
Correct |
5 ms |
2460 KB |
Output is correct |
42 |
Correct |
6 ms |
2464 KB |
Output is correct |
43 |
Correct |
3 ms |
1648 KB |
Output is correct |
44 |
Correct |
3 ms |
1880 KB |
Output is correct |
45 |
Correct |
4 ms |
2520 KB |
Output is correct |
46 |
Correct |
4 ms |
2572 KB |
Output is correct |
47 |
Correct |
4 ms |
2520 KB |
Output is correct |
48 |
Correct |
5 ms |
2520 KB |
Output is correct |
49 |
Correct |
3 ms |
1836 KB |
Output is correct |
50 |
Correct |
4 ms |
2508 KB |
Output is correct |
51 |
Correct |
5 ms |
2520 KB |
Output is correct |
52 |
Correct |
5 ms |
2136 KB |
Output is correct |
53 |
Correct |
5 ms |
2364 KB |
Output is correct |
54 |
Correct |
3 ms |
2524 KB |
Output is correct |
55 |
Correct |
3 ms |
2524 KB |
Output is correct |
56 |
Correct |
3 ms |
2524 KB |
Output is correct |