/**
* author: wxhtzdy
* created: 10.07.2022 13:22:51
**/
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
string s;
cin >> s;
int n = (int) s.size();
vector<int> a(n);
for (int i = 0; i < n; i++) {
a[i] = (int) (s[i] - 'A');
}
const int A = 15;
vector<vector<int>> ids(A);
for (int i = 0; i < n; i++) {
ids[a[i]].push_back(i);
}
vector<vector<vector<long long>>> cntL(A, vector<vector<long long>>(A));
vector<vector<vector<long long>>> cntR(A, vector<vector<long long>>(A));
for (int i = 0; i < A; i++) {
int sz = (int) ids[i].size();
for (int j = 0; j < A; j++) {
cntL[i][j].resize(sz);
int ptr = 0;
for (int k = 0; k < sz; k++) {
while (ptr < (int) ids[j].size() && ids[j][ptr] < ids[i][k]) {
ptr += 1;
}
cntL[i][j][k] = ptr;
if (k > 0) {
cntL[i][j][k] += cntL[i][j][k - 1];
}
}
}
}
for (int i = 0; i < A; i++) {
int sz = (int) ids[i].size();
for (int j = 0; j < A; j++) {
cntR[i][j].resize(sz);
int ptr = (int) ids[j].size() - 1;
for (int k = sz - 1; k >= 0; k--) {
while (ptr >= 0 && ids[j][ptr] > ids[i][k]) {
ptr -= 1;
}
cntR[i][j][k] = (int) ids[j].size() - 1 - ptr;
if (k < sz - 1) {
cntR[i][j][k] += cntR[i][j][k + 1];
}
}
}
}
vector<long double> dp(1 << A, 1e14);
dp[0] = 0;
for (int s = 0; s < (1 << A); s++) {
for (int i = 0; i < A; i++) {
if (s >> i & 1) {
continue;
}
if (ids[i].empty()) {
dp[s | (1 << i)] = min(dp[s | (1 << i)], dp[s]);
continue;
}
auto Calc = [&](int id) {
int L = id + 1;
int R = (int) ids[i].size() - id - 1;
long double res = (double) (L * 1LL * (L - 1) / 2) / 2.0;
res += (R * 1LL * (R - 1) / 2) / 2.0;
for (int j = 0; j < A; j++) {
if (s >> j & 1) {
if (id >= 0) {
res += cntL[i][j][id];
}
if (id + 1 < (int) ids[i].size()) {
res += cntR[i][j][id + 1];
}
}
}
return res;
};
int low = -1, high = (int) ids[i].size() - 2, at = high + 1;
while (low <= high) {
int mid = (low + high) >> 1;
if (Calc(mid) > Calc(mid + 1)) {
low = mid + 1;
} else {
at = mid;
high = mid - 1;
}
}
dp[s | (1 << i)] = min(dp[s | (1 << i)], dp[s] + Calc(at));
}
}
int zzz = dp[(1 << A) - 1];
if (dp[(1 << A) - 1] != (double) zzz) {
cout << fixed << setprecision(1) << dp[(1 << A) - 1];
} else {
cout << fixed << setprecision(0) << dp[(1 << A) - 1];
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
980 KB |
Output is correct |
2 |
Correct |
7 ms |
852 KB |
Output is correct |
3 |
Correct |
9 ms |
724 KB |
Output is correct |
4 |
Correct |
10 ms |
852 KB |
Output is correct |
5 |
Correct |
18 ms |
984 KB |
Output is correct |
6 |
Correct |
39 ms |
20240 KB |
Output is correct |
7 |
Correct |
43 ms |
24112 KB |
Output is correct |
8 |
Correct |
44 ms |
25604 KB |
Output is correct |
9 |
Correct |
45 ms |
25568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
852 KB |
Output is correct |
2 |
Correct |
14 ms |
852 KB |
Output is correct |
3 |
Correct |
45 ms |
852 KB |
Output is correct |
4 |
Correct |
43 ms |
852 KB |
Output is correct |
5 |
Correct |
27 ms |
852 KB |
Output is correct |
6 |
Correct |
22 ms |
852 KB |
Output is correct |
7 |
Correct |
39 ms |
852 KB |
Output is correct |
8 |
Correct |
33 ms |
852 KB |
Output is correct |
9 |
Correct |
35 ms |
852 KB |
Output is correct |
10 |
Correct |
38 ms |
852 KB |
Output is correct |
11 |
Correct |
35 ms |
852 KB |
Output is correct |
12 |
Correct |
37 ms |
852 KB |
Output is correct |
13 |
Correct |
34 ms |
852 KB |
Output is correct |
14 |
Correct |
35 ms |
852 KB |
Output is correct |
15 |
Correct |
36 ms |
852 KB |
Output is correct |
16 |
Correct |
34 ms |
852 KB |
Output is correct |
17 |
Correct |
37 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
852 KB |
Output is correct |
2 |
Correct |
14 ms |
852 KB |
Output is correct |
3 |
Correct |
45 ms |
852 KB |
Output is correct |
4 |
Correct |
43 ms |
852 KB |
Output is correct |
5 |
Correct |
27 ms |
852 KB |
Output is correct |
6 |
Correct |
22 ms |
852 KB |
Output is correct |
7 |
Correct |
39 ms |
852 KB |
Output is correct |
8 |
Correct |
33 ms |
852 KB |
Output is correct |
9 |
Correct |
35 ms |
852 KB |
Output is correct |
10 |
Correct |
38 ms |
852 KB |
Output is correct |
11 |
Correct |
35 ms |
852 KB |
Output is correct |
12 |
Correct |
37 ms |
852 KB |
Output is correct |
13 |
Correct |
34 ms |
852 KB |
Output is correct |
14 |
Correct |
35 ms |
852 KB |
Output is correct |
15 |
Correct |
36 ms |
852 KB |
Output is correct |
16 |
Correct |
34 ms |
852 KB |
Output is correct |
17 |
Correct |
37 ms |
852 KB |
Output is correct |
18 |
Correct |
13 ms |
852 KB |
Output is correct |
19 |
Correct |
14 ms |
852 KB |
Output is correct |
20 |
Correct |
44 ms |
852 KB |
Output is correct |
21 |
Correct |
44 ms |
852 KB |
Output is correct |
22 |
Correct |
27 ms |
852 KB |
Output is correct |
23 |
Correct |
23 ms |
852 KB |
Output is correct |
24 |
Correct |
36 ms |
852 KB |
Output is correct |
25 |
Correct |
32 ms |
852 KB |
Output is correct |
26 |
Correct |
36 ms |
852 KB |
Output is correct |
27 |
Correct |
38 ms |
852 KB |
Output is correct |
28 |
Correct |
35 ms |
852 KB |
Output is correct |
29 |
Correct |
36 ms |
852 KB |
Output is correct |
30 |
Correct |
34 ms |
744 KB |
Output is correct |
31 |
Correct |
35 ms |
852 KB |
Output is correct |
32 |
Correct |
36 ms |
852 KB |
Output is correct |
33 |
Correct |
35 ms |
852 KB |
Output is correct |
34 |
Correct |
38 ms |
852 KB |
Output is correct |
35 |
Correct |
21 ms |
3284 KB |
Output is correct |
36 |
Correct |
21 ms |
3284 KB |
Output is correct |
37 |
Correct |
111 ms |
3284 KB |
Output is correct |
38 |
Correct |
105 ms |
3284 KB |
Output is correct |
39 |
Correct |
40 ms |
3284 KB |
Output is correct |
40 |
Correct |
99 ms |
3268 KB |
Output is correct |
41 |
Correct |
106 ms |
3284 KB |
Output is correct |
42 |
Correct |
106 ms |
3284 KB |
Output is correct |
43 |
Correct |
102 ms |
3284 KB |
Output is correct |
44 |
Correct |
106 ms |
3284 KB |
Output is correct |
45 |
Correct |
104 ms |
3284 KB |
Output is correct |
46 |
Correct |
105 ms |
3284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
980 KB |
Output is correct |
2 |
Correct |
7 ms |
852 KB |
Output is correct |
3 |
Correct |
9 ms |
724 KB |
Output is correct |
4 |
Correct |
10 ms |
852 KB |
Output is correct |
5 |
Correct |
18 ms |
984 KB |
Output is correct |
6 |
Correct |
39 ms |
20240 KB |
Output is correct |
7 |
Correct |
43 ms |
24112 KB |
Output is correct |
8 |
Correct |
44 ms |
25604 KB |
Output is correct |
9 |
Correct |
45 ms |
25568 KB |
Output is correct |
10 |
Correct |
13 ms |
852 KB |
Output is correct |
11 |
Correct |
14 ms |
852 KB |
Output is correct |
12 |
Correct |
45 ms |
852 KB |
Output is correct |
13 |
Correct |
43 ms |
852 KB |
Output is correct |
14 |
Correct |
27 ms |
852 KB |
Output is correct |
15 |
Correct |
22 ms |
852 KB |
Output is correct |
16 |
Correct |
39 ms |
852 KB |
Output is correct |
17 |
Correct |
33 ms |
852 KB |
Output is correct |
18 |
Correct |
35 ms |
852 KB |
Output is correct |
19 |
Correct |
38 ms |
852 KB |
Output is correct |
20 |
Correct |
35 ms |
852 KB |
Output is correct |
21 |
Correct |
37 ms |
852 KB |
Output is correct |
22 |
Correct |
34 ms |
852 KB |
Output is correct |
23 |
Correct |
35 ms |
852 KB |
Output is correct |
24 |
Correct |
36 ms |
852 KB |
Output is correct |
25 |
Correct |
34 ms |
852 KB |
Output is correct |
26 |
Correct |
37 ms |
852 KB |
Output is correct |
27 |
Correct |
13 ms |
852 KB |
Output is correct |
28 |
Correct |
14 ms |
852 KB |
Output is correct |
29 |
Correct |
44 ms |
852 KB |
Output is correct |
30 |
Correct |
44 ms |
852 KB |
Output is correct |
31 |
Correct |
27 ms |
852 KB |
Output is correct |
32 |
Correct |
23 ms |
852 KB |
Output is correct |
33 |
Correct |
36 ms |
852 KB |
Output is correct |
34 |
Correct |
32 ms |
852 KB |
Output is correct |
35 |
Correct |
36 ms |
852 KB |
Output is correct |
36 |
Correct |
38 ms |
852 KB |
Output is correct |
37 |
Correct |
35 ms |
852 KB |
Output is correct |
38 |
Correct |
36 ms |
852 KB |
Output is correct |
39 |
Correct |
34 ms |
744 KB |
Output is correct |
40 |
Correct |
35 ms |
852 KB |
Output is correct |
41 |
Correct |
36 ms |
852 KB |
Output is correct |
42 |
Correct |
35 ms |
852 KB |
Output is correct |
43 |
Correct |
38 ms |
852 KB |
Output is correct |
44 |
Correct |
21 ms |
3284 KB |
Output is correct |
45 |
Correct |
21 ms |
3284 KB |
Output is correct |
46 |
Correct |
111 ms |
3284 KB |
Output is correct |
47 |
Correct |
105 ms |
3284 KB |
Output is correct |
48 |
Correct |
40 ms |
3284 KB |
Output is correct |
49 |
Correct |
99 ms |
3268 KB |
Output is correct |
50 |
Correct |
106 ms |
3284 KB |
Output is correct |
51 |
Correct |
106 ms |
3284 KB |
Output is correct |
52 |
Correct |
102 ms |
3284 KB |
Output is correct |
53 |
Correct |
106 ms |
3284 KB |
Output is correct |
54 |
Correct |
104 ms |
3284 KB |
Output is correct |
55 |
Correct |
105 ms |
3284 KB |
Output is correct |
56 |
Correct |
23 ms |
852 KB |
Output is correct |
57 |
Correct |
49 ms |
852 KB |
Output is correct |
58 |
Correct |
17 ms |
960 KB |
Output is correct |
59 |
Correct |
7 ms |
852 KB |
Output is correct |
60 |
Correct |
9 ms |
836 KB |
Output is correct |
61 |
Correct |
9 ms |
852 KB |
Output is correct |
62 |
Correct |
17 ms |
980 KB |
Output is correct |
63 |
Correct |
41 ms |
20368 KB |
Output is correct |
64 |
Correct |
42 ms |
24080 KB |
Output is correct |
65 |
Correct |
43 ms |
25608 KB |
Output is correct |
66 |
Correct |
42 ms |
25632 KB |
Output is correct |
67 |
Correct |
13 ms |
852 KB |
Output is correct |
68 |
Correct |
13 ms |
852 KB |
Output is correct |
69 |
Correct |
45 ms |
852 KB |
Output is correct |
70 |
Correct |
46 ms |
852 KB |
Output is correct |
71 |
Correct |
27 ms |
828 KB |
Output is correct |
72 |
Correct |
21 ms |
852 KB |
Output is correct |
73 |
Correct |
37 ms |
852 KB |
Output is correct |
74 |
Correct |
32 ms |
852 KB |
Output is correct |
75 |
Correct |
36 ms |
852 KB |
Output is correct |
76 |
Correct |
35 ms |
852 KB |
Output is correct |
77 |
Correct |
35 ms |
828 KB |
Output is correct |
78 |
Correct |
37 ms |
852 KB |
Output is correct |
79 |
Correct |
34 ms |
852 KB |
Output is correct |
80 |
Correct |
36 ms |
852 KB |
Output is correct |
81 |
Correct |
36 ms |
852 KB |
Output is correct |
82 |
Correct |
36 ms |
852 KB |
Output is correct |
83 |
Correct |
38 ms |
852 KB |
Output is correct |
84 |
Correct |
21 ms |
3284 KB |
Output is correct |
85 |
Correct |
21 ms |
3348 KB |
Output is correct |
86 |
Correct |
109 ms |
3272 KB |
Output is correct |
87 |
Correct |
106 ms |
3272 KB |
Output is correct |
88 |
Correct |
40 ms |
3284 KB |
Output is correct |
89 |
Correct |
94 ms |
3272 KB |
Output is correct |
90 |
Correct |
106 ms |
3284 KB |
Output is correct |
91 |
Correct |
105 ms |
3284 KB |
Output is correct |
92 |
Correct |
103 ms |
3284 KB |
Output is correct |
93 |
Correct |
112 ms |
3284 KB |
Output is correct |
94 |
Correct |
106 ms |
3252 KB |
Output is correct |
95 |
Correct |
103 ms |
3284 KB |
Output is correct |
96 |
Correct |
242 ms |
25572 KB |
Output is correct |
97 |
Correct |
57 ms |
852 KB |
Output is correct |
98 |
Correct |
217 ms |
25400 KB |
Output is correct |
99 |
Correct |
71 ms |
25580 KB |
Output is correct |
100 |
Correct |
36 ms |
852 KB |
Output is correct |
101 |
Correct |
183 ms |
25428 KB |
Output is correct |
102 |
Correct |
202 ms |
25528 KB |
Output is correct |
103 |
Correct |
203 ms |
25212 KB |
Output is correct |
104 |
Correct |
203 ms |
25372 KB |
Output is correct |
105 |
Correct |
206 ms |
25400 KB |
Output is correct |
106 |
Correct |
204 ms |
25280 KB |
Output is correct |
107 |
Correct |
208 ms |
25340 KB |
Output is correct |
108 |
Correct |
207 ms |
25292 KB |
Output is correct |
109 |
Correct |
203 ms |
25292 KB |
Output is correct |