#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;
long long s = (1LL * p * (p - 1) + 1LL * r * (r - 1)) / 2;
p = pos[i][p];
for (int j = 0; j < n; j++) if (S >> j & 1) {
if (p >= 0) 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++) {
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
2132 KB |
Output is correct |
2 |
Correct |
15 ms |
588 KB |
Output is correct |
3 |
Correct |
17 ms |
592 KB |
Output is correct |
4 |
Correct |
17 ms |
608 KB |
Output is correct |
5 |
Correct |
24 ms |
2356 KB |
Output is correct |
6 |
Correct |
101 ms |
139700 KB |
Output is correct |
7 |
Correct |
112 ms |
166604 KB |
Output is correct |
8 |
Correct |
115 ms |
177384 KB |
Output is correct |
9 |
Correct |
111 ms |
177376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
596 KB |
Output is correct |
2 |
Correct |
23 ms |
724 KB |
Output is correct |
3 |
Correct |
48 ms |
760 KB |
Output is correct |
4 |
Correct |
43 ms |
760 KB |
Output is correct |
5 |
Correct |
32 ms |
768 KB |
Output is correct |
6 |
Correct |
25 ms |
596 KB |
Output is correct |
7 |
Correct |
44 ms |
728 KB |
Output is correct |
8 |
Correct |
37 ms |
596 KB |
Output is correct |
9 |
Correct |
39 ms |
596 KB |
Output is correct |
10 |
Correct |
39 ms |
596 KB |
Output is correct |
11 |
Correct |
37 ms |
596 KB |
Output is correct |
12 |
Correct |
37 ms |
620 KB |
Output is correct |
13 |
Correct |
36 ms |
616 KB |
Output is correct |
14 |
Correct |
38 ms |
596 KB |
Output is correct |
15 |
Correct |
39 ms |
616 KB |
Output is correct |
16 |
Correct |
41 ms |
596 KB |
Output is correct |
17 |
Correct |
40 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
596 KB |
Output is correct |
2 |
Correct |
23 ms |
724 KB |
Output is correct |
3 |
Correct |
48 ms |
760 KB |
Output is correct |
4 |
Correct |
43 ms |
760 KB |
Output is correct |
5 |
Correct |
32 ms |
768 KB |
Output is correct |
6 |
Correct |
25 ms |
596 KB |
Output is correct |
7 |
Correct |
44 ms |
728 KB |
Output is correct |
8 |
Correct |
37 ms |
596 KB |
Output is correct |
9 |
Correct |
39 ms |
596 KB |
Output is correct |
10 |
Correct |
39 ms |
596 KB |
Output is correct |
11 |
Correct |
37 ms |
596 KB |
Output is correct |
12 |
Correct |
37 ms |
620 KB |
Output is correct |
13 |
Correct |
36 ms |
616 KB |
Output is correct |
14 |
Correct |
38 ms |
596 KB |
Output is correct |
15 |
Correct |
39 ms |
616 KB |
Output is correct |
16 |
Correct |
41 ms |
596 KB |
Output is correct |
17 |
Correct |
40 ms |
596 KB |
Output is correct |
18 |
Correct |
20 ms |
604 KB |
Output is correct |
19 |
Correct |
21 ms |
768 KB |
Output is correct |
20 |
Correct |
44 ms |
724 KB |
Output is correct |
21 |
Correct |
42 ms |
724 KB |
Output is correct |
22 |
Correct |
33 ms |
724 KB |
Output is correct |
23 |
Correct |
27 ms |
596 KB |
Output is correct |
24 |
Correct |
49 ms |
724 KB |
Output is correct |
25 |
Correct |
37 ms |
616 KB |
Output is correct |
26 |
Correct |
37 ms |
596 KB |
Output is correct |
27 |
Correct |
37 ms |
596 KB |
Output is correct |
28 |
Correct |
38 ms |
596 KB |
Output is correct |
29 |
Correct |
37 ms |
596 KB |
Output is correct |
30 |
Correct |
35 ms |
624 KB |
Output is correct |
31 |
Correct |
37 ms |
596 KB |
Output is correct |
32 |
Correct |
38 ms |
704 KB |
Output is correct |
33 |
Correct |
38 ms |
596 KB |
Output is correct |
34 |
Correct |
41 ms |
596 KB |
Output is correct |
35 |
Correct |
35 ms |
18320 KB |
Output is correct |
36 |
Correct |
35 ms |
18260 KB |
Output is correct |
37 |
Correct |
104 ms |
18276 KB |
Output is correct |
38 |
Correct |
112 ms |
18380 KB |
Output is correct |
39 |
Correct |
49 ms |
18312 KB |
Output is correct |
40 |
Correct |
97 ms |
18180 KB |
Output is correct |
41 |
Correct |
104 ms |
17916 KB |
Output is correct |
42 |
Correct |
101 ms |
17804 KB |
Output is correct |
43 |
Correct |
105 ms |
17916 KB |
Output is correct |
44 |
Correct |
107 ms |
17944 KB |
Output is correct |
45 |
Correct |
102 ms |
17876 KB |
Output is correct |
46 |
Correct |
102 ms |
17992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
2132 KB |
Output is correct |
2 |
Correct |
15 ms |
588 KB |
Output is correct |
3 |
Correct |
17 ms |
592 KB |
Output is correct |
4 |
Correct |
17 ms |
608 KB |
Output is correct |
5 |
Correct |
24 ms |
2356 KB |
Output is correct |
6 |
Correct |
101 ms |
139700 KB |
Output is correct |
7 |
Correct |
112 ms |
166604 KB |
Output is correct |
8 |
Correct |
115 ms |
177384 KB |
Output is correct |
9 |
Correct |
111 ms |
177376 KB |
Output is correct |
10 |
Correct |
21 ms |
596 KB |
Output is correct |
11 |
Correct |
23 ms |
724 KB |
Output is correct |
12 |
Correct |
48 ms |
760 KB |
Output is correct |
13 |
Correct |
43 ms |
760 KB |
Output is correct |
14 |
Correct |
32 ms |
768 KB |
Output is correct |
15 |
Correct |
25 ms |
596 KB |
Output is correct |
16 |
Correct |
44 ms |
728 KB |
Output is correct |
17 |
Correct |
37 ms |
596 KB |
Output is correct |
18 |
Correct |
39 ms |
596 KB |
Output is correct |
19 |
Correct |
39 ms |
596 KB |
Output is correct |
20 |
Correct |
37 ms |
596 KB |
Output is correct |
21 |
Correct |
37 ms |
620 KB |
Output is correct |
22 |
Correct |
36 ms |
616 KB |
Output is correct |
23 |
Correct |
38 ms |
596 KB |
Output is correct |
24 |
Correct |
39 ms |
616 KB |
Output is correct |
25 |
Correct |
41 ms |
596 KB |
Output is correct |
26 |
Correct |
40 ms |
596 KB |
Output is correct |
27 |
Correct |
20 ms |
604 KB |
Output is correct |
28 |
Correct |
21 ms |
768 KB |
Output is correct |
29 |
Correct |
44 ms |
724 KB |
Output is correct |
30 |
Correct |
42 ms |
724 KB |
Output is correct |
31 |
Correct |
33 ms |
724 KB |
Output is correct |
32 |
Correct |
27 ms |
596 KB |
Output is correct |
33 |
Correct |
49 ms |
724 KB |
Output is correct |
34 |
Correct |
37 ms |
616 KB |
Output is correct |
35 |
Correct |
37 ms |
596 KB |
Output is correct |
36 |
Correct |
37 ms |
596 KB |
Output is correct |
37 |
Correct |
38 ms |
596 KB |
Output is correct |
38 |
Correct |
37 ms |
596 KB |
Output is correct |
39 |
Correct |
35 ms |
624 KB |
Output is correct |
40 |
Correct |
37 ms |
596 KB |
Output is correct |
41 |
Correct |
38 ms |
704 KB |
Output is correct |
42 |
Correct |
38 ms |
596 KB |
Output is correct |
43 |
Correct |
41 ms |
596 KB |
Output is correct |
44 |
Correct |
35 ms |
18320 KB |
Output is correct |
45 |
Correct |
35 ms |
18260 KB |
Output is correct |
46 |
Correct |
104 ms |
18276 KB |
Output is correct |
47 |
Correct |
112 ms |
18380 KB |
Output is correct |
48 |
Correct |
49 ms |
18312 KB |
Output is correct |
49 |
Correct |
97 ms |
18180 KB |
Output is correct |
50 |
Correct |
104 ms |
17916 KB |
Output is correct |
51 |
Correct |
101 ms |
17804 KB |
Output is correct |
52 |
Correct |
105 ms |
17916 KB |
Output is correct |
53 |
Correct |
107 ms |
17944 KB |
Output is correct |
54 |
Correct |
102 ms |
17876 KB |
Output is correct |
55 |
Correct |
102 ms |
17992 KB |
Output is correct |
56 |
Correct |
27 ms |
596 KB |
Output is correct |
57 |
Correct |
46 ms |
596 KB |
Output is correct |
58 |
Correct |
23 ms |
2180 KB |
Output is correct |
59 |
Correct |
15 ms |
588 KB |
Output is correct |
60 |
Correct |
18 ms |
596 KB |
Output is correct |
61 |
Correct |
19 ms |
600 KB |
Output is correct |
62 |
Correct |
24 ms |
2360 KB |
Output is correct |
63 |
Correct |
94 ms |
139704 KB |
Output is correct |
64 |
Correct |
120 ms |
166556 KB |
Output is correct |
65 |
Correct |
112 ms |
177348 KB |
Output is correct |
66 |
Correct |
109 ms |
177416 KB |
Output is correct |
67 |
Correct |
21 ms |
600 KB |
Output is correct |
68 |
Correct |
22 ms |
724 KB |
Output is correct |
69 |
Correct |
47 ms |
752 KB |
Output is correct |
70 |
Correct |
45 ms |
724 KB |
Output is correct |
71 |
Correct |
33 ms |
724 KB |
Output is correct |
72 |
Correct |
25 ms |
612 KB |
Output is correct |
73 |
Correct |
39 ms |
724 KB |
Output is correct |
74 |
Correct |
36 ms |
596 KB |
Output is correct |
75 |
Correct |
38 ms |
616 KB |
Output is correct |
76 |
Correct |
39 ms |
616 KB |
Output is correct |
77 |
Correct |
37 ms |
596 KB |
Output is correct |
78 |
Correct |
38 ms |
596 KB |
Output is correct |
79 |
Correct |
36 ms |
596 KB |
Output is correct |
80 |
Correct |
38 ms |
596 KB |
Output is correct |
81 |
Correct |
36 ms |
596 KB |
Output is correct |
82 |
Correct |
39 ms |
596 KB |
Output is correct |
83 |
Correct |
41 ms |
624 KB |
Output is correct |
84 |
Correct |
43 ms |
18260 KB |
Output is correct |
85 |
Correct |
37 ms |
18308 KB |
Output is correct |
86 |
Correct |
103 ms |
18280 KB |
Output is correct |
87 |
Correct |
100 ms |
18248 KB |
Output is correct |
88 |
Correct |
50 ms |
18244 KB |
Output is correct |
89 |
Correct |
98 ms |
18184 KB |
Output is correct |
90 |
Correct |
110 ms |
17920 KB |
Output is correct |
91 |
Correct |
111 ms |
17916 KB |
Output is correct |
92 |
Correct |
105 ms |
17916 KB |
Output is correct |
93 |
Correct |
105 ms |
17876 KB |
Output is correct |
94 |
Correct |
101 ms |
17920 KB |
Output is correct |
95 |
Correct |
100 ms |
17924 KB |
Output is correct |
96 |
Correct |
256 ms |
177360 KB |
Output is correct |
97 |
Correct |
52 ms |
596 KB |
Output is correct |
98 |
Correct |
246 ms |
177304 KB |
Output is correct |
99 |
Correct |
131 ms |
177412 KB |
Output is correct |
100 |
Correct |
36 ms |
612 KB |
Output is correct |
101 |
Correct |
230 ms |
177340 KB |
Output is correct |
102 |
Correct |
237 ms |
177388 KB |
Output is correct |
103 |
Correct |
253 ms |
176652 KB |
Output is correct |
104 |
Correct |
249 ms |
176700 KB |
Output is correct |
105 |
Correct |
239 ms |
176720 KB |
Output is correct |
106 |
Correct |
257 ms |
176724 KB |
Output is correct |
107 |
Correct |
247 ms |
176684 KB |
Output is correct |
108 |
Correct |
238 ms |
176536 KB |
Output is correct |
109 |
Correct |
249 ms |
176732 KB |
Output is correct |