#include <iostream>
#include <algorithm>
#include <vector>
#include <math.h>
#include <set>
#define int long long
#define ff first
#define ss second
#define pb push_back
#define pp pop_back
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define r0 return 0
using namespace std;
const int N = 3 * 1e5 + 5, M = 55, MOD = 998244353;
int n,m,k,l,r,q,cur,pos1,pos2,ans;
string s;
int pref1[N],pref2[N],pref3[N];
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> k;
cin >> s; s = " " + s;
for (int i = 1; i <= n; i++) {
pref1[i] = pref1[i - 1] + (s[i] == 'J');
pref2[i] = pref2[i - 1] + (s[i] == 'O');
pref3[i] = pref3[i - 1] + (s[i] == 'I');
}
ans = 1e9;
for (int i = 1; i <= n; i++) {
if (s[i] != 'J') continue;
int l = 1,r = i;
pos1 = 1e9;
while (l <= r) {
int mid = (l + r) / 2;
if (pref1[i] - pref1[mid - 1] >= k) {
pos1 = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
if (pos1 == 1e9) continue;
// cout << 1 << endl;
cur = 1e9;
l = i; r = n;
while (l <= r) {
int mid = (l + r) / 2;
if (pref2[mid] - pref2[i - 1] >= k) {
cur = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
if(cur == 1e9) continue;
// cout << 2 << endl;
pos2 = 1e9;
l = cur; r = n;
while (l <= r) {
int mid = (l + r) / 2;
if (pref3[mid] - pref3[cur - 1] >= k) {
pos2 = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
if (pos2 == 1e9) continue;
// cout << 3 << endl;
ans = min(ans,pos2 - pos1 + 1 - 3 * k);
}
if (ans == 1e9) {
cout << -1 << endl;
} else {
cout << ans << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
0 ms |
448 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
0 ms |
344 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
0 ms |
448 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
0 ms |
344 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
9 ms |
5680 KB |
Output is correct |
37 |
Correct |
10 ms |
5924 KB |
Output is correct |
38 |
Correct |
9 ms |
5924 KB |
Output is correct |
39 |
Correct |
11 ms |
5904 KB |
Output is correct |
40 |
Correct |
8 ms |
6148 KB |
Output is correct |
41 |
Correct |
10 ms |
6140 KB |
Output is correct |
42 |
Correct |
11 ms |
5924 KB |
Output is correct |
43 |
Correct |
4 ms |
4956 KB |
Output is correct |
44 |
Correct |
5 ms |
5200 KB |
Output is correct |
45 |
Correct |
9 ms |
5984 KB |
Output is correct |
46 |
Correct |
7 ms |
5924 KB |
Output is correct |
47 |
Correct |
7 ms |
6156 KB |
Output is correct |
48 |
Correct |
7 ms |
5920 KB |
Output is correct |
49 |
Correct |
5 ms |
4704 KB |
Output is correct |
50 |
Correct |
7 ms |
5924 KB |
Output is correct |
51 |
Correct |
9 ms |
5924 KB |
Output is correct |
52 |
Correct |
5 ms |
5932 KB |
Output is correct |
53 |
Correct |
6 ms |
5924 KB |
Output is correct |
54 |
Correct |
8 ms |
5924 KB |
Output is correct |
55 |
Correct |
5 ms |
5924 KB |
Output is correct |
56 |
Correct |
3 ms |
5924 KB |
Output is correct |