/**
_ _ __ _ _ _ _ _ _
|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];
ll suff[N_MAX + 2];
ld dp[N_MAX + 2];
int cnt[N_MAX + 2];
struct Line {
ld a, b;
ld f (int x) {
return a * x + b;
}
};
Line lines[N_MAX + 2];
deque <int> rel;
bool check (const Line &l1, const Line &l2, const Line &l3) {
return (l2.b - l1.b) * (l2.a - l3.a) < (l3.b - l2.b) * (l1.a - l2.a);
}
void add_line (int j) {
while ((int) rel.size() >= 2
&& check(lines[rel.end()[-2]], lines[rel.back()], lines[j]) == false) {
rel.pop_back();
}
rel.push_back(j);
}
pair <ld, int> query (int i) {
if (rel.empty() == true) {
return make_pair(INF, 0);
}
while ((int) rel.size() >= 2
&& make_pair(lines[rel[0]].f(i), cnt[rel[0]]) >=
make_pair(lines[rel[1]].f(i), cnt[rel[1]])) {
rel.pop_front();
}
return make_pair(lines[rel.front()].f(i) - suff[i + 1], cnt[rel.front()]);
}
ll solve (ld pen) {
rel.clear();
deque <int> dq;
int curr = 0;
for (int i = 1; i <= N; i++) {
for (int j = curr; j < min(A[i] - i, i); j++) {
// dp[j] + sum(A[i1] + i1 - j | i <= i1 <= i2)
// dp[j] + suff[i] - suff[i2 + 1] - j * i2 + j * (i - 1)
lines[j].a = -j;
lines[j].b = dp[j] + suff[i] + (ll) j * (i - 1);
add_line(j);
}
curr = min(A[i] - i, i);
while (dq.empty() == false && dp[i - 1] < dp[dq.back()]) {
dq.pop_back();
}
dq.push_back(i - 1);
while (dq.empty() == false && dq.front() < curr) {
dq.pop_front();
}
tie(dp[i], cnt[i]) = query(i);
if (dq.empty() == false) {
int j = dq.front();
if (make_pair(dp[j], cnt[j]) < make_pair(dp[i], cnt[i])) {
dp[i] = dp[j]; cnt[i] = cnt[j];
}
}
dp[i] += pen; cnt[i]++;
}
rel.clear();
return (ll) round(dp[N] - pen * K);
}
int main () {
// ios_base::sync_with_stdio(false);
// cin.tie(0);
cout << fixed << setprecision(2);
mt19937 rnd(time(0));
cin >> N >> K;
// N = 20; K = rnd() % N + 1;
string S;
cin >> S;
// for (int i = 0; i < N * 2; i++) {
// S += (i < N ? 'A' : 'B');
// }
// shuffle(S.begin(), S.end(), rnd);
// cerr << N << " " << K << "\n";
// cerr << S << "\n";
for (int i = 0, j = 0; i < N * 2; i++) {
if (S[i] == 'A') {
A[++j] = i + 1;
}
}
for (int i = N; i >= 1; i--) {
suff[i] = suff[i + 1] + A[i] - i;
}
ld l = 0, r = LLONG_MAX; int iter = 300;
while (iter--) {
ld mid = (l + r) / 2;
solve(mid);
if (cnt[N] <= K) {
r = mid;
} else {
l = mid + EPS;
}
}
cout << solve(l) << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
6604 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
6604 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
5 ms |
6744 KB |
Output is correct |
18 |
Correct |
8 ms |
6492 KB |
Output is correct |
19 |
Correct |
8 ms |
6492 KB |
Output is correct |
20 |
Correct |
7 ms |
2548 KB |
Output is correct |
21 |
Correct |
8 ms |
2396 KB |
Output is correct |
22 |
Correct |
7 ms |
2396 KB |
Output is correct |
23 |
Correct |
6 ms |
2396 KB |
Output is correct |
24 |
Correct |
9 ms |
6632 KB |
Output is correct |
25 |
Correct |
7 ms |
6492 KB |
Output is correct |
26 |
Correct |
7 ms |
6492 KB |
Output is correct |
27 |
Correct |
8 ms |
6492 KB |
Output is correct |
28 |
Correct |
8 ms |
6628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
6604 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
5 ms |
6744 KB |
Output is correct |
18 |
Correct |
8 ms |
6492 KB |
Output is correct |
19 |
Correct |
8 ms |
6492 KB |
Output is correct |
20 |
Correct |
7 ms |
2548 KB |
Output is correct |
21 |
Correct |
8 ms |
2396 KB |
Output is correct |
22 |
Correct |
7 ms |
2396 KB |
Output is correct |
23 |
Correct |
6 ms |
2396 KB |
Output is correct |
24 |
Correct |
9 ms |
6632 KB |
Output is correct |
25 |
Correct |
7 ms |
6492 KB |
Output is correct |
26 |
Correct |
7 ms |
6492 KB |
Output is correct |
27 |
Correct |
8 ms |
6492 KB |
Output is correct |
28 |
Correct |
8 ms |
6628 KB |
Output is correct |
29 |
Correct |
74 ms |
6744 KB |
Output is correct |
30 |
Correct |
79 ms |
6744 KB |
Output is correct |
31 |
Correct |
80 ms |
6748 KB |
Output is correct |
32 |
Correct |
63 ms |
2816 KB |
Output is correct |
33 |
Correct |
75 ms |
2808 KB |
Output is correct |
34 |
Correct |
77 ms |
2652 KB |
Output is correct |
35 |
Correct |
56 ms |
2648 KB |
Output is correct |
36 |
Correct |
108 ms |
7000 KB |
Output is correct |
37 |
Correct |
100 ms |
6748 KB |
Output is correct |
38 |
Correct |
64 ms |
6744 KB |
Output is correct |
39 |
Correct |
79 ms |
6800 KB |
Output is correct |
40 |
Correct |
71 ms |
6804 KB |
Output is correct |
41 |
Correct |
88 ms |
6744 KB |
Output is correct |
42 |
Correct |
44 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
6604 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
5 ms |
6744 KB |
Output is correct |
18 |
Correct |
8 ms |
6492 KB |
Output is correct |
19 |
Correct |
8 ms |
6492 KB |
Output is correct |
20 |
Correct |
7 ms |
2548 KB |
Output is correct |
21 |
Correct |
8 ms |
2396 KB |
Output is correct |
22 |
Correct |
7 ms |
2396 KB |
Output is correct |
23 |
Correct |
6 ms |
2396 KB |
Output is correct |
24 |
Correct |
9 ms |
6632 KB |
Output is correct |
25 |
Correct |
7 ms |
6492 KB |
Output is correct |
26 |
Correct |
7 ms |
6492 KB |
Output is correct |
27 |
Correct |
8 ms |
6492 KB |
Output is correct |
28 |
Correct |
8 ms |
6628 KB |
Output is correct |
29 |
Correct |
74 ms |
6744 KB |
Output is correct |
30 |
Correct |
79 ms |
6744 KB |
Output is correct |
31 |
Correct |
80 ms |
6748 KB |
Output is correct |
32 |
Correct |
63 ms |
2816 KB |
Output is correct |
33 |
Correct |
75 ms |
2808 KB |
Output is correct |
34 |
Correct |
77 ms |
2652 KB |
Output is correct |
35 |
Correct |
56 ms |
2648 KB |
Output is correct |
36 |
Correct |
108 ms |
7000 KB |
Output is correct |
37 |
Correct |
100 ms |
6748 KB |
Output is correct |
38 |
Correct |
64 ms |
6744 KB |
Output is correct |
39 |
Correct |
79 ms |
6800 KB |
Output is correct |
40 |
Correct |
71 ms |
6804 KB |
Output is correct |
41 |
Correct |
88 ms |
6744 KB |
Output is correct |
42 |
Correct |
44 ms |
2652 KB |
Output is correct |
43 |
Correct |
978 ms |
6588 KB |
Output is correct |
44 |
Correct |
1623 ms |
14024 KB |
Output is correct |
45 |
Correct |
1645 ms |
13860 KB |
Output is correct |
46 |
Correct |
1241 ms |
12008 KB |
Output is correct |
47 |
Correct |
1333 ms |
11860 KB |
Output is correct |
48 |
Correct |
1268 ms |
11868 KB |
Output is correct |
49 |
Correct |
1096 ms |
12008 KB |
Output is correct |
50 |
Correct |
2208 ms |
14024 KB |
Output is correct |
51 |
Correct |
2299 ms |
14084 KB |
Output is correct |
52 |
Correct |
1243 ms |
14056 KB |
Output is correct |
53 |
Correct |
1250 ms |
14052 KB |
Output is correct |
54 |
Correct |
2245 ms |
14276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
6604 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
5 ms |
6744 KB |
Output is correct |
18 |
Correct |
8 ms |
6492 KB |
Output is correct |
19 |
Correct |
8 ms |
6492 KB |
Output is correct |
20 |
Correct |
7 ms |
2548 KB |
Output is correct |
21 |
Correct |
8 ms |
2396 KB |
Output is correct |
22 |
Correct |
7 ms |
2396 KB |
Output is correct |
23 |
Correct |
6 ms |
2396 KB |
Output is correct |
24 |
Correct |
9 ms |
6632 KB |
Output is correct |
25 |
Correct |
7 ms |
6492 KB |
Output is correct |
26 |
Correct |
7 ms |
6492 KB |
Output is correct |
27 |
Correct |
8 ms |
6492 KB |
Output is correct |
28 |
Correct |
8 ms |
6628 KB |
Output is correct |
29 |
Correct |
74 ms |
6744 KB |
Output is correct |
30 |
Correct |
79 ms |
6744 KB |
Output is correct |
31 |
Correct |
80 ms |
6748 KB |
Output is correct |
32 |
Correct |
63 ms |
2816 KB |
Output is correct |
33 |
Correct |
75 ms |
2808 KB |
Output is correct |
34 |
Correct |
77 ms |
2652 KB |
Output is correct |
35 |
Correct |
56 ms |
2648 KB |
Output is correct |
36 |
Correct |
108 ms |
7000 KB |
Output is correct |
37 |
Correct |
100 ms |
6748 KB |
Output is correct |
38 |
Correct |
64 ms |
6744 KB |
Output is correct |
39 |
Correct |
79 ms |
6800 KB |
Output is correct |
40 |
Correct |
71 ms |
6804 KB |
Output is correct |
41 |
Correct |
88 ms |
6744 KB |
Output is correct |
42 |
Correct |
44 ms |
2652 KB |
Output is correct |
43 |
Correct |
978 ms |
6588 KB |
Output is correct |
44 |
Correct |
1623 ms |
14024 KB |
Output is correct |
45 |
Correct |
1645 ms |
13860 KB |
Output is correct |
46 |
Correct |
1241 ms |
12008 KB |
Output is correct |
47 |
Correct |
1333 ms |
11860 KB |
Output is correct |
48 |
Correct |
1268 ms |
11868 KB |
Output is correct |
49 |
Correct |
1096 ms |
12008 KB |
Output is correct |
50 |
Correct |
2208 ms |
14024 KB |
Output is correct |
51 |
Correct |
2299 ms |
14084 KB |
Output is correct |
52 |
Correct |
1243 ms |
14056 KB |
Output is correct |
53 |
Correct |
1250 ms |
14052 KB |
Output is correct |
54 |
Correct |
2245 ms |
14276 KB |
Output is correct |
55 |
Execution timed out |
7026 ms |
48244 KB |
Time limit exceeded |
56 |
Halted |
0 ms |
0 KB |
- |