#include <bits/stdc++.h>
#define IO_OP ios::sync_with_stdio(0), cin.tie(0)
#define F first
#define S second
#define V vector
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
const int INF = 1e9 + 7;
const ll oo = 1e18;
vi G[15];
ll dp[1 << 15];
void cmin(ll& a, ll b) {
a = min(a, b);
}
ll C2(int n) { return 1LL * n * (n - 1) / 2; }
signed main()
{
IO_OP;
string s; cin >> s;
int n = SZ(s);
for(int i = 0; i < n; i++)
G[s[i] - 'A'].PB(i);
vi groups;
for(int i = 0; i < 15; i++)
if(G[i].size())
groups.PB(i);
int g = SZ(groups);
V<V<V<ll>>> aux(g, V<V<ll>>(g));
for(int i = 0; i < g; i++) {
for(int j = 0; j < g; j++) if(i != j) {
vi& gi = G[groups[i]], gj = G[groups[j]];
aux[i][j].resize(SZ(gj) + 1);
vi lef(SZ(gj)), rig(SZ(gj));
for(int k = 0, l = 0; k < SZ(gj); k++) {
while(l < SZ(gi) && gi[l] < gj[k])
l++;
lef[k] = l;
}
for(int k = SZ(gj) - 1, l = SZ(gi) - 1; k >= 0; k--) {
while(l >= 0 && gi[l] > gj[k])
l--;
rig[k] = SZ(gi) - 1 - l;
}
for(int k = 1; k < SZ(lef); k++)
lef[k] += lef[k - 1];
for(int k = SZ(rig) - 2; k >= 0; k--)
rig[k] += rig[k + 1];
for(int k = 0; k < SZ(aux[i][j]); k++) {
if(k) aux[i][j][k] += lef[k - 1];
if(k < SZ(gj)) aux[i][j][k] += rig[k];
}
}
}
for(int i = 1; i < (1 << g); i++) {
dp[i] = oo;
for(int j = 0; j < g; j++) {
if(i >> j & 1) {
int k = i ^ (1 << j);
int len = SZ(G[groups[j]]);
auto go = [&] (int pos) {
ll res = C2(pos) + C2(len - pos);
for(int l = 0; l < g; l++) if(k >> l & 1)
res += aux[l][j][pos] * 2;
return res;
};
int lb = 0, rb = len - 1;
while(lb <= rb) {
int mb = (lb + rb) / 2;
if(go(mb) > go(mb + 1)) lb = mb + 1;
else rb = mb - 1;
}
cmin(dp[i], dp[k] + go(lb));
}
}
}
ll ans = dp[(1 << g) - 1];
if(ans & 1) cout << ans / 2 << ".5\n";
else cout << ans / 2 << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
2 ms |
1172 KB |
Output is correct |
7 |
Correct |
2 ms |
1172 KB |
Output is correct |
8 |
Correct |
2 ms |
1172 KB |
Output is correct |
9 |
Correct |
2 ms |
1172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 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 |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 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 |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
7 ms |
1136 KB |
Output is correct |
38 |
Correct |
4 ms |
1108 KB |
Output is correct |
39 |
Correct |
3 ms |
1108 KB |
Output is correct |
40 |
Correct |
4 ms |
1108 KB |
Output is correct |
41 |
Correct |
4 ms |
1108 KB |
Output is correct |
42 |
Correct |
4 ms |
1108 KB |
Output is correct |
43 |
Correct |
4 ms |
1108 KB |
Output is correct |
44 |
Correct |
4 ms |
1108 KB |
Output is correct |
45 |
Correct |
5 ms |
1108 KB |
Output is correct |
46 |
Correct |
5 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
2 ms |
1172 KB |
Output is correct |
7 |
Correct |
2 ms |
1172 KB |
Output is correct |
8 |
Correct |
2 ms |
1172 KB |
Output is correct |
9 |
Correct |
2 ms |
1172 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 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 |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
7 ms |
1136 KB |
Output is correct |
47 |
Correct |
4 ms |
1108 KB |
Output is correct |
48 |
Correct |
3 ms |
1108 KB |
Output is correct |
49 |
Correct |
4 ms |
1108 KB |
Output is correct |
50 |
Correct |
4 ms |
1108 KB |
Output is correct |
51 |
Correct |
4 ms |
1108 KB |
Output is correct |
52 |
Correct |
4 ms |
1108 KB |
Output is correct |
53 |
Correct |
4 ms |
1108 KB |
Output is correct |
54 |
Correct |
5 ms |
1108 KB |
Output is correct |
55 |
Correct |
5 ms |
1108 KB |
Output is correct |
56 |
Correct |
0 ms |
212 KB |
Output is correct |
57 |
Correct |
60 ms |
468 KB |
Output is correct |
58 |
Correct |
0 ms |
212 KB |
Output is correct |
59 |
Correct |
0 ms |
212 KB |
Output is correct |
60 |
Correct |
1 ms |
212 KB |
Output is correct |
61 |
Correct |
1 ms |
212 KB |
Output is correct |
62 |
Correct |
0 ms |
212 KB |
Output is correct |
63 |
Correct |
2 ms |
1168 KB |
Output is correct |
64 |
Correct |
2 ms |
1172 KB |
Output is correct |
65 |
Correct |
2 ms |
1140 KB |
Output is correct |
66 |
Correct |
1 ms |
1172 KB |
Output is correct |
67 |
Correct |
0 ms |
212 KB |
Output is correct |
68 |
Correct |
0 ms |
212 KB |
Output is correct |
69 |
Correct |
1 ms |
212 KB |
Output is correct |
70 |
Correct |
1 ms |
212 KB |
Output is correct |
71 |
Correct |
1 ms |
212 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
0 ms |
212 KB |
Output is correct |
74 |
Correct |
0 ms |
212 KB |
Output is correct |
75 |
Correct |
1 ms |
212 KB |
Output is correct |
76 |
Correct |
1 ms |
212 KB |
Output is correct |
77 |
Correct |
1 ms |
212 KB |
Output is correct |
78 |
Correct |
0 ms |
212 KB |
Output is correct |
79 |
Correct |
1 ms |
212 KB |
Output is correct |
80 |
Correct |
0 ms |
272 KB |
Output is correct |
81 |
Correct |
1 ms |
212 KB |
Output is correct |
82 |
Correct |
1 ms |
212 KB |
Output is correct |
83 |
Correct |
1 ms |
212 KB |
Output is correct |
84 |
Correct |
0 ms |
340 KB |
Output is correct |
85 |
Correct |
1 ms |
340 KB |
Output is correct |
86 |
Correct |
7 ms |
1108 KB |
Output is correct |
87 |
Correct |
3 ms |
1112 KB |
Output is correct |
88 |
Correct |
3 ms |
1236 KB |
Output is correct |
89 |
Correct |
5 ms |
1036 KB |
Output is correct |
90 |
Correct |
4 ms |
1108 KB |
Output is correct |
91 |
Correct |
5 ms |
1108 KB |
Output is correct |
92 |
Correct |
4 ms |
1108 KB |
Output is correct |
93 |
Correct |
4 ms |
1108 KB |
Output is correct |
94 |
Correct |
4 ms |
1124 KB |
Output is correct |
95 |
Correct |
5 ms |
1108 KB |
Output is correct |
96 |
Correct |
197 ms |
12348 KB |
Output is correct |
97 |
Correct |
27 ms |
388 KB |
Output is correct |
98 |
Correct |
175 ms |
12212 KB |
Output is correct |
99 |
Correct |
76 ms |
13016 KB |
Output is correct |
100 |
Correct |
46 ms |
504 KB |
Output is correct |
101 |
Correct |
208 ms |
12168 KB |
Output is correct |
102 |
Correct |
176 ms |
12396 KB |
Output is correct |
103 |
Correct |
178 ms |
12316 KB |
Output is correct |
104 |
Correct |
175 ms |
12452 KB |
Output is correct |
105 |
Correct |
192 ms |
12256 KB |
Output is correct |
106 |
Correct |
195 ms |
12208 KB |
Output is correct |
107 |
Correct |
172 ms |
12240 KB |
Output is correct |
108 |
Correct |
90 ms |
11212 KB |
Output is correct |
109 |
Correct |
183 ms |
12384 KB |
Output is correct |