#include <bits/stdc++.h>
#define ll long long
#define plll pair<pll, int>
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;
ll N, K;
ll in[1000006], suf[1000006];
pll dp[1000006];
char c[2000006];
pll f(plll p, ll x)
{
auto [a, b] = p;
return pll(a.F * x + a.S, b);
}
ll check(pll p1, pll p2, pll p3)
{
return (p1.S - p2.S) * (p3.F - p2.F) >= (p2.S - p3.S) * (p2.F - p1.F);
}
pll operator+(pll p, pll q)
{
return pll(p.F + q.F, p.S + q.S);
}
void calc(ll pen)
{
for (int i = 1; i <= N; i++)
dp[i] = pll(1e18, 0);
deque<plll> hull, free;
auto update = [&](plll p)
{
while (hull.size() >= 2 && check(hull[(int)hull.size() - 2].F, hull.back().F, p.F))
hull.pop_back();
hull.emplace_back(p);
};
free.emplace_back(plll(dp[0], 0));
for (int i = 1; i <= N; i++)
{
// dp[j][k - 1] + (i - in[j]) * (N - j) - (suf[in[j] + 1] - suf[i + 1])
// dp[j][k - 1] + i * N - i * j - in[j] * N + in[j] * j - suf[in[j] + 1] + suf[i + 1]
// dp[j][k - 1] - in[j] * N + in[j] * j - suf[in[j] + 1]
// - i * j
// + i * N + suf[i + 1]
while (!free.empty() && in[free.front().S] < i)
{
int j = free.front().S;
update(plll(pll(-j, dp[j].F - in[j] * N + in[j] * j - suf[in[j] + 1]), dp[j].S));
free.pop_front();
}
while (hull.size() >= 2 && f(hull[0], i) > f(hull[1], i))
hull.pop_front();
if (!hull.empty())
dp[i] = min(dp[i], f(hull[0], i) + pll(i * N + suf[i + 1] + pen, -1));
if (!free.empty())
dp[i] = min(dp[i], free[0].F + pll(pen, -1));
free.emplace_back(plll(dp[i], i));
// cout << dp[i][k] << " \n"[i == N];
}
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> N >> K;
for (int i = 1; i <= 2 * N; i++)
cin >> c[i];
for (int i = 1, j = 0, k = 1; i <= N; i++, k++)
{
while (c[k] != 'B')
j++, k++;
in[i - 1] = j;
}
for (int i = 0; i < N; i++)
in[i] = max((ll)i, in[i]);
for (int i = 1, j = N, k = 1; i <= N; i++, k++)
{
while (c[k] != 'A')
j--, k++;
suf[i] = j;
}
for (int i = N; i >= 0; i--)
suf[i] += suf[i + 1];
ll l = 0, r = 2e12;
while(l < r)
{
ll mid = (l + r + 1) / 2;
calc(mid);
if(-dp[N].S < K)
r = mid - 1;
else
l = mid;
}
calc(l);
cout << dp[N].F - l * K << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
0 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
0 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
372 KB |
Output is correct |
27 |
Correct |
1 ms |
324 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
0 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
372 KB |
Output is correct |
27 |
Correct |
1 ms |
324 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
7 ms |
464 KB |
Output is correct |
30 |
Correct |
9 ms |
468 KB |
Output is correct |
31 |
Correct |
9 ms |
468 KB |
Output is correct |
32 |
Correct |
6 ms |
472 KB |
Output is correct |
33 |
Correct |
6 ms |
468 KB |
Output is correct |
34 |
Correct |
6 ms |
584 KB |
Output is correct |
35 |
Correct |
6 ms |
468 KB |
Output is correct |
36 |
Correct |
9 ms |
468 KB |
Output is correct |
37 |
Correct |
9 ms |
468 KB |
Output is correct |
38 |
Correct |
6 ms |
468 KB |
Output is correct |
39 |
Correct |
8 ms |
468 KB |
Output is correct |
40 |
Correct |
7 ms |
468 KB |
Output is correct |
41 |
Correct |
7 ms |
468 KB |
Output is correct |
42 |
Correct |
5 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
0 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
372 KB |
Output is correct |
27 |
Correct |
1 ms |
324 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
7 ms |
464 KB |
Output is correct |
30 |
Correct |
9 ms |
468 KB |
Output is correct |
31 |
Correct |
9 ms |
468 KB |
Output is correct |
32 |
Correct |
6 ms |
472 KB |
Output is correct |
33 |
Correct |
6 ms |
468 KB |
Output is correct |
34 |
Correct |
6 ms |
584 KB |
Output is correct |
35 |
Correct |
6 ms |
468 KB |
Output is correct |
36 |
Correct |
9 ms |
468 KB |
Output is correct |
37 |
Correct |
9 ms |
468 KB |
Output is correct |
38 |
Correct |
6 ms |
468 KB |
Output is correct |
39 |
Correct |
8 ms |
468 KB |
Output is correct |
40 |
Correct |
7 ms |
468 KB |
Output is correct |
41 |
Correct |
7 ms |
468 KB |
Output is correct |
42 |
Correct |
5 ms |
596 KB |
Output is correct |
43 |
Correct |
110 ms |
2900 KB |
Output is correct |
44 |
Correct |
179 ms |
4636 KB |
Output is correct |
45 |
Correct |
177 ms |
4556 KB |
Output is correct |
46 |
Correct |
110 ms |
3852 KB |
Output is correct |
47 |
Correct |
116 ms |
5076 KB |
Output is correct |
48 |
Correct |
122 ms |
4892 KB |
Output is correct |
49 |
Correct |
128 ms |
4924 KB |
Output is correct |
50 |
Correct |
195 ms |
4648 KB |
Output is correct |
51 |
Correct |
198 ms |
4556 KB |
Output is correct |
52 |
Correct |
126 ms |
4896 KB |
Output is correct |
53 |
Correct |
123 ms |
4812 KB |
Output is correct |
54 |
Correct |
191 ms |
4636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
0 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
372 KB |
Output is correct |
27 |
Correct |
1 ms |
324 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
7 ms |
464 KB |
Output is correct |
30 |
Correct |
9 ms |
468 KB |
Output is correct |
31 |
Correct |
9 ms |
468 KB |
Output is correct |
32 |
Correct |
6 ms |
472 KB |
Output is correct |
33 |
Correct |
6 ms |
468 KB |
Output is correct |
34 |
Correct |
6 ms |
584 KB |
Output is correct |
35 |
Correct |
6 ms |
468 KB |
Output is correct |
36 |
Correct |
9 ms |
468 KB |
Output is correct |
37 |
Correct |
9 ms |
468 KB |
Output is correct |
38 |
Correct |
6 ms |
468 KB |
Output is correct |
39 |
Correct |
8 ms |
468 KB |
Output is correct |
40 |
Correct |
7 ms |
468 KB |
Output is correct |
41 |
Correct |
7 ms |
468 KB |
Output is correct |
42 |
Correct |
5 ms |
596 KB |
Output is correct |
43 |
Correct |
110 ms |
2900 KB |
Output is correct |
44 |
Correct |
179 ms |
4636 KB |
Output is correct |
45 |
Correct |
177 ms |
4556 KB |
Output is correct |
46 |
Correct |
110 ms |
3852 KB |
Output is correct |
47 |
Correct |
116 ms |
5076 KB |
Output is correct |
48 |
Correct |
122 ms |
4892 KB |
Output is correct |
49 |
Correct |
128 ms |
4924 KB |
Output is correct |
50 |
Correct |
195 ms |
4648 KB |
Output is correct |
51 |
Correct |
198 ms |
4556 KB |
Output is correct |
52 |
Correct |
126 ms |
4896 KB |
Output is correct |
53 |
Correct |
123 ms |
4812 KB |
Output is correct |
54 |
Correct |
191 ms |
4636 KB |
Output is correct |
55 |
Correct |
1238 ms |
28328 KB |
Output is correct |
56 |
Correct |
1585 ms |
45544 KB |
Output is correct |
57 |
Correct |
1939 ms |
44668 KB |
Output is correct |
58 |
Correct |
1134 ms |
35528 KB |
Output is correct |
59 |
Correct |
1171 ms |
47656 KB |
Output is correct |
60 |
Correct |
1271 ms |
47640 KB |
Output is correct |
61 |
Correct |
1264 ms |
47580 KB |
Output is correct |
62 |
Correct |
2064 ms |
44600 KB |
Output is correct |
63 |
Correct |
2084 ms |
44660 KB |
Output is correct |
64 |
Correct |
1336 ms |
47664 KB |
Output is correct |
65 |
Correct |
1318 ms |
47612 KB |
Output is correct |
66 |
Correct |
2003 ms |
45028 KB |
Output is correct |
67 |
Correct |
1325 ms |
47724 KB |
Output is correct |