#include<bits/stdc++.h>
using namespace std;
#define int long long
const int inf=1e18;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
string s;
cin >> s;
int n=s.size();
vector<int> a(n);
map<int, int> num; int ind=1;
vector<vector<int> > group(15, vector<int> (1, 0));
for(int i=0; i<n; i++){
if(num[s[i]]==0){
num[s[i]]=ind;
ind++;
}
a[i]=num[s[i]]-1;
group[a[i]].push_back(i);
}
int g=ind-1;
for(int i=0; i<15; i++){
group[i].push_back(n-1);
}
vector<vector<int> > prefix(n+1, vector<int> (g, 0));
vector<vector<int> > suffix(n+1, vector<int> (g, 0)); //exklusiv
for(int i=0; i<n; i++){
prefix[i+1]=prefix[i];
prefix[i+1][a[i]]++;
}
for(int i=n-1; i>0; i--){
suffix[i-1]=suffix[i];
suffix[i-1][a[i]]++;
}
vector<vector<vector<int> > > prefixsum(n+1, vector<vector<int> > (g, vector<int> (g, 0)));
vector<vector<vector<int> > > suffixsum(n+1, vector<vector<int> > (g, vector<int> (g, 0)));
for(int i=1; i<n; i++){
prefixsum[i]=prefixsum[i-1];
for(int j=0; j<g; j++){
prefixsum[i][a[i]][j]+=prefix[i][j];
}
}
for(int i=n-2; i>=0; i--){
suffixsum[i]=suffixsum[i+1];
for(int j=0; j<g; j++){
suffixsum[i][a[i]][j]+=suffix[i][j];
}
}
auto cost=[&](int mask, int lastgroup, int lastleft){
if(lastleft+1==(int)group[lastgroup].size()){
return inf;
}
int indleft=group[lastgroup][lastleft];
int indright=group[lastgroup][lastleft+1];
//cout << "left: " << indleft << " right: " << indright << "\n" << flush;
int ans=0;
for(int i=0; i<g; i++){
if((mask>>i)%2==0 || i==lastgroup) continue;
ans+=prefixsum[indleft][lastgroup][i]*2;
ans+=suffixsum[indright][lastgroup][i]*2;
}
ans+=prefixsum[indleft][lastgroup][lastgroup];
ans+=suffixsum[indright][lastgroup][lastgroup];
return ans;
};
auto mincost=[&](int mask, int lastgroup){
//cout << "mincost:\n";
//cout << "mask: " << mask << " last group: " << lastgroup << "\n";
int l=0; int r=group[lastgroup].size()-2;
int ans=r;
while(l<=r){
//cout << "bs " << l << " " << r << "\n" << flush;
int m=l+(r-l)/2;
if(cost(mask, lastgroup, m)<=cost(mask, lastgroup, m+1)){
ans=m;
r=m-1;
}
else{
l=m+1;
}
}
//cout << "mincost " << mask << " " << lastgroup << " " << ans << " " << cost(mask, lastgroup, ans) << "\n" << flush;
return cost(mask, lastgroup, ans);
};
vector<int> dp((1<<g), inf);
dp[0]=0;
for(int i=1; i<(1<<g); i++){
for(int last=0; last<g; last++){
if((i>>last)%2==0) continue;
dp[i]=min(dp[i], dp[i-(1<<last)]+mincost(i, last));
}
//cout << i << " " << dp[i] << "\n" << flush;
}
double ans=(double)dp[(1<<g)-1]/(double)2;
cout.precision(100);
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
33 ms |
23732 KB |
Output is correct |
7 |
Correct |
41 ms |
28320 KB |
Output is correct |
8 |
Correct |
43 ms |
30084 KB |
Output is correct |
9 |
Correct |
42 ms |
30176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
0 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
272 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
5 ms |
3284 KB |
Output is correct |
36 |
Correct |
5 ms |
3284 KB |
Output is correct |
37 |
Correct |
32 ms |
27160 KB |
Output is correct |
38 |
Correct |
36 ms |
27244 KB |
Output is correct |
39 |
Correct |
33 ms |
27092 KB |
Output is correct |
40 |
Correct |
32 ms |
26964 KB |
Output is correct |
41 |
Correct |
33 ms |
26512 KB |
Output is correct |
42 |
Correct |
37 ms |
26608 KB |
Output is correct |
43 |
Correct |
32 ms |
26580 KB |
Output is correct |
44 |
Correct |
31 ms |
26576 KB |
Output is correct |
45 |
Correct |
32 ms |
26580 KB |
Output is correct |
46 |
Correct |
34 ms |
26492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
33 ms |
23732 KB |
Output is correct |
7 |
Correct |
41 ms |
28320 KB |
Output is correct |
8 |
Correct |
43 ms |
30084 KB |
Output is correct |
9 |
Correct |
42 ms |
30176 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
0 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
272 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
5 ms |
3284 KB |
Output is correct |
45 |
Correct |
5 ms |
3284 KB |
Output is correct |
46 |
Correct |
32 ms |
27160 KB |
Output is correct |
47 |
Correct |
36 ms |
27244 KB |
Output is correct |
48 |
Correct |
33 ms |
27092 KB |
Output is correct |
49 |
Correct |
32 ms |
26964 KB |
Output is correct |
50 |
Correct |
33 ms |
26512 KB |
Output is correct |
51 |
Correct |
37 ms |
26608 KB |
Output is correct |
52 |
Correct |
32 ms |
26580 KB |
Output is correct |
53 |
Correct |
31 ms |
26576 KB |
Output is correct |
54 |
Correct |
32 ms |
26580 KB |
Output is correct |
55 |
Correct |
34 ms |
26492 KB |
Output is correct |
56 |
Correct |
1 ms |
212 KB |
Output is correct |
57 |
Correct |
67 ms |
724 KB |
Output is correct |
58 |
Correct |
1 ms |
468 KB |
Output is correct |
59 |
Correct |
0 ms |
312 KB |
Output is correct |
60 |
Correct |
0 ms |
212 KB |
Output is correct |
61 |
Correct |
0 ms |
212 KB |
Output is correct |
62 |
Correct |
1 ms |
596 KB |
Output is correct |
63 |
Correct |
41 ms |
23760 KB |
Output is correct |
64 |
Correct |
48 ms |
28296 KB |
Output is correct |
65 |
Correct |
44 ms |
30140 KB |
Output is correct |
66 |
Correct |
44 ms |
30120 KB |
Output is correct |
67 |
Correct |
1 ms |
212 KB |
Output is correct |
68 |
Correct |
0 ms |
340 KB |
Output is correct |
69 |
Correct |
1 ms |
468 KB |
Output is correct |
70 |
Correct |
1 ms |
340 KB |
Output is correct |
71 |
Correct |
1 ms |
340 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
1 ms |
340 KB |
Output is correct |
74 |
Correct |
1 ms |
344 KB |
Output is correct |
75 |
Correct |
1 ms |
340 KB |
Output is correct |
76 |
Correct |
0 ms |
340 KB |
Output is correct |
77 |
Correct |
1 ms |
340 KB |
Output is correct |
78 |
Correct |
1 ms |
340 KB |
Output is correct |
79 |
Correct |
0 ms |
340 KB |
Output is correct |
80 |
Correct |
1 ms |
340 KB |
Output is correct |
81 |
Correct |
0 ms |
340 KB |
Output is correct |
82 |
Correct |
0 ms |
340 KB |
Output is correct |
83 |
Correct |
1 ms |
340 KB |
Output is correct |
84 |
Correct |
4 ms |
3284 KB |
Output is correct |
85 |
Correct |
5 ms |
3284 KB |
Output is correct |
86 |
Correct |
34 ms |
27148 KB |
Output is correct |
87 |
Correct |
35 ms |
27088 KB |
Output is correct |
88 |
Correct |
33 ms |
27228 KB |
Output is correct |
89 |
Correct |
32 ms |
26964 KB |
Output is correct |
90 |
Correct |
33 ms |
26580 KB |
Output is correct |
91 |
Correct |
33 ms |
26580 KB |
Output is correct |
92 |
Correct |
31 ms |
26612 KB |
Output is correct |
93 |
Correct |
36 ms |
26584 KB |
Output is correct |
94 |
Correct |
33 ms |
26572 KB |
Output is correct |
95 |
Correct |
35 ms |
26580 KB |
Output is correct |
96 |
Correct |
730 ms |
484556 KB |
Output is correct |
97 |
Correct |
34 ms |
596 KB |
Output is correct |
98 |
Correct |
717 ms |
484544 KB |
Output is correct |
99 |
Correct |
526 ms |
484484 KB |
Output is correct |
100 |
Correct |
52 ms |
596 KB |
Output is correct |
101 |
Correct |
667 ms |
484584 KB |
Output is correct |
102 |
Correct |
695 ms |
484632 KB |
Output is correct |
103 |
Correct |
731 ms |
482776 KB |
Output is correct |
104 |
Correct |
676 ms |
482812 KB |
Output is correct |
105 |
Correct |
680 ms |
482852 KB |
Output is correct |
106 |
Correct |
693 ms |
482672 KB |
Output is correct |
107 |
Correct |
676 ms |
482724 KB |
Output is correct |
108 |
Correct |
554 ms |
453964 KB |
Output is correct |
109 |
Correct |
776 ms |
482692 KB |
Output is correct |