#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define PB push_back
#define int long long
using namespace std;
const int INF = (int)1e18;
const int N = 1e5 + 500;
const int ALP = 15;
const int MSK = (1 << ALP);
int P[N][ALP], sol[N][ALP], n, tmp[ALP];
int cst[MSK][ALP], dp[MSK];
char s[N];
int f(int msk){
if(msk == MSK - 1) return 0;
if(dp[msk] != -1) return dp[msk];
dp[msk] = INF;
for(int a = 0;a < ALP;a++)
if(!((1 << a) & msk))
dp[msk] = min(dp[msk], f(msk | (1 << a)) + cst[msk][a]);
return dp[msk];
}
signed main(){
memset(dp, -1, sizeof(dp));
scanf("%s", s); n = strlen(s);
for(int i = 0;i < n;i++){
for(int j = 0;j < ALP;j++)
P[i][j] = (i ? P[i - 1][j] : 0);
P[i][s[i] - 'A']++;
}
for(int j = 0;j < ALP;j++){
vector < int > pos;
memset(tmp, 0, sizeof(tmp));
for(int i = 0;i < n;i++){
if(s[i] - 'A' != j) continue;
pos.PB(i);
for(int a = 0;a < ALP;a++){
tmp[a] += P[i][a] - (a == j);
sol[i][a] += tmp[a] * (1 + (a != j));
}
}
memset(tmp, 0, sizeof(tmp));
for(int i = n - 1;i >= 0;i--){
if(s[i] - 'A' != j) continue;
for(int a = 0;a < ALP;a++){
sol[i][a] += tmp[a] * (1 + (a != j));
tmp[a] += P[n - 1][a] - P[i][a];
}
}
for(int msk = 0;msk < (1 << ALP);msk++){
if(msk & (1 << j)) continue;
int uk = 0;
for(int a = 0;a < ALP;a++)
if(msk & (1 << a))
uk += 2 * P[n - 1][a];
uk += P[n - 1][j] - 1;
int naj = -1;
for(int k = 18;k >= 0;k--){
if(naj + (1 << k) >= (int)pos.size()) continue;
naj += (1 << k);
int kol = 0;
for(int b = 0;b < ALP;b++)
if(msk & (1 << b)) kol += 2 * P[pos[naj]][b];
kol += naj;
if(kol > uk - kol)
naj -= (1 << k);
}
for(int a = 0;a < ALP;a++)
if((1 << a) & (msk + (1 << j)))
cst[msk][j] += (naj > -1 ? sol[pos[naj]][a] : (tmp[a] * (1 + (a != j))));
}
}
int fin = f(0);
printf("%lld", fin / 2);
if(fin & 1) printf(".5");
printf("\n");
return 0;
}
Compilation message
passes.cpp: In function 'int main()':
passes.cpp:33:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | scanf("%s", s); n = strlen(s);
| ~~~~~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
4564 KB |
Output is correct |
2 |
Correct |
39 ms |
4408 KB |
Output is correct |
3 |
Correct |
42 ms |
4388 KB |
Output is correct |
4 |
Correct |
37 ms |
4384 KB |
Output is correct |
5 |
Correct |
34 ms |
4640 KB |
Output is correct |
6 |
Correct |
54 ms |
23696 KB |
Output is correct |
7 |
Correct |
56 ms |
27328 KB |
Output is correct |
8 |
Correct |
58 ms |
28864 KB |
Output is correct |
9 |
Correct |
60 ms |
28860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
4400 KB |
Output is correct |
2 |
Correct |
34 ms |
4416 KB |
Output is correct |
3 |
Correct |
53 ms |
4408 KB |
Output is correct |
4 |
Correct |
50 ms |
4308 KB |
Output is correct |
5 |
Correct |
40 ms |
4420 KB |
Output is correct |
6 |
Correct |
41 ms |
4388 KB |
Output is correct |
7 |
Correct |
53 ms |
4436 KB |
Output is correct |
8 |
Correct |
44 ms |
4328 KB |
Output is correct |
9 |
Correct |
48 ms |
4392 KB |
Output is correct |
10 |
Correct |
48 ms |
4388 KB |
Output is correct |
11 |
Correct |
49 ms |
4308 KB |
Output is correct |
12 |
Correct |
46 ms |
4308 KB |
Output is correct |
13 |
Correct |
48 ms |
4388 KB |
Output is correct |
14 |
Correct |
47 ms |
4388 KB |
Output is correct |
15 |
Correct |
43 ms |
4308 KB |
Output is correct |
16 |
Correct |
47 ms |
4308 KB |
Output is correct |
17 |
Correct |
47 ms |
4396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
4400 KB |
Output is correct |
2 |
Correct |
34 ms |
4416 KB |
Output is correct |
3 |
Correct |
53 ms |
4408 KB |
Output is correct |
4 |
Correct |
50 ms |
4308 KB |
Output is correct |
5 |
Correct |
40 ms |
4420 KB |
Output is correct |
6 |
Correct |
41 ms |
4388 KB |
Output is correct |
7 |
Correct |
53 ms |
4436 KB |
Output is correct |
8 |
Correct |
44 ms |
4328 KB |
Output is correct |
9 |
Correct |
48 ms |
4392 KB |
Output is correct |
10 |
Correct |
48 ms |
4388 KB |
Output is correct |
11 |
Correct |
49 ms |
4308 KB |
Output is correct |
12 |
Correct |
46 ms |
4308 KB |
Output is correct |
13 |
Correct |
48 ms |
4388 KB |
Output is correct |
14 |
Correct |
47 ms |
4388 KB |
Output is correct |
15 |
Correct |
43 ms |
4308 KB |
Output is correct |
16 |
Correct |
47 ms |
4308 KB |
Output is correct |
17 |
Correct |
47 ms |
4396 KB |
Output is correct |
18 |
Correct |
36 ms |
4308 KB |
Output is correct |
19 |
Correct |
33 ms |
4416 KB |
Output is correct |
20 |
Correct |
53 ms |
4412 KB |
Output is correct |
21 |
Correct |
56 ms |
4436 KB |
Output is correct |
22 |
Correct |
44 ms |
4308 KB |
Output is correct |
23 |
Correct |
40 ms |
4308 KB |
Output is correct |
24 |
Correct |
52 ms |
4408 KB |
Output is correct |
25 |
Correct |
44 ms |
4388 KB |
Output is correct |
26 |
Correct |
44 ms |
4388 KB |
Output is correct |
27 |
Correct |
46 ms |
4308 KB |
Output is correct |
28 |
Correct |
45 ms |
4396 KB |
Output is correct |
29 |
Correct |
45 ms |
4308 KB |
Output is correct |
30 |
Correct |
55 ms |
4308 KB |
Output is correct |
31 |
Correct |
47 ms |
4308 KB |
Output is correct |
32 |
Correct |
44 ms |
4308 KB |
Output is correct |
33 |
Correct |
51 ms |
4392 KB |
Output is correct |
34 |
Correct |
45 ms |
4308 KB |
Output is correct |
35 |
Correct |
42 ms |
6876 KB |
Output is correct |
36 |
Correct |
42 ms |
6884 KB |
Output is correct |
37 |
Correct |
82 ms |
6860 KB |
Output is correct |
38 |
Correct |
80 ms |
6648 KB |
Output is correct |
39 |
Correct |
46 ms |
6868 KB |
Output is correct |
40 |
Correct |
77 ms |
6752 KB |
Output is correct |
41 |
Correct |
85 ms |
6728 KB |
Output is correct |
42 |
Correct |
96 ms |
6724 KB |
Output is correct |
43 |
Correct |
85 ms |
6740 KB |
Output is correct |
44 |
Correct |
80 ms |
6720 KB |
Output is correct |
45 |
Correct |
81 ms |
6628 KB |
Output is correct |
46 |
Correct |
86 ms |
6720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
4564 KB |
Output is correct |
2 |
Correct |
39 ms |
4408 KB |
Output is correct |
3 |
Correct |
42 ms |
4388 KB |
Output is correct |
4 |
Correct |
37 ms |
4384 KB |
Output is correct |
5 |
Correct |
34 ms |
4640 KB |
Output is correct |
6 |
Correct |
54 ms |
23696 KB |
Output is correct |
7 |
Correct |
56 ms |
27328 KB |
Output is correct |
8 |
Correct |
58 ms |
28864 KB |
Output is correct |
9 |
Correct |
60 ms |
28860 KB |
Output is correct |
10 |
Correct |
39 ms |
4400 KB |
Output is correct |
11 |
Correct |
34 ms |
4416 KB |
Output is correct |
12 |
Correct |
53 ms |
4408 KB |
Output is correct |
13 |
Correct |
50 ms |
4308 KB |
Output is correct |
14 |
Correct |
40 ms |
4420 KB |
Output is correct |
15 |
Correct |
41 ms |
4388 KB |
Output is correct |
16 |
Correct |
53 ms |
4436 KB |
Output is correct |
17 |
Correct |
44 ms |
4328 KB |
Output is correct |
18 |
Correct |
48 ms |
4392 KB |
Output is correct |
19 |
Correct |
48 ms |
4388 KB |
Output is correct |
20 |
Correct |
49 ms |
4308 KB |
Output is correct |
21 |
Correct |
46 ms |
4308 KB |
Output is correct |
22 |
Correct |
48 ms |
4388 KB |
Output is correct |
23 |
Correct |
47 ms |
4388 KB |
Output is correct |
24 |
Correct |
43 ms |
4308 KB |
Output is correct |
25 |
Correct |
47 ms |
4308 KB |
Output is correct |
26 |
Correct |
47 ms |
4396 KB |
Output is correct |
27 |
Correct |
36 ms |
4308 KB |
Output is correct |
28 |
Correct |
33 ms |
4416 KB |
Output is correct |
29 |
Correct |
53 ms |
4412 KB |
Output is correct |
30 |
Correct |
56 ms |
4436 KB |
Output is correct |
31 |
Correct |
44 ms |
4308 KB |
Output is correct |
32 |
Correct |
40 ms |
4308 KB |
Output is correct |
33 |
Correct |
52 ms |
4408 KB |
Output is correct |
34 |
Correct |
44 ms |
4388 KB |
Output is correct |
35 |
Correct |
44 ms |
4388 KB |
Output is correct |
36 |
Correct |
46 ms |
4308 KB |
Output is correct |
37 |
Correct |
45 ms |
4396 KB |
Output is correct |
38 |
Correct |
45 ms |
4308 KB |
Output is correct |
39 |
Correct |
55 ms |
4308 KB |
Output is correct |
40 |
Correct |
47 ms |
4308 KB |
Output is correct |
41 |
Correct |
44 ms |
4308 KB |
Output is correct |
42 |
Correct |
51 ms |
4392 KB |
Output is correct |
43 |
Correct |
45 ms |
4308 KB |
Output is correct |
44 |
Correct |
42 ms |
6876 KB |
Output is correct |
45 |
Correct |
42 ms |
6884 KB |
Output is correct |
46 |
Correct |
82 ms |
6860 KB |
Output is correct |
47 |
Correct |
80 ms |
6648 KB |
Output is correct |
48 |
Correct |
46 ms |
6868 KB |
Output is correct |
49 |
Correct |
77 ms |
6752 KB |
Output is correct |
50 |
Correct |
85 ms |
6728 KB |
Output is correct |
51 |
Correct |
96 ms |
6724 KB |
Output is correct |
52 |
Correct |
85 ms |
6740 KB |
Output is correct |
53 |
Correct |
80 ms |
6720 KB |
Output is correct |
54 |
Correct |
81 ms |
6628 KB |
Output is correct |
55 |
Correct |
86 ms |
6720 KB |
Output is correct |
56 |
Correct |
40 ms |
4404 KB |
Output is correct |
57 |
Correct |
61 ms |
4308 KB |
Output is correct |
58 |
Correct |
36 ms |
4564 KB |
Output is correct |
59 |
Correct |
33 ms |
4392 KB |
Output is correct |
60 |
Correct |
31 ms |
4308 KB |
Output is correct |
61 |
Correct |
33 ms |
4488 KB |
Output is correct |
62 |
Correct |
34 ms |
4648 KB |
Output is correct |
63 |
Correct |
53 ms |
23752 KB |
Output is correct |
64 |
Correct |
58 ms |
27408 KB |
Output is correct |
65 |
Correct |
56 ms |
28892 KB |
Output is correct |
66 |
Correct |
57 ms |
28872 KB |
Output is correct |
67 |
Correct |
34 ms |
4308 KB |
Output is correct |
68 |
Correct |
34 ms |
4436 KB |
Output is correct |
69 |
Correct |
54 ms |
4416 KB |
Output is correct |
70 |
Correct |
51 ms |
4416 KB |
Output is correct |
71 |
Correct |
45 ms |
4416 KB |
Output is correct |
72 |
Correct |
41 ms |
4396 KB |
Output is correct |
73 |
Correct |
52 ms |
4428 KB |
Output is correct |
74 |
Correct |
46 ms |
4392 KB |
Output is correct |
75 |
Correct |
43 ms |
4392 KB |
Output is correct |
76 |
Correct |
44 ms |
4308 KB |
Output is correct |
77 |
Correct |
44 ms |
4308 KB |
Output is correct |
78 |
Correct |
49 ms |
4400 KB |
Output is correct |
79 |
Correct |
61 ms |
4400 KB |
Output is correct |
80 |
Correct |
55 ms |
4308 KB |
Output is correct |
81 |
Correct |
44 ms |
4428 KB |
Output is correct |
82 |
Correct |
43 ms |
4396 KB |
Output is correct |
83 |
Correct |
44 ms |
4308 KB |
Output is correct |
84 |
Correct |
39 ms |
6960 KB |
Output is correct |
85 |
Correct |
37 ms |
6964 KB |
Output is correct |
86 |
Correct |
86 ms |
6784 KB |
Output is correct |
87 |
Correct |
83 ms |
6768 KB |
Output is correct |
88 |
Correct |
57 ms |
6956 KB |
Output is correct |
89 |
Correct |
79 ms |
6756 KB |
Output is correct |
90 |
Correct |
79 ms |
6740 KB |
Output is correct |
91 |
Correct |
80 ms |
6780 KB |
Output is correct |
92 |
Correct |
88 ms |
6852 KB |
Output is correct |
93 |
Correct |
85 ms |
6740 KB |
Output is correct |
94 |
Correct |
80 ms |
6748 KB |
Output is correct |
95 |
Correct |
78 ms |
6732 KB |
Output is correct |
96 |
Correct |
148 ms |
28176 KB |
Output is correct |
97 |
Correct |
56 ms |
4308 KB |
Output is correct |
98 |
Correct |
151 ms |
28144 KB |
Output is correct |
99 |
Correct |
72 ms |
28860 KB |
Output is correct |
100 |
Correct |
47 ms |
4308 KB |
Output is correct |
101 |
Correct |
146 ms |
28056 KB |
Output is correct |
102 |
Correct |
167 ms |
28228 KB |
Output is correct |
103 |
Correct |
148 ms |
28108 KB |
Output is correct |
104 |
Correct |
152 ms |
28168 KB |
Output is correct |
105 |
Correct |
156 ms |
28144 KB |
Output is correct |
106 |
Correct |
154 ms |
28188 KB |
Output is correct |
107 |
Correct |
148 ms |
28252 KB |
Output is correct |
108 |
Correct |
145 ms |
28108 KB |
Output is correct |
109 |
Correct |
171 ms |
28276 KB |
Output is correct |