/*input
ABABABAB
*/
#pragma GCC optimize ("O3")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll dp[1 << 15];
int main()
{
ios_base::sync_with_stdio(false);
string s;
cin >> s;
for (int i = 0; i < (1 << 15); i++)
dp[i] = 1e18;
dp[0] = 0;
vector<int>p[15];
for (int i = 0; i < (int)s.size(); i++)
p[s[i] - 'A'].push_back(i);
int G = 15;
while (p[G - 1].empty())
G--;
vector<ll>L1[15][15];
vector<ll>R1[15][15];
for (int w = 0; w < G; w++)
{
for (int v = 0; v < G; v++)
{
if (v == w)
continue;
L1[w][v] = vector<ll>(p[v].size() + 2, 0);
R1[w][v] = vector<ll>(p[v].size() + 2, 0);
vector<ll>&L = L1[w][v];
vector<ll>&R = R1[w][v];
auto it = p[v].begin();
for (int j : p[w])
{
while (it != p[v].end() && (*it) <= j)
it++;
//auto it = lower_bound(p[v].begin(), p[v].end(), j);
L[(it - p[v].begin()) + 1] += 4;
R[(p[v].end() - it) + 1] += 4;
//cout << ";" << (char)('A' + v) << " " << s[j] << " " << j << " " << (it - p[v].begin()) + 1 << " " << (p[v].end() - it) + 1 << endl;
}
for (ll i = 1; i <= (int)p[v].size(); i++)
L[i] += L[i - 1];
for (ll i = 1; i <= (int)p[v].size(); i++)
L[i] += L[i - 1];
for (ll i = 1; i <= (int)p[v].size(); i++)
R[i] += R[i - 1];
for (ll i = 1; i <= (int)p[v].size(); i++)
R[i] += R[i - 1];
}
}
int l = 0;
for (int msk = 0; msk < (1 << G); msk++)
{
for (int v = 0; v < G; v++)
{
if ((msk & (1 << v)) == 0)
{
int msk1 = msk ^ (1 << v);
auto f = [&](ll l)->ll
{
ll r = p[v].size() - l;
ll sum = l * (l - 1) + r * (r - 1);
for (int w = 0; w < G; w++)
if ((msk & (1 << w)) != 0)
{
sum += L1[w][v][l];
sum += R1[w][v][(int)p[v].size() - l];
}
return sum;
};
l = min(l, (int)p[v].size());
for (int w = p[v].size(); w >= 1; w /= 2)
{
while (l - w >= 0 && f(l - w) < f(l))
l -= w;
while (l + w <= (int)p[v].size() && f(l + w) < f(l))
l += w;
}
dp[msk1] = min(dp[msk1], dp[msk] + f(l));
}
}
}
ll ret = dp[(1 << G) - 1];
cout << ret / 4;
if (ret % 4 == 2)
{
cout << ".5";
}
else if (ret % 4 != 0)
{
cout << "." << 25 * (ret % 4);
}
cout << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
1428 KB |
Output is correct |
7 |
Correct |
1 ms |
1428 KB |
Output is correct |
8 |
Correct |
2 ms |
1428 KB |
Output is correct |
9 |
Correct |
2 ms |
1428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
468 KB |
Output is correct |
18 |
Correct |
0 ms |
468 KB |
Output is correct |
19 |
Correct |
0 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
0 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
0 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
0 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
0 ms |
468 KB |
Output is correct |
31 |
Correct |
0 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
2 ms |
1748 KB |
Output is correct |
36 |
Correct |
2 ms |
1748 KB |
Output is correct |
37 |
Correct |
6 ms |
2004 KB |
Output is correct |
38 |
Correct |
4 ms |
2004 KB |
Output is correct |
39 |
Correct |
3 ms |
2004 KB |
Output is correct |
40 |
Correct |
4 ms |
2060 KB |
Output is correct |
41 |
Correct |
5 ms |
2004 KB |
Output is correct |
42 |
Correct |
5 ms |
2004 KB |
Output is correct |
43 |
Correct |
5 ms |
2004 KB |
Output is correct |
44 |
Correct |
5 ms |
2004 KB |
Output is correct |
45 |
Correct |
5 ms |
2004 KB |
Output is correct |
46 |
Correct |
6 ms |
2004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
1428 KB |
Output is correct |
7 |
Correct |
1 ms |
1428 KB |
Output is correct |
8 |
Correct |
2 ms |
1428 KB |
Output is correct |
9 |
Correct |
2 ms |
1428 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
468 KB |
Output is correct |
14 |
Correct |
0 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
468 KB |
Output is correct |
16 |
Correct |
0 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
468 KB |
Output is correct |
18 |
Correct |
0 ms |
468 KB |
Output is correct |
19 |
Correct |
0 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
0 ms |
468 KB |
Output is correct |
27 |
Correct |
0 ms |
468 KB |
Output is correct |
28 |
Correct |
0 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
0 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
0 ms |
596 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
0 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
0 ms |
468 KB |
Output is correct |
40 |
Correct |
0 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
2 ms |
1748 KB |
Output is correct |
45 |
Correct |
2 ms |
1748 KB |
Output is correct |
46 |
Correct |
6 ms |
2004 KB |
Output is correct |
47 |
Correct |
4 ms |
2004 KB |
Output is correct |
48 |
Correct |
3 ms |
2004 KB |
Output is correct |
49 |
Correct |
4 ms |
2060 KB |
Output is correct |
50 |
Correct |
5 ms |
2004 KB |
Output is correct |
51 |
Correct |
5 ms |
2004 KB |
Output is correct |
52 |
Correct |
5 ms |
2004 KB |
Output is correct |
53 |
Correct |
5 ms |
2004 KB |
Output is correct |
54 |
Correct |
5 ms |
2004 KB |
Output is correct |
55 |
Correct |
6 ms |
2004 KB |
Output is correct |
56 |
Correct |
34 ms |
596 KB |
Output is correct |
57 |
Correct |
43 ms |
596 KB |
Output is correct |
58 |
Correct |
1 ms |
468 KB |
Output is correct |
59 |
Correct |
0 ms |
468 KB |
Output is correct |
60 |
Correct |
0 ms |
468 KB |
Output is correct |
61 |
Correct |
0 ms |
468 KB |
Output is correct |
62 |
Correct |
1 ms |
468 KB |
Output is correct |
63 |
Correct |
2 ms |
1428 KB |
Output is correct |
64 |
Correct |
2 ms |
1428 KB |
Output is correct |
65 |
Correct |
2 ms |
1428 KB |
Output is correct |
66 |
Correct |
2 ms |
1428 KB |
Output is correct |
67 |
Correct |
1 ms |
468 KB |
Output is correct |
68 |
Correct |
0 ms |
468 KB |
Output is correct |
69 |
Correct |
1 ms |
596 KB |
Output is correct |
70 |
Correct |
1 ms |
596 KB |
Output is correct |
71 |
Correct |
1 ms |
596 KB |
Output is correct |
72 |
Correct |
0 ms |
468 KB |
Output is correct |
73 |
Correct |
1 ms |
596 KB |
Output is correct |
74 |
Correct |
0 ms |
468 KB |
Output is correct |
75 |
Correct |
1 ms |
468 KB |
Output is correct |
76 |
Correct |
1 ms |
468 KB |
Output is correct |
77 |
Correct |
1 ms |
468 KB |
Output is correct |
78 |
Correct |
0 ms |
468 KB |
Output is correct |
79 |
Correct |
1 ms |
496 KB |
Output is correct |
80 |
Correct |
1 ms |
468 KB |
Output is correct |
81 |
Correct |
0 ms |
468 KB |
Output is correct |
82 |
Correct |
0 ms |
468 KB |
Output is correct |
83 |
Correct |
1 ms |
468 KB |
Output is correct |
84 |
Correct |
1 ms |
1748 KB |
Output is correct |
85 |
Correct |
2 ms |
1748 KB |
Output is correct |
86 |
Correct |
7 ms |
2004 KB |
Output is correct |
87 |
Correct |
4 ms |
2004 KB |
Output is correct |
88 |
Correct |
4 ms |
2004 KB |
Output is correct |
89 |
Correct |
5 ms |
2004 KB |
Output is correct |
90 |
Correct |
5 ms |
2052 KB |
Output is correct |
91 |
Correct |
6 ms |
2004 KB |
Output is correct |
92 |
Correct |
5 ms |
2004 KB |
Output is correct |
93 |
Correct |
9 ms |
2004 KB |
Output is correct |
94 |
Correct |
6 ms |
2004 KB |
Output is correct |
95 |
Correct |
5 ms |
2004 KB |
Output is correct |
96 |
Correct |
323 ms |
23124 KB |
Output is correct |
97 |
Correct |
33 ms |
596 KB |
Output is correct |
98 |
Correct |
176 ms |
23132 KB |
Output is correct |
99 |
Correct |
73 ms |
23312 KB |
Output is correct |
100 |
Correct |
39 ms |
596 KB |
Output is correct |
101 |
Correct |
263 ms |
23208 KB |
Output is correct |
102 |
Correct |
322 ms |
23224 KB |
Output is correct |
103 |
Correct |
323 ms |
23108 KB |
Output is correct |
104 |
Correct |
313 ms |
23160 KB |
Output is correct |
105 |
Correct |
340 ms |
23200 KB |
Output is correct |
106 |
Correct |
321 ms |
23104 KB |
Output is correct |
107 |
Correct |
308 ms |
23204 KB |
Output is correct |
108 |
Correct |
153 ms |
21460 KB |
Output is correct |
109 |
Correct |
322 ms |
23176 KB |
Output is correct |