#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize("-Ofast")
#include <bits/stdc++.h>
#include <algorithm>
#include <iostream>
#include <vector>
#include <limits>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <math.h>
using namespace std;
using ll = long long;
void solve(){
ll n, k;
string s;
cin >> n >> k;
cin >> s;
// Use while loop and do dp
vector<ll> Os(n + 1, 0);
vector<ll> Js(n + 1, 0);
vector<ll> Is(n + 1, 0);
for(ll i = 0; i < n; i++){
Os[i + 1] = Os[i];
Js[i + 1] = Js[i];
Is[i + 1] = Is[i];
if(s[i] == 'O'){
Os[i + 1]++;
}else if(s[i] == 'J'){
Js[i + 1]++;
}else{
Is[i + 1]++;
}
}
ll Curr_min = 1E9;
for(ll i = 0; i < n; i++){
ll temp = 0;
if(Js[i + 1] < k){
continue;
}
ll l = 0;
ll r = i + 1;
while(l + 1 < r){
ll mid = (l + r)/2;
if(Js[i + 1] - Js[mid] >= k){
l = mid;
}else{
r = mid;
}
}
temp += i - l + 1 - k;
//cout << temp << endl;
ll new_start = -1;
l = i;
r = n;
while(l + 1 < r){
ll mid = (l + r)/2;
if(Os[mid] - Os[i] >= k){
r = mid;
}else{
l = mid;
}
}
temp += r - i - 1 - k;
//cout << temp << endl;
new_start = r;
if(Is[n] - Is[new_start] < k){
continue;
}
l = new_start;
r = n;
while(l + 1 < r){
ll mid = (l + r)/2;
if(Is[mid] - Is[new_start] < k){
l = mid;
}else{
r = mid;
}
}
temp += r - new_start - k;
//cout << temp << endl;
Curr_min = min(Curr_min, max(temp, 0LL));
}
if(Curr_min == 1E9){
cout << -1;
}else{
cout << Curr_min;
}
}
int main(){
ios_base::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
ll t = 1;
while(t--){
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
500 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
456 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
500 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
456 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
2 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
464 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
2 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
2 ms |
548 KB |
Output is correct |
35 |
Correct |
1 ms |
508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
500 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
456 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
2 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
464 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
2 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
2 ms |
548 KB |
Output is correct |
35 |
Correct |
1 ms |
508 KB |
Output is correct |
36 |
Correct |
17 ms |
5084 KB |
Output is correct |
37 |
Correct |
19 ms |
5672 KB |
Output is correct |
38 |
Correct |
19 ms |
5596 KB |
Output is correct |
39 |
Correct |
27 ms |
5596 KB |
Output is correct |
40 |
Correct |
17 ms |
5596 KB |
Output is correct |
41 |
Correct |
22 ms |
5580 KB |
Output is correct |
42 |
Correct |
25 ms |
5596 KB |
Output is correct |
43 |
Correct |
10 ms |
3664 KB |
Output is correct |
44 |
Correct |
12 ms |
4316 KB |
Output is correct |
45 |
Correct |
21 ms |
5604 KB |
Output is correct |
46 |
Correct |
14 ms |
5596 KB |
Output is correct |
47 |
Correct |
15 ms |
5596 KB |
Output is correct |
48 |
Correct |
15 ms |
5596 KB |
Output is correct |
49 |
Correct |
10 ms |
3804 KB |
Output is correct |
50 |
Correct |
16 ms |
5764 KB |
Output is correct |
51 |
Correct |
14 ms |
5600 KB |
Output is correct |
52 |
Correct |
7 ms |
5364 KB |
Output is correct |
53 |
Correct |
8 ms |
5596 KB |
Output is correct |
54 |
Correct |
19 ms |
5596 KB |
Output is correct |
55 |
Correct |
15 ms |
5596 KB |
Output is correct |
56 |
Correct |
13 ms |
5596 KB |
Output is correct |