// DP approach with ternary search
// O(2^T * T^2 * log N + N * T)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr ll INF = numeric_limits<ll>::max() / 10;
int type_cnt;
int n;
vector<ll> cnt;
vector<vector<ll>> l, r; // t, pos -> number of passengers of type t left/right of pos
vector<vector<vector<ll>>> l2, r2; // A, B, i -> cost caused by group B boarding before A, to anyone up to the i-th member of group A and boarding from the left/right
vector<vector<int>> groups; // A -> positions of all members of group A, sorted
ll get_cost(size_t already_boarded, int next, int split)
{
ll result = 0;
for (int t = 0; t < type_cnt; ++t) {
ll tmp = l2[next][t][split] + r2[next][t][split];
if (already_boarded & (1ll << t)) {
result += 2 * tmp;
} else if (t == next) {
result += tmp;
}
}
return result;
}
// Cost of the next group boarding, if the given set of groups has already boarded before.
ll get_cost(size_t already_boarded, int next)
{
int mini = 0, maxi = groups[next].size() + 1;
while (maxi - mini > 1) {
int m1 = (maxi + mini) / 2 - 1;
int m2 = m1 + 1;
ll c1 = get_cost(already_boarded, next, m1);
ll c2 = get_cost(already_boarded, next, m2);
if (c1 < c2) maxi = m2;
else mini = m1 + 1;
}
ll result = get_cost(already_boarded, next, mini);
return result;
}
ll calc_costs()
{
vector<ll> dp(1ll << type_cnt, INF);
dp[0] = 0;
for (int i = 1; i < 1ll << type_cnt; ++i) {
for (int t = 0; t < type_cnt; ++t) {
if (!(i & (1ll << t))) continue;
size_t parent = i & ~(1ll << t);
dp[i] = min(dp[i], dp[parent] + get_cost(parent, t));
}
}
return dp[(1ll << type_cnt) - 1];
}
int main()
{
string s;
cin >> s;
n = s.size();
map<char, char> types;
for (char& c: s) {
if (!types.count(c)) {
char id = static_cast<char>(types.size());
types[c] = id;
}
c = types[c];
}
type_cnt = types.size();
cnt.assign(type_cnt, 0);
for (char c: s) ++cnt[c];
groups.resize(type_cnt);
for (int i = 0; i < n; ++i) {
groups[s[i]].push_back(i);
}
l = r = vector<vector<ll>>(type_cnt, vector<ll>(n));
vector<ll> tmp_cnt(type_cnt, 0);
for (size_t i = 0; i < n; ++i) {
for (size_t j = 0; j < type_cnt; ++j) {
l[j][i] = tmp_cnt[j];
r[j][i] = cnt[j] - tmp_cnt[j];
}
++tmp_cnt[s[i]];
--r[s[i]][i];
}
vector<vector<ll>> tmp_costs_l, tmp_costs_r;
tmp_costs_l = tmp_costs_r = vector<vector<ll>>(type_cnt, vector<ll>(type_cnt, 0));
l2 = r2 = vector<vector<vector<ll>>>(type_cnt, vector<vector<ll>>(type_cnt, {0}));
for (size_t i = 0; i < n; ++i) {
for (int j = 0; j < type_cnt; ++j) {
tmp_costs_l[s[i]][j] += l[j][i];
tmp_costs_r[s[n - i - 1]][j] += r[j][n - i - 1];
}
for (int k = 0; k < type_cnt; ++k) {
l2[s[i]][k].push_back(tmp_costs_l[s[i]][k]);
r2[s[n - i - 1]][k].push_back(tmp_costs_r[s[n - i - 1]][k]);
}
}
for (int i = 0; i < type_cnt; ++i) {
for (int j = 0; j < type_cnt; ++j) {
reverse(r2[i][j].begin(), r2[i][j].end());
}
}
auto result = calc_costs();
cout << result / 2 << ((result % 2) ? ".5" : "") << "\n";
}
Compilation message
passes.cpp: In function 'int main()':
passes.cpp:87:26: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
87 | for (size_t i = 0; i < n; ++i) {
| ~~^~~
passes.cpp:88:30: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
88 | for (size_t j = 0; j < type_cnt; ++j) {
| ~~^~~~~~~~~~
passes.cpp:99:26: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
99 | for (size_t i = 0; i < n; ++i) {
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
240 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
9 ms |
4344 KB |
Output is correct |
7 |
Correct |
10 ms |
4716 KB |
Output is correct |
8 |
Correct |
10 ms |
5352 KB |
Output is correct |
9 |
Correct |
11 ms |
5308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
244 KB |
Output is correct |
2 |
Correct |
1 ms |
236 KB |
Output is correct |
3 |
Correct |
1 ms |
360 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
376 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
252 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
244 KB |
Output is correct |
2 |
Correct |
1 ms |
236 KB |
Output is correct |
3 |
Correct |
1 ms |
360 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
376 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
252 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
256 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
360 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
256 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
216 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
216 KB |
Output is correct |
29 |
Correct |
1 ms |
256 KB |
Output is correct |
30 |
Correct |
1 ms |
224 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
2 ms |
852 KB |
Output is correct |
36 |
Correct |
2 ms |
852 KB |
Output is correct |
37 |
Correct |
10 ms |
4084 KB |
Output is correct |
38 |
Correct |
10 ms |
3572 KB |
Output is correct |
39 |
Correct |
8 ms |
4320 KB |
Output is correct |
40 |
Correct |
9 ms |
3540 KB |
Output is correct |
41 |
Correct |
8 ms |
4288 KB |
Output is correct |
42 |
Correct |
11 ms |
4164 KB |
Output is correct |
43 |
Correct |
10 ms |
3964 KB |
Output is correct |
44 |
Correct |
10 ms |
3924 KB |
Output is correct |
45 |
Correct |
8 ms |
4180 KB |
Output is correct |
46 |
Correct |
13 ms |
4192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
240 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
9 ms |
4344 KB |
Output is correct |
7 |
Correct |
10 ms |
4716 KB |
Output is correct |
8 |
Correct |
10 ms |
5352 KB |
Output is correct |
9 |
Correct |
11 ms |
5308 KB |
Output is correct |
10 |
Correct |
1 ms |
244 KB |
Output is correct |
11 |
Correct |
1 ms |
236 KB |
Output is correct |
12 |
Correct |
1 ms |
360 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
376 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
252 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
256 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
256 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
360 KB |
Output is correct |
31 |
Correct |
1 ms |
384 KB |
Output is correct |
32 |
Correct |
1 ms |
256 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
216 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
216 KB |
Output is correct |
38 |
Correct |
1 ms |
256 KB |
Output is correct |
39 |
Correct |
1 ms |
224 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
1 ms |
212 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
2 ms |
852 KB |
Output is correct |
45 |
Correct |
2 ms |
852 KB |
Output is correct |
46 |
Correct |
10 ms |
4084 KB |
Output is correct |
47 |
Correct |
10 ms |
3572 KB |
Output is correct |
48 |
Correct |
8 ms |
4320 KB |
Output is correct |
49 |
Correct |
9 ms |
3540 KB |
Output is correct |
50 |
Correct |
8 ms |
4288 KB |
Output is correct |
51 |
Correct |
11 ms |
4164 KB |
Output is correct |
52 |
Correct |
10 ms |
3964 KB |
Output is correct |
53 |
Correct |
10 ms |
3924 KB |
Output is correct |
54 |
Correct |
8 ms |
4180 KB |
Output is correct |
55 |
Correct |
13 ms |
4192 KB |
Output is correct |
56 |
Correct |
1 ms |
224 KB |
Output is correct |
57 |
Correct |
73 ms |
608 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
0 ms |
212 KB |
Output is correct |
60 |
Correct |
0 ms |
212 KB |
Output is correct |
61 |
Correct |
1 ms |
212 KB |
Output is correct |
62 |
Correct |
1 ms |
344 KB |
Output is correct |
63 |
Correct |
9 ms |
4384 KB |
Output is correct |
64 |
Correct |
11 ms |
4752 KB |
Output is correct |
65 |
Correct |
11 ms |
5308 KB |
Output is correct |
66 |
Correct |
12 ms |
5392 KB |
Output is correct |
67 |
Correct |
1 ms |
212 KB |
Output is correct |
68 |
Correct |
1 ms |
256 KB |
Output is correct |
69 |
Correct |
1 ms |
340 KB |
Output is correct |
70 |
Correct |
1 ms |
340 KB |
Output is correct |
71 |
Correct |
1 ms |
340 KB |
Output is correct |
72 |
Correct |
1 ms |
212 KB |
Output is correct |
73 |
Correct |
1 ms |
212 KB |
Output is correct |
74 |
Correct |
1 ms |
256 KB |
Output is correct |
75 |
Correct |
1 ms |
212 KB |
Output is correct |
76 |
Correct |
1 ms |
256 KB |
Output is correct |
77 |
Correct |
1 ms |
212 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
1 ms |
212 KB |
Output is correct |
80 |
Correct |
1 ms |
256 KB |
Output is correct |
81 |
Correct |
1 ms |
308 KB |
Output is correct |
82 |
Correct |
1 ms |
212 KB |
Output is correct |
83 |
Correct |
1 ms |
212 KB |
Output is correct |
84 |
Correct |
2 ms |
852 KB |
Output is correct |
85 |
Correct |
2 ms |
852 KB |
Output is correct |
86 |
Correct |
10 ms |
4096 KB |
Output is correct |
87 |
Correct |
9 ms |
3620 KB |
Output is correct |
88 |
Correct |
8 ms |
4412 KB |
Output is correct |
89 |
Correct |
10 ms |
3536 KB |
Output is correct |
90 |
Correct |
10 ms |
4316 KB |
Output is correct |
91 |
Correct |
9 ms |
4180 KB |
Output is correct |
92 |
Correct |
11 ms |
4096 KB |
Output is correct |
93 |
Correct |
10 ms |
3976 KB |
Output is correct |
94 |
Correct |
10 ms |
4200 KB |
Output is correct |
95 |
Correct |
9 ms |
4180 KB |
Output is correct |
96 |
Correct |
500 ms |
52956 KB |
Output is correct |
97 |
Correct |
31 ms |
468 KB |
Output is correct |
98 |
Correct |
503 ms |
51824 KB |
Output is correct |
99 |
Correct |
153 ms |
53988 KB |
Output is correct |
100 |
Correct |
37 ms |
640 KB |
Output is correct |
101 |
Correct |
407 ms |
52512 KB |
Output is correct |
102 |
Correct |
412 ms |
54148 KB |
Output is correct |
103 |
Correct |
352 ms |
54396 KB |
Output is correct |
104 |
Correct |
409 ms |
55704 KB |
Output is correct |
105 |
Correct |
384 ms |
54304 KB |
Output is correct |
106 |
Correct |
378 ms |
54504 KB |
Output is correct |
107 |
Correct |
533 ms |
54368 KB |
Output is correct |
108 |
Correct |
248 ms |
49712 KB |
Output is correct |
109 |
Correct |
460 ms |
57524 KB |
Output is correct |