#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e17;
typedef vector<int> vi; typedef vector<vector<int>> vvi;
typedef pair<int, int> pii;
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, k; cin >> n >> k;
string s; cin >> s;
vi bo(n), ao(n), co(n); // nearest O before i, nearest O after i, count of O
bo[0] = (s[0] == 'O') - 1;
co[0] = (s[0] == 'O');
for (int i = 1; i < n; i++){
bo[i] = (s[i] == 'O') ? i : bo[i-1];
co[i] = co[i-1] + (s[i] == 'O');
}
ao[n-1] = (s[n-1] == 'O') ? n-1 : -1;
for (int i = n-2; i >= 0; i--)
ao[i] = (s[i] == 'O') ? i : ao[i+1];
vector<pii> jc;
deque<int> d; int cost = 0;
for (int i = 0; i < n; i++){
if (s[i] != 'J') continue;
if (!d.empty())
cost += i - d.back() - 1;
d.push_back(i);
if ((int)d.size() == k+1){
int f = d.front();
d.pop_front();
cost -= d.front() - f - 1;
}
if ((int)d.size() == k)
jc.push_back({i, cost});
}
vi ic(n, INF); // cost if last I is at i
d.clear(); cost = 0;
for (int i = n-1; i >= 0; i--){
if (i < n-1)
ic[i] = ic[i+1];
if (s[i] != 'I') continue;
if (!d.empty())
cost += d.back() - i - 1;
d.push_back(i);
if ((int)d.size() == k+1){
int f = d.front();
d.pop_front();
cost -= f - d.front() - 1;
}
if ((int)d.size() == k)
ic[i] = min(ic[i], cost + i);
}
int ans = -1;
for (auto [j, c] : jc){
auto ptr = lower_bound(co.begin(), co.end(), co[j] + k);
if (ptr == co.end()) break;
int ci = ic[ptr-co.begin()];
if (ci == INF) continue;
int cost = ci + (c - j) - (k + 1);
if (ans == -1 || cost < ans)
ans = cost;
}
// if (co[i-1] - co[j] < k)
// continue;
// int cost = (ci + i) + (cj - j) - (k + 1);
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 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 |
348 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 |
348 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 |
348 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 |
348 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 |
348 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 |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 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 |
0 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 |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
376 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 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 |
348 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 |
348 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 |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 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 |
0 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 |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
376 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
344 KB |
Output is correct |
36 |
Correct |
10 ms |
7516 KB |
Output is correct |
37 |
Correct |
12 ms |
8304 KB |
Output is correct |
38 |
Correct |
11 ms |
8280 KB |
Output is correct |
39 |
Correct |
10 ms |
8028 KB |
Output is correct |
40 |
Correct |
9 ms |
8036 KB |
Output is correct |
41 |
Correct |
9 ms |
8148 KB |
Output is correct |
42 |
Correct |
11 ms |
8308 KB |
Output is correct |
43 |
Correct |
5 ms |
5300 KB |
Output is correct |
44 |
Correct |
7 ms |
6244 KB |
Output is correct |
45 |
Correct |
8 ms |
8044 KB |
Output is correct |
46 |
Correct |
8 ms |
8016 KB |
Output is correct |
47 |
Correct |
8 ms |
8044 KB |
Output is correct |
48 |
Correct |
9 ms |
8028 KB |
Output is correct |
49 |
Correct |
6 ms |
5472 KB |
Output is correct |
50 |
Correct |
8 ms |
7912 KB |
Output is correct |
51 |
Correct |
8 ms |
8044 KB |
Output is correct |
52 |
Correct |
6 ms |
7388 KB |
Output is correct |
53 |
Correct |
7 ms |
7804 KB |
Output is correct |
54 |
Correct |
10 ms |
8176 KB |
Output is correct |
55 |
Correct |
6 ms |
7776 KB |
Output is correct |
56 |
Correct |
5 ms |
7656 KB |
Output is correct |