/**
_ _ __ _ _ _ _ _ _
|a ||t ||o d | |o |
| __ _| | _ | __| _ |
| __ |/_ | __ /__\ / _\|
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int N_MAX = 2000000;
const ll INF = LLONG_MAX;
const ld EPS = 1e-9;
int N, K;
int A[N_MAX + 2];
struct SegInfo {
ll first; int rate;
};
SegInfo seg_tree[N_MAX * 4 + 2];
void build (int node, int l, int r) {
seg_tree[node] = SegInfo{0, 0};
if (l == r) {
return;
}
int mid = (l + r) / 2;
int left = node * 2, right = node * 2 + 1;
build(left, l, mid);
build(right, mid + 1, r);
}
void build () {
build(1, 1, N);
}
void split (int node, int l, int r) {
int mid = (l + r) / 2;
int left = node * 2, right = node * 2 + 1;
seg_tree[left].first += seg_tree[node].first + (ll) (r - mid) * seg_tree[node].rate;
seg_tree[left].rate += seg_tree[node].rate;
seg_tree[right].first += seg_tree[node].first;
seg_tree[right].rate += seg_tree[node].rate;
seg_tree[node].first = seg_tree[node].rate = 0;
}
void update (int node, int l, int r, int pos, ll first) {
if (r <= pos) {
seg_tree[node].first += first + (pos - r);
seg_tree[node].rate++;
return;
}
split(node, l, r);
int mid = (l + r) / 2;
int left = node * 2, right = node * 2 + 1;
update(left, l, mid, pos, first);
if (mid + 1 <= pos) {
update(right, mid + 1, r, pos, first);
}
}
void update (int pos, int first) {
update(1, 1, N, pos, first);
}
ll query (int node, int l, int r, int pos) {
if (l == r) {
return seg_tree[node].first;
}
split(node, l, r);
int mid = (l + r) / 2;
int left = node * 2, right = node * 2 + 1;
if (pos <= mid) {
return query(left, l, mid, pos);
} else {
return query(right, mid + 1, r, pos);
}
}
ll query (int pos) {
return query(1, 1, N, pos);
}
ld dp[N_MAX + 2];
int cnt[N_MAX + 2];
pair <ld, int> f (int j) {
return make_pair(dp[j] + query(j + 1), cnt[j]);
}
ll solve (ld pen) {
build();
deque <int> rel;
for (int i = 1; i <= N; i++) {
if (A[i] - i + 1 <= i) {
update(A[i] - i + 1, 0);
} else {
update(i, (A[i] - i + 1) - i);
}
while (rel.empty() == false && f(i - 1) <= f(rel.back())) {
rel.pop_back();
}
rel.push_back(i - 1);
while ((int) rel.size() >= 2 && f(rel[0]) >= f(rel[1])) {
rel.pop_front();
}
if (rel.empty() == true) {
dp[i] = INF;
} else {
tie(dp[i], cnt[i]) = f(rel.front());
dp[i] += pen; cnt[i]++;
}
}
return (ll) round(dp[N] - pen * K);
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(2);
cin >> N >> K;
string S;
cin >> S;
for (int i = 0, j = 0; i < N * 2; i++) {
if (S[i] == 'A') {
A[++j] = i + 1;
}
}
ld l = 0, r = LLONG_MAX; int iter = 150;
while (iter--) {
ld mid = (l + r) / 2;
solve(mid);
if (cnt[N] <= K) {
r = mid;
} else {
l = mid + EPS;
}
}
cout << solve(l) << "\n";
assert(cnt[N] <= K);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4532 KB |
Output is correct |
14 |
Correct |
1 ms |
4564 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4532 KB |
Output is correct |
14 |
Correct |
1 ms |
4564 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
10 ms |
4444 KB |
Output is correct |
18 |
Correct |
32 ms |
4444 KB |
Output is correct |
19 |
Correct |
38 ms |
4444 KB |
Output is correct |
20 |
Correct |
32 ms |
4440 KB |
Output is correct |
21 |
Correct |
30 ms |
4440 KB |
Output is correct |
22 |
Correct |
26 ms |
4444 KB |
Output is correct |
23 |
Correct |
25 ms |
4444 KB |
Output is correct |
24 |
Incorrect |
31 ms |
4444 KB |
Output isn't correct |
25 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4532 KB |
Output is correct |
14 |
Correct |
1 ms |
4564 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
10 ms |
4444 KB |
Output is correct |
18 |
Correct |
32 ms |
4444 KB |
Output is correct |
19 |
Correct |
38 ms |
4444 KB |
Output is correct |
20 |
Correct |
32 ms |
4440 KB |
Output is correct |
21 |
Correct |
30 ms |
4440 KB |
Output is correct |
22 |
Correct |
26 ms |
4444 KB |
Output is correct |
23 |
Correct |
25 ms |
4444 KB |
Output is correct |
24 |
Incorrect |
31 ms |
4444 KB |
Output isn't correct |
25 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4532 KB |
Output is correct |
14 |
Correct |
1 ms |
4564 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
10 ms |
4444 KB |
Output is correct |
18 |
Correct |
32 ms |
4444 KB |
Output is correct |
19 |
Correct |
38 ms |
4444 KB |
Output is correct |
20 |
Correct |
32 ms |
4440 KB |
Output is correct |
21 |
Correct |
30 ms |
4440 KB |
Output is correct |
22 |
Correct |
26 ms |
4444 KB |
Output is correct |
23 |
Correct |
25 ms |
4444 KB |
Output is correct |
24 |
Incorrect |
31 ms |
4444 KB |
Output isn't correct |
25 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4532 KB |
Output is correct |
14 |
Correct |
1 ms |
4564 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
10 ms |
4444 KB |
Output is correct |
18 |
Correct |
32 ms |
4444 KB |
Output is correct |
19 |
Correct |
38 ms |
4444 KB |
Output is correct |
20 |
Correct |
32 ms |
4440 KB |
Output is correct |
21 |
Correct |
30 ms |
4440 KB |
Output is correct |
22 |
Correct |
26 ms |
4444 KB |
Output is correct |
23 |
Correct |
25 ms |
4444 KB |
Output is correct |
24 |
Incorrect |
31 ms |
4444 KB |
Output isn't correct |
25 |
Halted |
0 ms |
0 KB |
- |