#include <bits/stdc++.h>
using namespace std;
typedef long long llint;
const int maxn = 1e5+10;
const int maxg = 15;
const int off = (1 << maxg);
int n;
char niz[maxn];
llint dp[off + 10];
vector< int > pos[300];
llint cntl[maxg + 1][maxn], cntal[maxg + 1][maxg + 1][maxn];
llint cntr[maxg + 1][maxn], cntar[maxg + 1][maxg + 1][maxn];
llint get(int mask, int x, int wh) {
llint out = cntal[x][x][wh] + cntar[x][x][wh + 1];
for (int i = 0; i < maxg; i++) {
if (mask & (1 << i)) {
llint tren = cntal[x][i][wh] + cntar[x][i][wh + 1];
out += tren * 2;
}
}
//if (mask == 0 && x == 2) printf("get: %d %d %d --> %lld\n", mask, x, wh, out);
return out;
}
llint f(int mask) {
//printf("debug: %d\n", mask);
if (mask == 0) return 0;
llint &ret = dp[mask];
if (ret != -1) return ret;
ret = 1LL << 60;
for (int i = 0; i < maxg; i++) {
if (mask & (1 << i)) {
llint cost = 1LL << 60;
int tr = mask ^ (1 << i);
llint base = f(tr);
if (pos[i].size() == 0) {
ret = min(ret, base);
continue;
}
int lo = 0, hi = pos[i].size();
while (lo < hi) {
int mid = (lo + hi) / 2;
llint a = get(tr, i, mid);
llint b = get(tr, i, mid + 1);
if (a < b) hi = mid;
else lo = mid + 1;
}
cost = base + get(tr, i, lo);
ret = min(ret, cost);
}
}
//printf("f: %d --> %lld\n", mask, ret);
return ret;
}
int main() {
scanf("%s", niz);
n = strlen(niz);
for (int i = 0; i < n; i++)
pos[niz[i] - 'A'].push_back(i);
for (int i = 0; i < maxg; i++) {
int tren = 0;
for (int j = 0; j < n; j++) {
cntl[i][j] = tren;
if (niz[j] == i + 'A') tren++;
}
tren = 0;
for (int j = n - 1; j >= 0; j--) {
cntr[i][j] = tren;
if (niz[j] == i + 'A') tren++;
}
}
for (int i = 0; i < maxg; i++) {
for (int j = 0; j < maxg; j++) {
for (int k = 0; k < pos[i].size(); k++) {
cntal[i][j][k + 1] = cntal[i][j][k] + cntl[j][pos[i][k]];
}
cntal[i][j][pos[i].size() + 1] = cntal[i][j][pos[i].size()] + pos[j].size();
for (int k = pos[i].size() - 1; k >= 0; k--) {
cntar[i][j][k + 1] = cntar[i][j][k + 2] + cntr[j][pos[i][k]];
}
cntar[i][j][0] = cntar[i][j][1] + pos[j].size();
}
}
for (int i = 0; i < off + 10; i++)
dp[i] = -1;
llint sol = f((1 << maxg) - 1);
printf("%lld", sol / 2);
if (sol % 2 == 1) printf(".5");
printf("\n");
return 0;
}
Compilation message
passes.cpp: In function 'int main()':
passes.cpp:84:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for (int k = 0; k < pos[i].size(); k++) {
| ~~^~~~~~~~~~~~~~~
passes.cpp:63:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | scanf("%s", niz);
| ~~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
3700 KB |
Output is correct |
2 |
Correct |
6 ms |
3156 KB |
Output is correct |
3 |
Correct |
11 ms |
3160 KB |
Output is correct |
4 |
Correct |
8 ms |
3156 KB |
Output is correct |
5 |
Correct |
12 ms |
3668 KB |
Output is correct |
6 |
Correct |
44 ms |
40592 KB |
Output is correct |
7 |
Correct |
48 ms |
47684 KB |
Output is correct |
8 |
Correct |
51 ms |
50528 KB |
Output is correct |
9 |
Correct |
43 ms |
50432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
3284 KB |
Output is correct |
2 |
Correct |
10 ms |
3316 KB |
Output is correct |
3 |
Correct |
40 ms |
3304 KB |
Output is correct |
4 |
Correct |
35 ms |
3284 KB |
Output is correct |
5 |
Correct |
23 ms |
3284 KB |
Output is correct |
6 |
Correct |
21 ms |
3284 KB |
Output is correct |
7 |
Correct |
40 ms |
3324 KB |
Output is correct |
8 |
Correct |
30 ms |
3284 KB |
Output is correct |
9 |
Correct |
27 ms |
3300 KB |
Output is correct |
10 |
Correct |
25 ms |
3284 KB |
Output is correct |
11 |
Correct |
26 ms |
3296 KB |
Output is correct |
12 |
Correct |
25 ms |
3284 KB |
Output is correct |
13 |
Correct |
28 ms |
3156 KB |
Output is correct |
14 |
Correct |
27 ms |
3284 KB |
Output is correct |
15 |
Correct |
28 ms |
3284 KB |
Output is correct |
16 |
Correct |
34 ms |
3284 KB |
Output is correct |
17 |
Correct |
29 ms |
3284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
3284 KB |
Output is correct |
2 |
Correct |
10 ms |
3316 KB |
Output is correct |
3 |
Correct |
40 ms |
3304 KB |
Output is correct |
4 |
Correct |
35 ms |
3284 KB |
Output is correct |
5 |
Correct |
23 ms |
3284 KB |
Output is correct |
6 |
Correct |
21 ms |
3284 KB |
Output is correct |
7 |
Correct |
40 ms |
3324 KB |
Output is correct |
8 |
Correct |
30 ms |
3284 KB |
Output is correct |
9 |
Correct |
27 ms |
3300 KB |
Output is correct |
10 |
Correct |
25 ms |
3284 KB |
Output is correct |
11 |
Correct |
26 ms |
3296 KB |
Output is correct |
12 |
Correct |
25 ms |
3284 KB |
Output is correct |
13 |
Correct |
28 ms |
3156 KB |
Output is correct |
14 |
Correct |
27 ms |
3284 KB |
Output is correct |
15 |
Correct |
28 ms |
3284 KB |
Output is correct |
16 |
Correct |
34 ms |
3284 KB |
Output is correct |
17 |
Correct |
29 ms |
3284 KB |
Output is correct |
18 |
Correct |
10 ms |
3284 KB |
Output is correct |
19 |
Correct |
11 ms |
3328 KB |
Output is correct |
20 |
Correct |
34 ms |
3284 KB |
Output is correct |
21 |
Correct |
33 ms |
3284 KB |
Output is correct |
22 |
Correct |
24 ms |
3284 KB |
Output is correct |
23 |
Correct |
21 ms |
3284 KB |
Output is correct |
24 |
Correct |
31 ms |
3308 KB |
Output is correct |
25 |
Correct |
27 ms |
3284 KB |
Output is correct |
26 |
Correct |
32 ms |
3300 KB |
Output is correct |
27 |
Correct |
32 ms |
3284 KB |
Output is correct |
28 |
Correct |
26 ms |
3296 KB |
Output is correct |
29 |
Correct |
24 ms |
3284 KB |
Output is correct |
30 |
Correct |
27 ms |
3264 KB |
Output is correct |
31 |
Correct |
28 ms |
3284 KB |
Output is correct |
32 |
Correct |
28 ms |
3292 KB |
Output is correct |
33 |
Correct |
27 ms |
3284 KB |
Output is correct |
34 |
Correct |
28 ms |
3288 KB |
Output is correct |
35 |
Correct |
15 ms |
8020 KB |
Output is correct |
36 |
Correct |
17 ms |
8020 KB |
Output is correct |
37 |
Correct |
112 ms |
8096 KB |
Output is correct |
38 |
Correct |
119 ms |
7996 KB |
Output is correct |
39 |
Correct |
33 ms |
8020 KB |
Output is correct |
40 |
Correct |
80 ms |
8020 KB |
Output is correct |
41 |
Correct |
86 ms |
7892 KB |
Output is correct |
42 |
Correct |
85 ms |
7956 KB |
Output is correct |
43 |
Correct |
106 ms |
7956 KB |
Output is correct |
44 |
Correct |
90 ms |
7988 KB |
Output is correct |
45 |
Correct |
87 ms |
7996 KB |
Output is correct |
46 |
Correct |
101 ms |
7892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
3700 KB |
Output is correct |
2 |
Correct |
6 ms |
3156 KB |
Output is correct |
3 |
Correct |
11 ms |
3160 KB |
Output is correct |
4 |
Correct |
8 ms |
3156 KB |
Output is correct |
5 |
Correct |
12 ms |
3668 KB |
Output is correct |
6 |
Correct |
44 ms |
40592 KB |
Output is correct |
7 |
Correct |
48 ms |
47684 KB |
Output is correct |
8 |
Correct |
51 ms |
50528 KB |
Output is correct |
9 |
Correct |
43 ms |
50432 KB |
Output is correct |
10 |
Correct |
10 ms |
3284 KB |
Output is correct |
11 |
Correct |
10 ms |
3316 KB |
Output is correct |
12 |
Correct |
40 ms |
3304 KB |
Output is correct |
13 |
Correct |
35 ms |
3284 KB |
Output is correct |
14 |
Correct |
23 ms |
3284 KB |
Output is correct |
15 |
Correct |
21 ms |
3284 KB |
Output is correct |
16 |
Correct |
40 ms |
3324 KB |
Output is correct |
17 |
Correct |
30 ms |
3284 KB |
Output is correct |
18 |
Correct |
27 ms |
3300 KB |
Output is correct |
19 |
Correct |
25 ms |
3284 KB |
Output is correct |
20 |
Correct |
26 ms |
3296 KB |
Output is correct |
21 |
Correct |
25 ms |
3284 KB |
Output is correct |
22 |
Correct |
28 ms |
3156 KB |
Output is correct |
23 |
Correct |
27 ms |
3284 KB |
Output is correct |
24 |
Correct |
28 ms |
3284 KB |
Output is correct |
25 |
Correct |
34 ms |
3284 KB |
Output is correct |
26 |
Correct |
29 ms |
3284 KB |
Output is correct |
27 |
Correct |
10 ms |
3284 KB |
Output is correct |
28 |
Correct |
11 ms |
3328 KB |
Output is correct |
29 |
Correct |
34 ms |
3284 KB |
Output is correct |
30 |
Correct |
33 ms |
3284 KB |
Output is correct |
31 |
Correct |
24 ms |
3284 KB |
Output is correct |
32 |
Correct |
21 ms |
3284 KB |
Output is correct |
33 |
Correct |
31 ms |
3308 KB |
Output is correct |
34 |
Correct |
27 ms |
3284 KB |
Output is correct |
35 |
Correct |
32 ms |
3300 KB |
Output is correct |
36 |
Correct |
32 ms |
3284 KB |
Output is correct |
37 |
Correct |
26 ms |
3296 KB |
Output is correct |
38 |
Correct |
24 ms |
3284 KB |
Output is correct |
39 |
Correct |
27 ms |
3264 KB |
Output is correct |
40 |
Correct |
28 ms |
3284 KB |
Output is correct |
41 |
Correct |
28 ms |
3292 KB |
Output is correct |
42 |
Correct |
27 ms |
3284 KB |
Output is correct |
43 |
Correct |
28 ms |
3288 KB |
Output is correct |
44 |
Correct |
15 ms |
8020 KB |
Output is correct |
45 |
Correct |
17 ms |
8020 KB |
Output is correct |
46 |
Correct |
112 ms |
8096 KB |
Output is correct |
47 |
Correct |
119 ms |
7996 KB |
Output is correct |
48 |
Correct |
33 ms |
8020 KB |
Output is correct |
49 |
Correct |
80 ms |
8020 KB |
Output is correct |
50 |
Correct |
86 ms |
7892 KB |
Output is correct |
51 |
Correct |
85 ms |
7956 KB |
Output is correct |
52 |
Correct |
106 ms |
7956 KB |
Output is correct |
53 |
Correct |
90 ms |
7988 KB |
Output is correct |
54 |
Correct |
87 ms |
7996 KB |
Output is correct |
55 |
Correct |
101 ms |
7892 KB |
Output is correct |
56 |
Correct |
19 ms |
3276 KB |
Output is correct |
57 |
Correct |
48 ms |
3284 KB |
Output is correct |
58 |
Correct |
17 ms |
3700 KB |
Output is correct |
59 |
Correct |
7 ms |
3156 KB |
Output is correct |
60 |
Correct |
9 ms |
3272 KB |
Output is correct |
61 |
Correct |
7 ms |
3284 KB |
Output is correct |
62 |
Correct |
12 ms |
3668 KB |
Output is correct |
63 |
Correct |
37 ms |
40488 KB |
Output is correct |
64 |
Correct |
39 ms |
47780 KB |
Output is correct |
65 |
Correct |
42 ms |
50508 KB |
Output is correct |
66 |
Correct |
42 ms |
50496 KB |
Output is correct |
67 |
Correct |
10 ms |
3280 KB |
Output is correct |
68 |
Correct |
9 ms |
3284 KB |
Output is correct |
69 |
Correct |
38 ms |
3284 KB |
Output is correct |
70 |
Correct |
35 ms |
3284 KB |
Output is correct |
71 |
Correct |
23 ms |
3284 KB |
Output is correct |
72 |
Correct |
20 ms |
3288 KB |
Output is correct |
73 |
Correct |
32 ms |
3328 KB |
Output is correct |
74 |
Correct |
24 ms |
3296 KB |
Output is correct |
75 |
Correct |
27 ms |
3284 KB |
Output is correct |
76 |
Correct |
27 ms |
3284 KB |
Output is correct |
77 |
Correct |
25 ms |
3300 KB |
Output is correct |
78 |
Correct |
25 ms |
3284 KB |
Output is correct |
79 |
Correct |
33 ms |
3284 KB |
Output is correct |
80 |
Correct |
30 ms |
3284 KB |
Output is correct |
81 |
Correct |
27 ms |
3284 KB |
Output is correct |
82 |
Correct |
28 ms |
3284 KB |
Output is correct |
83 |
Correct |
27 ms |
3284 KB |
Output is correct |
84 |
Correct |
16 ms |
8020 KB |
Output is correct |
85 |
Correct |
17 ms |
8024 KB |
Output is correct |
86 |
Correct |
98 ms |
8092 KB |
Output is correct |
87 |
Correct |
88 ms |
7996 KB |
Output is correct |
88 |
Correct |
34 ms |
8020 KB |
Output is correct |
89 |
Correct |
79 ms |
8020 KB |
Output is correct |
90 |
Correct |
87 ms |
8004 KB |
Output is correct |
91 |
Correct |
86 ms |
7968 KB |
Output is correct |
92 |
Correct |
86 ms |
7892 KB |
Output is correct |
93 |
Correct |
86 ms |
7892 KB |
Output is correct |
94 |
Correct |
103 ms |
8000 KB |
Output is correct |
95 |
Correct |
88 ms |
7892 KB |
Output is correct |
96 |
Correct |
180 ms |
50728 KB |
Output is correct |
97 |
Correct |
45 ms |
3284 KB |
Output is correct |
98 |
Correct |
211 ms |
50632 KB |
Output is correct |
99 |
Correct |
83 ms |
50508 KB |
Output is correct |
100 |
Correct |
33 ms |
3156 KB |
Output is correct |
101 |
Correct |
168 ms |
50668 KB |
Output is correct |
102 |
Correct |
191 ms |
50728 KB |
Output is correct |
103 |
Correct |
207 ms |
50508 KB |
Output is correct |
104 |
Correct |
189 ms |
50584 KB |
Output is correct |
105 |
Correct |
186 ms |
50652 KB |
Output is correct |
106 |
Correct |
192 ms |
50624 KB |
Output is correct |
107 |
Correct |
204 ms |
50512 KB |
Output is correct |
108 |
Correct |
178 ms |
50408 KB |
Output is correct |
109 |
Correct |
193 ms |
50728 KB |
Output is correct |