#include "bits/stdc++.h"
using namespace std;
#define ar array
typedef int64_t ll;
#define int ll
const int G = 15;
const int N = 1e5 + 5;
vector<int> gop[G];
int dp[1 << G];
int pref[G][G][N], cc[G][N];
int suff[G][G][N];
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
string s; cin>>s;
int n = s.size(), g = 0;
for(int i=0;i<n;i++){
g = max(g, (ll)s[i] - 'A' + 1);
gop[s[i] - 'A'].push_back(i);
}
for(int i=0;i<n;i++){
if(i){
for(int j=0;j<g;j++) cc[j][i] = cc[j][i-1];
}
cc[s[i] - 'A'][i]++;
}
for(int a=0;a<g;a++){
gop[a].push_back(n);
gop[a].push_back(n);
for(int b=0;b<g;b++){
for(int i=0;i<n;i++){
if(i) pref[a][b][i] = pref[a][b][i - 1];
if(s[i] - 'A' == a){
pref[a][b][i] += cc[b][i] * 2;
}
}
for(int i=n-1;~i;i--){
suff[a][b][i] = suff[a][b][i + 1];
if(s[i] - 'A' == a){
suff[a][b][i] += (cc[b][n - 1] - cc[b][i]) * 2;
}
}
}
}
memset(dp, 63, sizeof dp);
dp[0] = 0;
for(int mask=0;mask < (1 << g);mask++){
//~ int b = __builtin_popcount(mask);
//~ if((mask >> b) == 0){
//~ for(int k=0;k<g;k++) cout<<(mask >> k & 1);
//~ cout<<" : "<<dp[mask]<<"\n";
//~ }
for(int j=0;j<g;j++){
if(mask >> j & 1) continue;
auto check = [&](int i){
int res = 0;
for(int k=0;k<g;k++){
if(mask >> k & 1){
if(i) res += pref[j][k][i - 1];
res += suff[j][k][i];
}
}
if(i){
int m = cc[j][i - 1];
if(m) res = res + m * (m - 1) / 2;
}
int m = cc[j][n - 1] - (i ? cc[j][i - 1] : 0);
if(m) res = res + m * (m - 1) / 2;
return res;
};
int l = 0, r = gop[j].size() - 1;
while(l + 1 < r - 1){
int m = (l + r) >> 1;
if(check(gop[j][m]) < check(gop[j][m + 1])) r = m + 1;
else l = m;
}
int res = min({check(gop[j][l]), check(gop[j][r]), check(gop[j][l + 1])});
//~ if((mask >> b) == 0){
//~ for(int k=0;k<(int)gop[j].size();k++){
//~ cout<<check(gop[j][k])<<" ";
//~ } cout<<"\n";
//~ cout<<res<<"\n";
//~ }
dp[mask | (1 << j)] = min(dp[mask | (1 << j)], dp[mask] + res);
}
}
int res = dp[(1 << g) - 1];
cout<<res/2;
if(res&1) cout<<".5";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
4 ms |
3340 KB |
Output is correct |
7 |
Correct |
5 ms |
3852 KB |
Output is correct |
8 |
Correct |
5 ms |
4108 KB |
Output is correct |
9 |
Correct |
5 ms |
4096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
1236 KB |
Output is correct |
4 |
Correct |
1 ms |
1236 KB |
Output is correct |
5 |
Correct |
1 ms |
1236 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
1 ms |
1236 KB |
Output is correct |
8 |
Correct |
1 ms |
1108 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
1 ms |
1108 KB |
Output is correct |
12 |
Correct |
2 ms |
1108 KB |
Output is correct |
13 |
Correct |
1 ms |
1108 KB |
Output is correct |
14 |
Correct |
1 ms |
1108 KB |
Output is correct |
15 |
Correct |
1 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
1108 KB |
Output is correct |
17 |
Correct |
1 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
1236 KB |
Output is correct |
4 |
Correct |
1 ms |
1236 KB |
Output is correct |
5 |
Correct |
1 ms |
1236 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
1 ms |
1236 KB |
Output is correct |
8 |
Correct |
1 ms |
1108 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
1 ms |
1108 KB |
Output is correct |
12 |
Correct |
2 ms |
1108 KB |
Output is correct |
13 |
Correct |
1 ms |
1108 KB |
Output is correct |
14 |
Correct |
1 ms |
1108 KB |
Output is correct |
15 |
Correct |
1 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
1108 KB |
Output is correct |
17 |
Correct |
1 ms |
1108 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
724 KB |
Output is correct |
20 |
Correct |
1 ms |
1236 KB |
Output is correct |
21 |
Correct |
2 ms |
1236 KB |
Output is correct |
22 |
Correct |
2 ms |
1236 KB |
Output is correct |
23 |
Correct |
2 ms |
1108 KB |
Output is correct |
24 |
Correct |
1 ms |
1236 KB |
Output is correct |
25 |
Correct |
1 ms |
1108 KB |
Output is correct |
26 |
Correct |
1 ms |
1108 KB |
Output is correct |
27 |
Correct |
1 ms |
1108 KB |
Output is correct |
28 |
Correct |
1 ms |
1108 KB |
Output is correct |
29 |
Correct |
1 ms |
1108 KB |
Output is correct |
30 |
Correct |
1 ms |
1108 KB |
Output is correct |
31 |
Correct |
1 ms |
1108 KB |
Output is correct |
32 |
Correct |
1 ms |
1108 KB |
Output is correct |
33 |
Correct |
1 ms |
1108 KB |
Output is correct |
34 |
Correct |
1 ms |
1108 KB |
Output is correct |
35 |
Correct |
12 ms |
12144 KB |
Output is correct |
36 |
Correct |
10 ms |
12244 KB |
Output is correct |
37 |
Correct |
18 ms |
18260 KB |
Output is correct |
38 |
Correct |
15 ms |
18228 KB |
Output is correct |
39 |
Correct |
16 ms |
18416 KB |
Output is correct |
40 |
Correct |
17 ms |
18132 KB |
Output is correct |
41 |
Correct |
21 ms |
17908 KB |
Output is correct |
42 |
Correct |
17 ms |
17876 KB |
Output is correct |
43 |
Correct |
17 ms |
17944 KB |
Output is correct |
44 |
Correct |
17 ms |
17876 KB |
Output is correct |
45 |
Correct |
18 ms |
17884 KB |
Output is correct |
46 |
Correct |
16 ms |
17988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
4 ms |
3340 KB |
Output is correct |
7 |
Correct |
5 ms |
3852 KB |
Output is correct |
8 |
Correct |
5 ms |
4108 KB |
Output is correct |
9 |
Correct |
5 ms |
4096 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
724 KB |
Output is correct |
12 |
Correct |
1 ms |
1236 KB |
Output is correct |
13 |
Correct |
1 ms |
1236 KB |
Output is correct |
14 |
Correct |
1 ms |
1236 KB |
Output is correct |
15 |
Correct |
1 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
1236 KB |
Output is correct |
17 |
Correct |
1 ms |
1108 KB |
Output is correct |
18 |
Correct |
1 ms |
1108 KB |
Output is correct |
19 |
Correct |
1 ms |
1108 KB |
Output is correct |
20 |
Correct |
1 ms |
1108 KB |
Output is correct |
21 |
Correct |
2 ms |
1108 KB |
Output is correct |
22 |
Correct |
1 ms |
1108 KB |
Output is correct |
23 |
Correct |
1 ms |
1108 KB |
Output is correct |
24 |
Correct |
1 ms |
1108 KB |
Output is correct |
25 |
Correct |
1 ms |
1108 KB |
Output is correct |
26 |
Correct |
1 ms |
1108 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
724 KB |
Output is correct |
29 |
Correct |
1 ms |
1236 KB |
Output is correct |
30 |
Correct |
2 ms |
1236 KB |
Output is correct |
31 |
Correct |
2 ms |
1236 KB |
Output is correct |
32 |
Correct |
2 ms |
1108 KB |
Output is correct |
33 |
Correct |
1 ms |
1236 KB |
Output is correct |
34 |
Correct |
1 ms |
1108 KB |
Output is correct |
35 |
Correct |
1 ms |
1108 KB |
Output is correct |
36 |
Correct |
1 ms |
1108 KB |
Output is correct |
37 |
Correct |
1 ms |
1108 KB |
Output is correct |
38 |
Correct |
1 ms |
1108 KB |
Output is correct |
39 |
Correct |
1 ms |
1108 KB |
Output is correct |
40 |
Correct |
1 ms |
1108 KB |
Output is correct |
41 |
Correct |
1 ms |
1108 KB |
Output is correct |
42 |
Correct |
1 ms |
1108 KB |
Output is correct |
43 |
Correct |
1 ms |
1108 KB |
Output is correct |
44 |
Correct |
12 ms |
12144 KB |
Output is correct |
45 |
Correct |
10 ms |
12244 KB |
Output is correct |
46 |
Correct |
18 ms |
18260 KB |
Output is correct |
47 |
Correct |
15 ms |
18228 KB |
Output is correct |
48 |
Correct |
16 ms |
18416 KB |
Output is correct |
49 |
Correct |
17 ms |
18132 KB |
Output is correct |
50 |
Correct |
21 ms |
17908 KB |
Output is correct |
51 |
Correct |
17 ms |
17876 KB |
Output is correct |
52 |
Correct |
17 ms |
17944 KB |
Output is correct |
53 |
Correct |
17 ms |
17876 KB |
Output is correct |
54 |
Correct |
18 ms |
17884 KB |
Output is correct |
55 |
Correct |
16 ms |
17988 KB |
Output is correct |
56 |
Correct |
56 ms |
3164 KB |
Output is correct |
57 |
Correct |
51 ms |
3156 KB |
Output is correct |
58 |
Correct |
1 ms |
596 KB |
Output is correct |
59 |
Correct |
0 ms |
468 KB |
Output is correct |
60 |
Correct |
1 ms |
596 KB |
Output is correct |
61 |
Correct |
0 ms |
468 KB |
Output is correct |
62 |
Correct |
1 ms |
596 KB |
Output is correct |
63 |
Correct |
4 ms |
3340 KB |
Output is correct |
64 |
Correct |
4 ms |
3808 KB |
Output is correct |
65 |
Correct |
5 ms |
4076 KB |
Output is correct |
66 |
Correct |
5 ms |
4108 KB |
Output is correct |
67 |
Correct |
1 ms |
596 KB |
Output is correct |
68 |
Correct |
1 ms |
724 KB |
Output is correct |
69 |
Correct |
1 ms |
1236 KB |
Output is correct |
70 |
Correct |
1 ms |
1236 KB |
Output is correct |
71 |
Correct |
1 ms |
1236 KB |
Output is correct |
72 |
Correct |
1 ms |
1108 KB |
Output is correct |
73 |
Correct |
1 ms |
1236 KB |
Output is correct |
74 |
Correct |
1 ms |
1108 KB |
Output is correct |
75 |
Correct |
1 ms |
1108 KB |
Output is correct |
76 |
Correct |
1 ms |
1108 KB |
Output is correct |
77 |
Correct |
1 ms |
1108 KB |
Output is correct |
78 |
Correct |
1 ms |
1108 KB |
Output is correct |
79 |
Correct |
1 ms |
1108 KB |
Output is correct |
80 |
Correct |
1 ms |
1220 KB |
Output is correct |
81 |
Correct |
1 ms |
1108 KB |
Output is correct |
82 |
Correct |
1 ms |
1108 KB |
Output is correct |
83 |
Correct |
2 ms |
1108 KB |
Output is correct |
84 |
Correct |
9 ms |
12144 KB |
Output is correct |
85 |
Correct |
11 ms |
12144 KB |
Output is correct |
86 |
Correct |
16 ms |
18368 KB |
Output is correct |
87 |
Correct |
20 ms |
18260 KB |
Output is correct |
88 |
Correct |
15 ms |
18384 KB |
Output is correct |
89 |
Correct |
17 ms |
18132 KB |
Output is correct |
90 |
Correct |
15 ms |
17876 KB |
Output is correct |
91 |
Correct |
18 ms |
17876 KB |
Output is correct |
92 |
Correct |
17 ms |
17876 KB |
Output is correct |
93 |
Correct |
17 ms |
17876 KB |
Output is correct |
94 |
Correct |
16 ms |
17876 KB |
Output is correct |
95 |
Correct |
19 ms |
17876 KB |
Output is correct |
96 |
Correct |
589 ms |
365772 KB |
Output is correct |
97 |
Correct |
35 ms |
2900 KB |
Output is correct |
98 |
Correct |
488 ms |
365568 KB |
Output is correct |
99 |
Correct |
315 ms |
365648 KB |
Output is correct |
100 |
Correct |
51 ms |
3100 KB |
Output is correct |
101 |
Correct |
463 ms |
365752 KB |
Output is correct |
102 |
Correct |
701 ms |
365904 KB |
Output is correct |
103 |
Correct |
562 ms |
365676 KB |
Output is correct |
104 |
Correct |
574 ms |
365940 KB |
Output is correct |
105 |
Correct |
554 ms |
365808 KB |
Output is correct |
106 |
Correct |
549 ms |
365824 KB |
Output is correct |
107 |
Correct |
606 ms |
365968 KB |
Output is correct |
108 |
Correct |
393 ms |
319644 KB |
Output is correct |
109 |
Correct |
522 ms |
365848 KB |
Output is correct |