#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
using ll = long long;
const int MXG = 15;
const ll INF = 1e17;
string s;
int n;
vector<ll> A[MXG];
// pref[i][j][k] -> i after j cost from left at k
vector<ll> pref[MXG][MXG];
// suff[i][j][k] -> i after j cost from right at k
vector<ll> suff[MXG][MXG];
ll dp[1 << MXG];
bool visited[1 << MXG];
// Ok but, why is vector.insert(vector.begin(), x) is 100x slower than vector.push_back(x)...
void init()
{
// pref
for(int i = 0; i < MXG; i++) {
for(int j = 0; j < MXG; j++) {
if(i == j || A[i].empty() || A[j].empty()) continue;
int r = 0;
int cost = 0;
for(int k = 0; k < A[i].size(); k++) {
while(r < A[j].size() && A[j][r] < A[i][k]) {
r++;
}
cost += r;
pref[i][j].push_back(4*cost);
}
}
}
// suff
for(int i = 0; i < MXG; i++) {
for(int j = 0; j < MXG; j++) {
if(i == j || A[i].empty() || A[j].empty()) continue;
int r = A[j].size()-1;
int cost = 0;
for(int k = A[i].size()-1; k >= 0; k--) {
while(r >= 0 && A[j][r] > A[i][k]) {
r--;
}
cost += A[j].size()-1-r;
suff[i][j].push_back(4*cost);
}
reverse(suff[i][j].begin(), suff[i][j].end());
}
}
}
ll cost(int group, int bitmask, int split)
{
ll sum = 0;
for(int g = 0; g < MXG; g++) {
if(((bitmask >> g) & 1) == 0 || A[g].empty() || g == group) continue;
//cerr << "left: " << (split > -1 ? pref[group][g][split] : 0) << "\n";
sum += split > -1 ? pref[group][g][split] : 0;
//cerr << "right: " << (split+1 < A[group].size() ? suff[group][g][split+1] : 0) << "\n";
sum += split+1 < A[group].size() ? suff[group][g][split+1] : 0;
}
if(split > 0) {
//cerr << "left chance: " << split*(split+1) << "\n";
// int*int overflow LET'S GO!
sum += 1LL*split*(split+1); // chance to collide between on the left
}
ll cnt = A[group].size() - (split+1);
if(cnt > 1) {
//cerr << "right chance: " << cnt*(cnt-1) << "\n";
sum += cnt*(cnt-1); // chance to collide between on the right
}
return sum;
}
ll recur(int bitmask)
{
if(bitmask == 0) {
return 0;
}
if(visited[bitmask]) {
return dp[bitmask];
}
visited[bitmask] = true;
ll mn = INF;
for(int g = 0; g < MXG; g++) {
if(((bitmask >> g) & 1) == 0) continue;
int bits = bitmask - (1 << g);
ll initial = recur(bits);
if(A[g].empty()) {
mn = min(mn, initial);
continue;
}
int l = -1, r = A[g].size()-1;
while(l < r) {
int mid = l == -1 && r == 0 ? -1 : (l + r) / 2;
ll a = cost(g, bits, mid);
ll b = cost(g, bits, mid+1);
if(a < b) {
r = mid;
}
else {
l = mid+1;
}
}
mn = min(mn, initial + cost(g, bits, l));
}
dp[bitmask] = mn;
return dp[bitmask];
}
int main()
{
cin.tie(0);
ios::sync_with_stdio(0);
getline(cin, s);
n = s.size();
for(int i = 0; i < n; i++) {
A[s[i]-'A'].push_back(i);
}
init();
// the hell is this grader?
ll ans = recur(0x7fff);
cout << ans / 4;
if(ans % 4 == 1) {
cout << ".25";
}
else if(ans % 4 == 2) {
cout << ".5";
}
else if(ans % 4 == 3) {
cout << ".75";
}
return 0;
}
/*
AACCAA
HEHEHEHIHILOL
ONMLKJIHGFEDCBAABCDEFGHIJKLMNO
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
*/
Compilation message
passes.cpp: In function 'void init()':
passes.cpp:30:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for(int k = 0; k < A[i].size(); k++) {
| ~~^~~~~~~~~~~~~
passes.cpp:31:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | while(r < A[j].size() && A[j][r] < A[i][k]) {
| ~~^~~~~~~~~~~~~
passes.cpp: In function 'll cost(int, int, int)':
passes.cpp:65:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | sum += split+1 < A[group].size() ? suff[group][g][split+1] : 0;
| ~~~~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
596 KB |
Output is correct |
2 |
Correct |
4 ms |
596 KB |
Output is correct |
3 |
Correct |
5 ms |
596 KB |
Output is correct |
4 |
Correct |
5 ms |
596 KB |
Output is correct |
5 |
Correct |
9 ms |
596 KB |
Output is correct |
6 |
Correct |
16 ms |
1680 KB |
Output is correct |
7 |
Correct |
15 ms |
1680 KB |
Output is correct |
8 |
Correct |
16 ms |
1680 KB |
Output is correct |
9 |
Correct |
15 ms |
1680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
596 KB |
Output is correct |
2 |
Correct |
7 ms |
620 KB |
Output is correct |
3 |
Correct |
32 ms |
596 KB |
Output is correct |
4 |
Correct |
35 ms |
596 KB |
Output is correct |
5 |
Correct |
20 ms |
636 KB |
Output is correct |
6 |
Correct |
18 ms |
596 KB |
Output is correct |
7 |
Correct |
31 ms |
596 KB |
Output is correct |
8 |
Correct |
26 ms |
620 KB |
Output is correct |
9 |
Correct |
28 ms |
596 KB |
Output is correct |
10 |
Correct |
25 ms |
596 KB |
Output is correct |
11 |
Correct |
24 ms |
624 KB |
Output is correct |
12 |
Correct |
26 ms |
596 KB |
Output is correct |
13 |
Correct |
28 ms |
624 KB |
Output is correct |
14 |
Correct |
27 ms |
596 KB |
Output is correct |
15 |
Correct |
25 ms |
596 KB |
Output is correct |
16 |
Correct |
27 ms |
596 KB |
Output is correct |
17 |
Correct |
26 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
596 KB |
Output is correct |
2 |
Correct |
7 ms |
620 KB |
Output is correct |
3 |
Correct |
32 ms |
596 KB |
Output is correct |
4 |
Correct |
35 ms |
596 KB |
Output is correct |
5 |
Correct |
20 ms |
636 KB |
Output is correct |
6 |
Correct |
18 ms |
596 KB |
Output is correct |
7 |
Correct |
31 ms |
596 KB |
Output is correct |
8 |
Correct |
26 ms |
620 KB |
Output is correct |
9 |
Correct |
28 ms |
596 KB |
Output is correct |
10 |
Correct |
25 ms |
596 KB |
Output is correct |
11 |
Correct |
24 ms |
624 KB |
Output is correct |
12 |
Correct |
26 ms |
596 KB |
Output is correct |
13 |
Correct |
28 ms |
624 KB |
Output is correct |
14 |
Correct |
27 ms |
596 KB |
Output is correct |
15 |
Correct |
25 ms |
596 KB |
Output is correct |
16 |
Correct |
27 ms |
596 KB |
Output is correct |
17 |
Correct |
26 ms |
596 KB |
Output is correct |
18 |
Correct |
8 ms |
596 KB |
Output is correct |
19 |
Correct |
8 ms |
596 KB |
Output is correct |
20 |
Correct |
34 ms |
724 KB |
Output is correct |
21 |
Correct |
37 ms |
596 KB |
Output is correct |
22 |
Correct |
20 ms |
632 KB |
Output is correct |
23 |
Correct |
21 ms |
596 KB |
Output is correct |
24 |
Correct |
29 ms |
612 KB |
Output is correct |
25 |
Correct |
24 ms |
596 KB |
Output is correct |
26 |
Correct |
27 ms |
596 KB |
Output is correct |
27 |
Correct |
25 ms |
628 KB |
Output is correct |
28 |
Correct |
24 ms |
628 KB |
Output is correct |
29 |
Correct |
25 ms |
596 KB |
Output is correct |
30 |
Correct |
27 ms |
620 KB |
Output is correct |
31 |
Correct |
28 ms |
596 KB |
Output is correct |
32 |
Correct |
24 ms |
596 KB |
Output is correct |
33 |
Correct |
28 ms |
620 KB |
Output is correct |
34 |
Correct |
30 ms |
608 KB |
Output is correct |
35 |
Correct |
14 ms |
748 KB |
Output is correct |
36 |
Correct |
12 ms |
852 KB |
Output is correct |
37 |
Correct |
95 ms |
2388 KB |
Output is correct |
38 |
Correct |
111 ms |
2132 KB |
Output is correct |
39 |
Correct |
35 ms |
2260 KB |
Output is correct |
40 |
Correct |
86 ms |
2132 KB |
Output is correct |
41 |
Correct |
99 ms |
2596 KB |
Output is correct |
42 |
Correct |
91 ms |
2616 KB |
Output is correct |
43 |
Correct |
98 ms |
2560 KB |
Output is correct |
44 |
Correct |
93 ms |
2516 KB |
Output is correct |
45 |
Correct |
94 ms |
2612 KB |
Output is correct |
46 |
Correct |
92 ms |
2648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
596 KB |
Output is correct |
2 |
Correct |
4 ms |
596 KB |
Output is correct |
3 |
Correct |
5 ms |
596 KB |
Output is correct |
4 |
Correct |
5 ms |
596 KB |
Output is correct |
5 |
Correct |
9 ms |
596 KB |
Output is correct |
6 |
Correct |
16 ms |
1680 KB |
Output is correct |
7 |
Correct |
15 ms |
1680 KB |
Output is correct |
8 |
Correct |
16 ms |
1680 KB |
Output is correct |
9 |
Correct |
15 ms |
1680 KB |
Output is correct |
10 |
Correct |
7 ms |
596 KB |
Output is correct |
11 |
Correct |
7 ms |
620 KB |
Output is correct |
12 |
Correct |
32 ms |
596 KB |
Output is correct |
13 |
Correct |
35 ms |
596 KB |
Output is correct |
14 |
Correct |
20 ms |
636 KB |
Output is correct |
15 |
Correct |
18 ms |
596 KB |
Output is correct |
16 |
Correct |
31 ms |
596 KB |
Output is correct |
17 |
Correct |
26 ms |
620 KB |
Output is correct |
18 |
Correct |
28 ms |
596 KB |
Output is correct |
19 |
Correct |
25 ms |
596 KB |
Output is correct |
20 |
Correct |
24 ms |
624 KB |
Output is correct |
21 |
Correct |
26 ms |
596 KB |
Output is correct |
22 |
Correct |
28 ms |
624 KB |
Output is correct |
23 |
Correct |
27 ms |
596 KB |
Output is correct |
24 |
Correct |
25 ms |
596 KB |
Output is correct |
25 |
Correct |
27 ms |
596 KB |
Output is correct |
26 |
Correct |
26 ms |
596 KB |
Output is correct |
27 |
Correct |
8 ms |
596 KB |
Output is correct |
28 |
Correct |
8 ms |
596 KB |
Output is correct |
29 |
Correct |
34 ms |
724 KB |
Output is correct |
30 |
Correct |
37 ms |
596 KB |
Output is correct |
31 |
Correct |
20 ms |
632 KB |
Output is correct |
32 |
Correct |
21 ms |
596 KB |
Output is correct |
33 |
Correct |
29 ms |
612 KB |
Output is correct |
34 |
Correct |
24 ms |
596 KB |
Output is correct |
35 |
Correct |
27 ms |
596 KB |
Output is correct |
36 |
Correct |
25 ms |
628 KB |
Output is correct |
37 |
Correct |
24 ms |
628 KB |
Output is correct |
38 |
Correct |
25 ms |
596 KB |
Output is correct |
39 |
Correct |
27 ms |
620 KB |
Output is correct |
40 |
Correct |
28 ms |
596 KB |
Output is correct |
41 |
Correct |
24 ms |
596 KB |
Output is correct |
42 |
Correct |
28 ms |
620 KB |
Output is correct |
43 |
Correct |
30 ms |
608 KB |
Output is correct |
44 |
Correct |
14 ms |
748 KB |
Output is correct |
45 |
Correct |
12 ms |
852 KB |
Output is correct |
46 |
Correct |
95 ms |
2388 KB |
Output is correct |
47 |
Correct |
111 ms |
2132 KB |
Output is correct |
48 |
Correct |
35 ms |
2260 KB |
Output is correct |
49 |
Correct |
86 ms |
2132 KB |
Output is correct |
50 |
Correct |
99 ms |
2596 KB |
Output is correct |
51 |
Correct |
91 ms |
2616 KB |
Output is correct |
52 |
Correct |
98 ms |
2560 KB |
Output is correct |
53 |
Correct |
93 ms |
2516 KB |
Output is correct |
54 |
Correct |
94 ms |
2612 KB |
Output is correct |
55 |
Correct |
92 ms |
2648 KB |
Output is correct |
56 |
Correct |
20 ms |
628 KB |
Output is correct |
57 |
Correct |
64 ms |
612 KB |
Output is correct |
58 |
Correct |
10 ms |
632 KB |
Output is correct |
59 |
Correct |
4 ms |
616 KB |
Output is correct |
60 |
Correct |
5 ms |
616 KB |
Output is correct |
61 |
Correct |
6 ms |
596 KB |
Output is correct |
62 |
Correct |
10 ms |
632 KB |
Output is correct |
63 |
Correct |
15 ms |
1680 KB |
Output is correct |
64 |
Correct |
15 ms |
1680 KB |
Output is correct |
65 |
Correct |
15 ms |
1680 KB |
Output is correct |
66 |
Correct |
15 ms |
1680 KB |
Output is correct |
67 |
Correct |
8 ms |
596 KB |
Output is correct |
68 |
Correct |
7 ms |
628 KB |
Output is correct |
69 |
Correct |
33 ms |
620 KB |
Output is correct |
70 |
Correct |
37 ms |
608 KB |
Output is correct |
71 |
Correct |
21 ms |
596 KB |
Output is correct |
72 |
Correct |
20 ms |
620 KB |
Output is correct |
73 |
Correct |
29 ms |
596 KB |
Output is correct |
74 |
Correct |
25 ms |
620 KB |
Output is correct |
75 |
Correct |
29 ms |
596 KB |
Output is correct |
76 |
Correct |
26 ms |
596 KB |
Output is correct |
77 |
Correct |
26 ms |
624 KB |
Output is correct |
78 |
Correct |
26 ms |
620 KB |
Output is correct |
79 |
Correct |
27 ms |
596 KB |
Output is correct |
80 |
Correct |
28 ms |
596 KB |
Output is correct |
81 |
Correct |
25 ms |
596 KB |
Output is correct |
82 |
Correct |
27 ms |
596 KB |
Output is correct |
83 |
Correct |
27 ms |
596 KB |
Output is correct |
84 |
Correct |
12 ms |
752 KB |
Output is correct |
85 |
Correct |
14 ms |
752 KB |
Output is correct |
86 |
Correct |
95 ms |
2428 KB |
Output is correct |
87 |
Correct |
112 ms |
2152 KB |
Output is correct |
88 |
Correct |
36 ms |
2260 KB |
Output is correct |
89 |
Correct |
87 ms |
2168 KB |
Output is correct |
90 |
Correct |
93 ms |
2600 KB |
Output is correct |
91 |
Correct |
96 ms |
2516 KB |
Output is correct |
92 |
Correct |
98 ms |
2516 KB |
Output is correct |
93 |
Correct |
92 ms |
2516 KB |
Output is correct |
94 |
Correct |
92 ms |
2516 KB |
Output is correct |
95 |
Correct |
97 ms |
2644 KB |
Output is correct |
96 |
Correct |
244 ms |
25240 KB |
Output is correct |
97 |
Correct |
68 ms |
596 KB |
Output is correct |
98 |
Correct |
235 ms |
25216 KB |
Output is correct |
99 |
Correct |
85 ms |
23944 KB |
Output is correct |
100 |
Correct |
40 ms |
716 KB |
Output is correct |
101 |
Correct |
229 ms |
25264 KB |
Output is correct |
102 |
Correct |
219 ms |
25352 KB |
Output is correct |
103 |
Correct |
223 ms |
25228 KB |
Output is correct |
104 |
Correct |
225 ms |
25332 KB |
Output is correct |
105 |
Correct |
230 ms |
25376 KB |
Output is correct |
106 |
Correct |
222 ms |
25204 KB |
Output is correct |
107 |
Correct |
225 ms |
25528 KB |
Output is correct |
108 |
Correct |
211 ms |
23452 KB |
Output is correct |
109 |
Correct |
234 ms |
25460 KB |
Output is correct |