#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 |
9 ms |
596 KB |
Output is correct |
3 |
Correct |
10 ms |
596 KB |
Output is correct |
4 |
Correct |
9 ms |
596 KB |
Output is correct |
5 |
Correct |
15 ms |
2356 KB |
Output is correct |
6 |
Correct |
89 ms |
139608 KB |
Output is correct |
7 |
Correct |
116 ms |
166468 KB |
Output is correct |
8 |
Correct |
106 ms |
177416 KB |
Output is correct |
9 |
Correct |
105 ms |
177400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
604 KB |
Output is correct |
2 |
Correct |
13 ms |
772 KB |
Output is correct |
3 |
Correct |
27 ms |
724 KB |
Output is correct |
4 |
Correct |
32 ms |
752 KB |
Output is correct |
5 |
Correct |
20 ms |
724 KB |
Output is correct |
6 |
Correct |
14 ms |
604 KB |
Output is correct |
7 |
Correct |
21 ms |
724 KB |
Output is correct |
8 |
Correct |
18 ms |
596 KB |
Output is correct |
9 |
Correct |
19 ms |
596 KB |
Output is correct |
10 |
Correct |
19 ms |
596 KB |
Output is correct |
11 |
Correct |
19 ms |
640 KB |
Output is correct |
12 |
Correct |
20 ms |
636 KB |
Output is correct |
13 |
Correct |
19 ms |
596 KB |
Output is correct |
14 |
Correct |
19 ms |
644 KB |
Output is correct |
15 |
Correct |
21 ms |
636 KB |
Output is correct |
16 |
Correct |
20 ms |
640 KB |
Output is correct |
17 |
Correct |
24 ms |
636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
604 KB |
Output is correct |
2 |
Correct |
13 ms |
772 KB |
Output is correct |
3 |
Correct |
27 ms |
724 KB |
Output is correct |
4 |
Correct |
32 ms |
752 KB |
Output is correct |
5 |
Correct |
20 ms |
724 KB |
Output is correct |
6 |
Correct |
14 ms |
604 KB |
Output is correct |
7 |
Correct |
21 ms |
724 KB |
Output is correct |
8 |
Correct |
18 ms |
596 KB |
Output is correct |
9 |
Correct |
19 ms |
596 KB |
Output is correct |
10 |
Correct |
19 ms |
596 KB |
Output is correct |
11 |
Correct |
19 ms |
640 KB |
Output is correct |
12 |
Correct |
20 ms |
636 KB |
Output is correct |
13 |
Correct |
19 ms |
596 KB |
Output is correct |
14 |
Correct |
19 ms |
644 KB |
Output is correct |
15 |
Correct |
21 ms |
636 KB |
Output is correct |
16 |
Correct |
20 ms |
640 KB |
Output is correct |
17 |
Correct |
24 ms |
636 KB |
Output is correct |
18 |
Correct |
10 ms |
596 KB |
Output is correct |
19 |
Correct |
12 ms |
776 KB |
Output is correct |
20 |
Correct |
27 ms |
768 KB |
Output is correct |
21 |
Correct |
24 ms |
724 KB |
Output is correct |
22 |
Correct |
16 ms |
768 KB |
Output is correct |
23 |
Correct |
14 ms |
596 KB |
Output is correct |
24 |
Correct |
21 ms |
732 KB |
Output is correct |
25 |
Correct |
18 ms |
644 KB |
Output is correct |
26 |
Correct |
21 ms |
640 KB |
Output is correct |
27 |
Correct |
23 ms |
596 KB |
Output is correct |
28 |
Correct |
21 ms |
640 KB |
Output is correct |
29 |
Correct |
22 ms |
596 KB |
Output is correct |
30 |
Correct |
19 ms |
644 KB |
Output is correct |
31 |
Correct |
19 ms |
596 KB |
Output is correct |
32 |
Correct |
20 ms |
724 KB |
Output is correct |
33 |
Correct |
18 ms |
640 KB |
Output is correct |
34 |
Correct |
20 ms |
636 KB |
Output is correct |
35 |
Correct |
27 ms |
18260 KB |
Output is correct |
36 |
Correct |
25 ms |
18324 KB |
Output is correct |
37 |
Correct |
88 ms |
18284 KB |
Output is correct |
38 |
Correct |
92 ms |
18248 KB |
Output is correct |
39 |
Correct |
35 ms |
18388 KB |
Output is correct |
40 |
Correct |
78 ms |
18260 KB |
Output is correct |
41 |
Correct |
83 ms |
17876 KB |
Output is correct |
42 |
Correct |
85 ms |
17908 KB |
Output is correct |
43 |
Correct |
94 ms |
17912 KB |
Output is correct |
44 |
Correct |
106 ms |
17900 KB |
Output is correct |
45 |
Correct |
84 ms |
17920 KB |
Output is correct |
46 |
Correct |
87 ms |
17912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
2132 KB |
Output is correct |
2 |
Correct |
9 ms |
596 KB |
Output is correct |
3 |
Correct |
10 ms |
596 KB |
Output is correct |
4 |
Correct |
9 ms |
596 KB |
Output is correct |
5 |
Correct |
15 ms |
2356 KB |
Output is correct |
6 |
Correct |
89 ms |
139608 KB |
Output is correct |
7 |
Correct |
116 ms |
166468 KB |
Output is correct |
8 |
Correct |
106 ms |
177416 KB |
Output is correct |
9 |
Correct |
105 ms |
177400 KB |
Output is correct |
10 |
Correct |
11 ms |
604 KB |
Output is correct |
11 |
Correct |
13 ms |
772 KB |
Output is correct |
12 |
Correct |
27 ms |
724 KB |
Output is correct |
13 |
Correct |
32 ms |
752 KB |
Output is correct |
14 |
Correct |
20 ms |
724 KB |
Output is correct |
15 |
Correct |
14 ms |
604 KB |
Output is correct |
16 |
Correct |
21 ms |
724 KB |
Output is correct |
17 |
Correct |
18 ms |
596 KB |
Output is correct |
18 |
Correct |
19 ms |
596 KB |
Output is correct |
19 |
Correct |
19 ms |
596 KB |
Output is correct |
20 |
Correct |
19 ms |
640 KB |
Output is correct |
21 |
Correct |
20 ms |
636 KB |
Output is correct |
22 |
Correct |
19 ms |
596 KB |
Output is correct |
23 |
Correct |
19 ms |
644 KB |
Output is correct |
24 |
Correct |
21 ms |
636 KB |
Output is correct |
25 |
Correct |
20 ms |
640 KB |
Output is correct |
26 |
Correct |
24 ms |
636 KB |
Output is correct |
27 |
Correct |
10 ms |
596 KB |
Output is correct |
28 |
Correct |
12 ms |
776 KB |
Output is correct |
29 |
Correct |
27 ms |
768 KB |
Output is correct |
30 |
Correct |
24 ms |
724 KB |
Output is correct |
31 |
Correct |
16 ms |
768 KB |
Output is correct |
32 |
Correct |
14 ms |
596 KB |
Output is correct |
33 |
Correct |
21 ms |
732 KB |
Output is correct |
34 |
Correct |
18 ms |
644 KB |
Output is correct |
35 |
Correct |
21 ms |
640 KB |
Output is correct |
36 |
Correct |
23 ms |
596 KB |
Output is correct |
37 |
Correct |
21 ms |
640 KB |
Output is correct |
38 |
Correct |
22 ms |
596 KB |
Output is correct |
39 |
Correct |
19 ms |
644 KB |
Output is correct |
40 |
Correct |
19 ms |
596 KB |
Output is correct |
41 |
Correct |
20 ms |
724 KB |
Output is correct |
42 |
Correct |
18 ms |
640 KB |
Output is correct |
43 |
Correct |
20 ms |
636 KB |
Output is correct |
44 |
Correct |
27 ms |
18260 KB |
Output is correct |
45 |
Correct |
25 ms |
18324 KB |
Output is correct |
46 |
Correct |
88 ms |
18284 KB |
Output is correct |
47 |
Correct |
92 ms |
18248 KB |
Output is correct |
48 |
Correct |
35 ms |
18388 KB |
Output is correct |
49 |
Correct |
78 ms |
18260 KB |
Output is correct |
50 |
Correct |
83 ms |
17876 KB |
Output is correct |
51 |
Correct |
85 ms |
17908 KB |
Output is correct |
52 |
Correct |
94 ms |
17912 KB |
Output is correct |
53 |
Correct |
106 ms |
17900 KB |
Output is correct |
54 |
Correct |
84 ms |
17920 KB |
Output is correct |
55 |
Correct |
87 ms |
17912 KB |
Output is correct |
56 |
Correct |
14 ms |
612 KB |
Output is correct |
57 |
Correct |
28 ms |
644 KB |
Output is correct |
58 |
Correct |
31 ms |
2172 KB |
Output is correct |
59 |
Correct |
9 ms |
588 KB |
Output is correct |
60 |
Correct |
12 ms |
596 KB |
Output is correct |
61 |
Correct |
14 ms |
596 KB |
Output is correct |
62 |
Correct |
16 ms |
2260 KB |
Output is correct |
63 |
Correct |
84 ms |
139648 KB |
Output is correct |
64 |
Correct |
97 ms |
166476 KB |
Output is correct |
65 |
Correct |
105 ms |
177416 KB |
Output is correct |
66 |
Correct |
108 ms |
177324 KB |
Output is correct |
67 |
Correct |
11 ms |
604 KB |
Output is correct |
68 |
Correct |
13 ms |
768 KB |
Output is correct |
69 |
Correct |
26 ms |
724 KB |
Output is correct |
70 |
Correct |
25 ms |
724 KB |
Output is correct |
71 |
Correct |
17 ms |
724 KB |
Output is correct |
72 |
Correct |
13 ms |
596 KB |
Output is correct |
73 |
Correct |
21 ms |
736 KB |
Output is correct |
74 |
Correct |
19 ms |
596 KB |
Output is correct |
75 |
Correct |
18 ms |
644 KB |
Output is correct |
76 |
Correct |
18 ms |
596 KB |
Output is correct |
77 |
Correct |
19 ms |
596 KB |
Output is correct |
78 |
Correct |
20 ms |
644 KB |
Output is correct |
79 |
Correct |
20 ms |
636 KB |
Output is correct |
80 |
Correct |
20 ms |
596 KB |
Output is correct |
81 |
Correct |
19 ms |
596 KB |
Output is correct |
82 |
Correct |
18 ms |
596 KB |
Output is correct |
83 |
Correct |
20 ms |
636 KB |
Output is correct |
84 |
Correct |
26 ms |
18260 KB |
Output is correct |
85 |
Correct |
26 ms |
18260 KB |
Output is correct |
86 |
Correct |
89 ms |
18288 KB |
Output is correct |
87 |
Correct |
85 ms |
18260 KB |
Output is correct |
88 |
Correct |
30 ms |
18324 KB |
Output is correct |
89 |
Correct |
76 ms |
18188 KB |
Output is correct |
90 |
Correct |
90 ms |
17920 KB |
Output is correct |
91 |
Correct |
85 ms |
17876 KB |
Output is correct |
92 |
Correct |
112 ms |
17916 KB |
Output is correct |
93 |
Correct |
90 ms |
17876 KB |
Output is correct |
94 |
Correct |
86 ms |
17876 KB |
Output is correct |
95 |
Correct |
89 ms |
17876 KB |
Output is correct |
96 |
Correct |
259 ms |
177372 KB |
Output is correct |
97 |
Correct |
27 ms |
596 KB |
Output is correct |
98 |
Correct |
225 ms |
177264 KB |
Output is correct |
99 |
Correct |
112 ms |
177408 KB |
Output is correct |
100 |
Correct |
19 ms |
596 KB |
Output is correct |
101 |
Correct |
224 ms |
177276 KB |
Output is correct |
102 |
Correct |
234 ms |
177320 KB |
Output is correct |
103 |
Correct |
237 ms |
176796 KB |
Output is correct |
104 |
Correct |
233 ms |
176700 KB |
Output is correct |
105 |
Correct |
247 ms |
176596 KB |
Output is correct |
106 |
Correct |
228 ms |
176652 KB |
Output is correct |
107 |
Correct |
272 ms |
176596 KB |
Output is correct |
108 |
Correct |
235 ms |
176476 KB |
Output is correct |
109 |
Correct |
234 ms |
176724 KB |
Output is correct |