#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010, n = 15;
int cnt[n], pre[maxn][n][n], suf[maxn][n][n];
string str;
long long f[1 << n];
vector<int> pos[n];
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> str;
fill(pos, pos + n, vector<int>{-1});
for (int i = 0; i < str.size(); i++) {
pos[str[i] - 'A'].push_back(i);
if (i) memcpy(pre[i], pre[i - 1], sizeof(pre[i]));
for (int j = 0; j < n; j++) pre[i][str[i] - 'A'][j] += cnt[j];
cnt[str[i] - 'A']++;
}
fill(cnt, cnt + n, 0);
for (int i = str.size() - 1; ~i; i--) {
memcpy(suf[i], suf[i + 1], sizeof(suf[i]));
for (int j = 0; j < n; j++) suf[i][str[i] - 'A'][j] += cnt[j];
cnt[str[i] - 'A']++;
}
fill(f + 1, f + (1 << n), 1e18);
for (int S = 0; S < 1 << n; S++) {
for (int i = 0; i < n; i++) if (!(S >> i & 1)) {
auto calc = [&](int p) {
int r = pos[i].size() - p - 1, T = S;
long long s = (1LL * p * (p - 1) + 1LL * r * (r - 1)) / 2;
p = pos[i][p];
for (int T = S, j; T; T ^= 1 << j) {
j = __builtin_ctz(T);
if (~p) s += 2 * pre[p][i][j];
s += 2 * suf[p + 1][i][j];
}
return s;
};
int l = 1, r = pos[i].size() - 1, p = 0;
while (l <= r) {
int mid = (l + r) / 2;
calc(mid) < calc(mid - 1) ? l = (p = mid) + 1 : r = mid - 1;
}
f[S | (1 << i)] = min(f[S | (1 << i)], f[S] + calc(p));
}
}
cout << f[(1 << n) - 1] / 2 << (f[(1 << n) - 1] % 2 ? ".5" : "") << "\n";
return 0;
}
Compilation message
passes.cpp: In function 'int main()':
passes.cpp:14:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
14 | for (int i = 0; i < str.size(); i++) {
| ~~^~~~~~~~~~~~
passes.cpp: In lambda function:
passes.cpp:30:48: warning: unused variable 'T' [-Wunused-variable]
30 | int r = pos[i].size() - p - 1, T = S;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
2132 KB |
Output is correct |
2 |
Correct |
10 ms |
468 KB |
Output is correct |
3 |
Correct |
11 ms |
468 KB |
Output is correct |
4 |
Correct |
11 ms |
596 KB |
Output is correct |
5 |
Correct |
18 ms |
2360 KB |
Output is correct |
6 |
Correct |
84 ms |
139712 KB |
Output is correct |
7 |
Correct |
99 ms |
166540 KB |
Output is correct |
8 |
Correct |
111 ms |
177384 KB |
Output is correct |
9 |
Correct |
107 ms |
177392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
596 KB |
Output is correct |
2 |
Correct |
16 ms |
768 KB |
Output is correct |
3 |
Correct |
27 ms |
772 KB |
Output is correct |
4 |
Correct |
26 ms |
764 KB |
Output is correct |
5 |
Correct |
17 ms |
724 KB |
Output is correct |
6 |
Correct |
13 ms |
608 KB |
Output is correct |
7 |
Correct |
21 ms |
724 KB |
Output is correct |
8 |
Correct |
19 ms |
636 KB |
Output is correct |
9 |
Correct |
20 ms |
768 KB |
Output is correct |
10 |
Correct |
20 ms |
636 KB |
Output is correct |
11 |
Correct |
19 ms |
596 KB |
Output is correct |
12 |
Correct |
19 ms |
596 KB |
Output is correct |
13 |
Correct |
18 ms |
640 KB |
Output is correct |
14 |
Correct |
19 ms |
596 KB |
Output is correct |
15 |
Correct |
19 ms |
640 KB |
Output is correct |
16 |
Correct |
19 ms |
596 KB |
Output is correct |
17 |
Correct |
20 ms |
644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
596 KB |
Output is correct |
2 |
Correct |
16 ms |
768 KB |
Output is correct |
3 |
Correct |
27 ms |
772 KB |
Output is correct |
4 |
Correct |
26 ms |
764 KB |
Output is correct |
5 |
Correct |
17 ms |
724 KB |
Output is correct |
6 |
Correct |
13 ms |
608 KB |
Output is correct |
7 |
Correct |
21 ms |
724 KB |
Output is correct |
8 |
Correct |
19 ms |
636 KB |
Output is correct |
9 |
Correct |
20 ms |
768 KB |
Output is correct |
10 |
Correct |
20 ms |
636 KB |
Output is correct |
11 |
Correct |
19 ms |
596 KB |
Output is correct |
12 |
Correct |
19 ms |
596 KB |
Output is correct |
13 |
Correct |
18 ms |
640 KB |
Output is correct |
14 |
Correct |
19 ms |
596 KB |
Output is correct |
15 |
Correct |
19 ms |
640 KB |
Output is correct |
16 |
Correct |
19 ms |
596 KB |
Output is correct |
17 |
Correct |
20 ms |
644 KB |
Output is correct |
18 |
Correct |
11 ms |
600 KB |
Output is correct |
19 |
Correct |
15 ms |
768 KB |
Output is correct |
20 |
Correct |
35 ms |
756 KB |
Output is correct |
21 |
Correct |
34 ms |
760 KB |
Output is correct |
22 |
Correct |
19 ms |
724 KB |
Output is correct |
23 |
Correct |
14 ms |
596 KB |
Output is correct |
24 |
Correct |
21 ms |
724 KB |
Output is correct |
25 |
Correct |
18 ms |
596 KB |
Output is correct |
26 |
Correct |
19 ms |
596 KB |
Output is correct |
27 |
Correct |
18 ms |
636 KB |
Output is correct |
28 |
Correct |
20 ms |
596 KB |
Output is correct |
29 |
Correct |
19 ms |
596 KB |
Output is correct |
30 |
Correct |
19 ms |
596 KB |
Output is correct |
31 |
Correct |
20 ms |
644 KB |
Output is correct |
32 |
Correct |
21 ms |
640 KB |
Output is correct |
33 |
Correct |
20 ms |
596 KB |
Output is correct |
34 |
Correct |
21 ms |
596 KB |
Output is correct |
35 |
Correct |
29 ms |
18260 KB |
Output is correct |
36 |
Correct |
26 ms |
18308 KB |
Output is correct |
37 |
Correct |
91 ms |
18284 KB |
Output is correct |
38 |
Correct |
80 ms |
18260 KB |
Output is correct |
39 |
Correct |
32 ms |
18260 KB |
Output is correct |
40 |
Correct |
75 ms |
18184 KB |
Output is correct |
41 |
Correct |
85 ms |
17924 KB |
Output is correct |
42 |
Correct |
87 ms |
17912 KB |
Output is correct |
43 |
Correct |
90 ms |
17916 KB |
Output is correct |
44 |
Correct |
96 ms |
17908 KB |
Output is correct |
45 |
Correct |
85 ms |
17876 KB |
Output is correct |
46 |
Correct |
89 ms |
17916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
2132 KB |
Output is correct |
2 |
Correct |
10 ms |
468 KB |
Output is correct |
3 |
Correct |
11 ms |
468 KB |
Output is correct |
4 |
Correct |
11 ms |
596 KB |
Output is correct |
5 |
Correct |
18 ms |
2360 KB |
Output is correct |
6 |
Correct |
84 ms |
139712 KB |
Output is correct |
7 |
Correct |
99 ms |
166540 KB |
Output is correct |
8 |
Correct |
111 ms |
177384 KB |
Output is correct |
9 |
Correct |
107 ms |
177392 KB |
Output is correct |
10 |
Correct |
10 ms |
596 KB |
Output is correct |
11 |
Correct |
16 ms |
768 KB |
Output is correct |
12 |
Correct |
27 ms |
772 KB |
Output is correct |
13 |
Correct |
26 ms |
764 KB |
Output is correct |
14 |
Correct |
17 ms |
724 KB |
Output is correct |
15 |
Correct |
13 ms |
608 KB |
Output is correct |
16 |
Correct |
21 ms |
724 KB |
Output is correct |
17 |
Correct |
19 ms |
636 KB |
Output is correct |
18 |
Correct |
20 ms |
768 KB |
Output is correct |
19 |
Correct |
20 ms |
636 KB |
Output is correct |
20 |
Correct |
19 ms |
596 KB |
Output is correct |
21 |
Correct |
19 ms |
596 KB |
Output is correct |
22 |
Correct |
18 ms |
640 KB |
Output is correct |
23 |
Correct |
19 ms |
596 KB |
Output is correct |
24 |
Correct |
19 ms |
640 KB |
Output is correct |
25 |
Correct |
19 ms |
596 KB |
Output is correct |
26 |
Correct |
20 ms |
644 KB |
Output is correct |
27 |
Correct |
11 ms |
600 KB |
Output is correct |
28 |
Correct |
15 ms |
768 KB |
Output is correct |
29 |
Correct |
35 ms |
756 KB |
Output is correct |
30 |
Correct |
34 ms |
760 KB |
Output is correct |
31 |
Correct |
19 ms |
724 KB |
Output is correct |
32 |
Correct |
14 ms |
596 KB |
Output is correct |
33 |
Correct |
21 ms |
724 KB |
Output is correct |
34 |
Correct |
18 ms |
596 KB |
Output is correct |
35 |
Correct |
19 ms |
596 KB |
Output is correct |
36 |
Correct |
18 ms |
636 KB |
Output is correct |
37 |
Correct |
20 ms |
596 KB |
Output is correct |
38 |
Correct |
19 ms |
596 KB |
Output is correct |
39 |
Correct |
19 ms |
596 KB |
Output is correct |
40 |
Correct |
20 ms |
644 KB |
Output is correct |
41 |
Correct |
21 ms |
640 KB |
Output is correct |
42 |
Correct |
20 ms |
596 KB |
Output is correct |
43 |
Correct |
21 ms |
596 KB |
Output is correct |
44 |
Correct |
29 ms |
18260 KB |
Output is correct |
45 |
Correct |
26 ms |
18308 KB |
Output is correct |
46 |
Correct |
91 ms |
18284 KB |
Output is correct |
47 |
Correct |
80 ms |
18260 KB |
Output is correct |
48 |
Correct |
32 ms |
18260 KB |
Output is correct |
49 |
Correct |
75 ms |
18184 KB |
Output is correct |
50 |
Correct |
85 ms |
17924 KB |
Output is correct |
51 |
Correct |
87 ms |
17912 KB |
Output is correct |
52 |
Correct |
90 ms |
17916 KB |
Output is correct |
53 |
Correct |
96 ms |
17908 KB |
Output is correct |
54 |
Correct |
85 ms |
17876 KB |
Output is correct |
55 |
Correct |
89 ms |
17916 KB |
Output is correct |
56 |
Correct |
13 ms |
612 KB |
Output is correct |
57 |
Correct |
28 ms |
628 KB |
Output is correct |
58 |
Correct |
20 ms |
2176 KB |
Output is correct |
59 |
Correct |
11 ms |
468 KB |
Output is correct |
60 |
Correct |
10 ms |
468 KB |
Output is correct |
61 |
Correct |
11 ms |
600 KB |
Output is correct |
62 |
Correct |
16 ms |
2352 KB |
Output is correct |
63 |
Correct |
83 ms |
139616 KB |
Output is correct |
64 |
Correct |
97 ms |
166564 KB |
Output is correct |
65 |
Correct |
109 ms |
177456 KB |
Output is correct |
66 |
Correct |
107 ms |
177436 KB |
Output is correct |
67 |
Correct |
10 ms |
600 KB |
Output is correct |
68 |
Correct |
12 ms |
776 KB |
Output is correct |
69 |
Correct |
25 ms |
724 KB |
Output is correct |
70 |
Correct |
25 ms |
768 KB |
Output is correct |
71 |
Correct |
22 ms |
768 KB |
Output is correct |
72 |
Correct |
13 ms |
596 KB |
Output is correct |
73 |
Correct |
22 ms |
724 KB |
Output is correct |
74 |
Correct |
22 ms |
596 KB |
Output is correct |
75 |
Correct |
24 ms |
596 KB |
Output is correct |
76 |
Correct |
20 ms |
596 KB |
Output is correct |
77 |
Correct |
18 ms |
640 KB |
Output is correct |
78 |
Correct |
19 ms |
636 KB |
Output is correct |
79 |
Correct |
18 ms |
640 KB |
Output is correct |
80 |
Correct |
18 ms |
644 KB |
Output is correct |
81 |
Correct |
19 ms |
644 KB |
Output is correct |
82 |
Correct |
18 ms |
596 KB |
Output is correct |
83 |
Correct |
21 ms |
596 KB |
Output is correct |
84 |
Correct |
25 ms |
18260 KB |
Output is correct |
85 |
Correct |
28 ms |
18336 KB |
Output is correct |
86 |
Correct |
107 ms |
18280 KB |
Output is correct |
87 |
Correct |
79 ms |
18260 KB |
Output is correct |
88 |
Correct |
33 ms |
18260 KB |
Output is correct |
89 |
Correct |
76 ms |
18188 KB |
Output is correct |
90 |
Correct |
87 ms |
17920 KB |
Output is correct |
91 |
Correct |
98 ms |
17916 KB |
Output is correct |
92 |
Correct |
89 ms |
17916 KB |
Output is correct |
93 |
Correct |
87 ms |
17808 KB |
Output is correct |
94 |
Correct |
89 ms |
17920 KB |
Output is correct |
95 |
Correct |
86 ms |
17912 KB |
Output is correct |
96 |
Correct |
257 ms |
177288 KB |
Output is correct |
97 |
Correct |
25 ms |
596 KB |
Output is correct |
98 |
Correct |
221 ms |
177308 KB |
Output is correct |
99 |
Correct |
120 ms |
177356 KB |
Output is correct |
100 |
Correct |
22 ms |
596 KB |
Output is correct |
101 |
Correct |
225 ms |
177324 KB |
Output is correct |
102 |
Correct |
224 ms |
177264 KB |
Output is correct |
103 |
Correct |
246 ms |
176640 KB |
Output is correct |
104 |
Correct |
259 ms |
176640 KB |
Output is correct |
105 |
Correct |
230 ms |
176728 KB |
Output is correct |
106 |
Correct |
261 ms |
176700 KB |
Output is correct |
107 |
Correct |
248 ms |
176668 KB |
Output is correct |
108 |
Correct |
249 ms |
176472 KB |
Output is correct |
109 |
Correct |
259 ms |
176728 KB |
Output is correct |