#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define ll long long
using namespace std;
const int N = 1e6 + 5;
const int MOD = 1e9 + 7;
int n, k;
string s;
void solve(){
cin >> n >> k >> s;
map<char, vector<int> > pos;
for(int i = 0; i < s.size(); ++i)
pos[s[i]].push_back(i);
int pre_sum[pos['J'].size()] = {0}, pre_sum1[pos['O'].size()] = {0}, pre_sum2[pos['I'].size()] = {0};
for(int i = 1; i < pos['J'].size(); ++i)
pre_sum[i] = pre_sum[i - 1] + (pos['J'][i] - pos['J'][i - 1] - 1);
for(int i = 1; i < pos['O'].size(); ++i)
pre_sum1[i] = pre_sum1[i - 1] + (pos['O'][i] - pos['O'][i - 1] - 1);
for(int i = 1; i < pos['I'].size(); ++i)
pre_sum2[i] = pre_sum2[i - 1] + (pos['I'][i] - pos['I'][i - 1] - 1);
int ans = INT_MAX;
for(int i = 0; i < pos['J'].size(); ++i){
int till = i + k - 1;
if(till >= pos['J'].size())
continue;
int res;
res = pre_sum[till] - pre_sum[i];
int from = pos['J'][till] + 1;
int low = 0, high = pos['O'].size() - 1, cur = -1;
while(low <= high){
int mid = (low + high) / 2;
if(pos['O'][mid] >= from){
cur = mid;
high = mid - 1;
}
else
low = mid + 1;
}
if(cur == -1) continue;
if(cur + k - 1 >= pos['O'].size()) continue;
res += pos['O'][cur] - from;
res += pre_sum1[cur + k - 1] - pre_sum1[cur];
int from1 = pos['O'][cur + k - 1] + 1;
low = 0, high = pos['I'].size() - 1, cur = -1;
while(low <= high){
int mid = (low + high) / 2;
if(pos['I'][mid] >= from1){
cur = mid;
high = mid - 1;
}
else
low = mid + 1;
}
if(cur == -1) continue;
if(cur + k - 1 >= pos['I'].size()) continue;
res += pos['I'][cur] - from1;
res += pre_sum2[cur + k - 1] - pre_sum2[cur];
ans = min(ans, res);
}
cout << (ans == INT_MAX ? -1 : ans) << "\n";
}
int main(){
fast;
int t = 1;
// cin >> t;
while(t--)
solve();
return 0;
}
Compilation message
ho_t2.cpp: In function 'void solve()':
ho_t2.cpp:14:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
14 | for(int i = 0; i < s.size(); ++i)
| ~~^~~~~~~~~~
ho_t2.cpp:19:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | for(int i = 1; i < pos['J'].size(); ++i)
| ~~^~~~~~~~~~~~~~~~~
ho_t2.cpp:22:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for(int i = 1; i < pos['O'].size(); ++i)
| ~~^~~~~~~~~~~~~~~~~
ho_t2.cpp:25:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for(int i = 1; i < pos['I'].size(); ++i)
| ~~^~~~~~~~~~~~~~~~~
ho_t2.cpp:30:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for(int i = 0; i < pos['J'].size(); ++i){
| ~~^~~~~~~~~~~~~~~~~
ho_t2.cpp:32:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | if(till >= pos['J'].size())
| ~~~~~^~~~~~~~~~~~~~~~~~
ho_t2.cpp:48:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | if(cur + k - 1 >= pos['O'].size()) continue;
| ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
ho_t2.cpp:63:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | if(cur + k - 1 >= pos['I'].size()) continue;
| ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
2 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
2 ms |
512 KB |
Output is correct |
26 |
Correct |
2 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
2 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
2 ms |
512 KB |
Output is correct |
26 |
Correct |
2 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Correct |
1 ms |
364 KB |
Output is correct |
36 |
Correct |
26 ms |
2408 KB |
Output is correct |
37 |
Correct |
28 ms |
2736 KB |
Output is correct |
38 |
Correct |
28 ms |
2616 KB |
Output is correct |
39 |
Correct |
26 ms |
2516 KB |
Output is correct |
40 |
Correct |
21 ms |
2532 KB |
Output is correct |
41 |
Correct |
24 ms |
2472 KB |
Output is correct |
42 |
Correct |
28 ms |
2736 KB |
Output is correct |
43 |
Correct |
12 ms |
1800 KB |
Output is correct |
44 |
Correct |
14 ms |
2160 KB |
Output is correct |
45 |
Correct |
18 ms |
2532 KB |
Output is correct |
46 |
Correct |
18 ms |
2524 KB |
Output is correct |
47 |
Correct |
20 ms |
2532 KB |
Output is correct |
48 |
Correct |
18 ms |
2532 KB |
Output is correct |
49 |
Correct |
13 ms |
1872 KB |
Output is correct |
50 |
Correct |
18 ms |
2520 KB |
Output is correct |
51 |
Correct |
18 ms |
2532 KB |
Output is correct |
52 |
Correct |
9 ms |
2288 KB |
Output is correct |
53 |
Correct |
10 ms |
2524 KB |
Output is correct |
54 |
Correct |
21 ms |
2424 KB |
Output is correct |
55 |
Correct |
12 ms |
2444 KB |
Output is correct |
56 |
Correct |
7 ms |
2424 KB |
Output is correct |