#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define a first
#define b second
#define pb push_back
#define endl '\n'
llo dp[1<<16];
llo it[100001];
llo ans[100001];
llo vis[100001];
vector<llo> pre[20];
llo pre4[15][15][100001];
llo pre2[15][15][100001];
llo pre3[15][100001];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string s;
cin>>s;
llo m=0;
llo n=s.size();
for(llo i=0;i<n;i++){
m=max(m,(llo)(s[i]-'A'+1));
it[i]=s[i]-'A';
pre[it[i]].pb(i);
}
for(llo i=0;i<(1<<m);i++){
dp[i]=1e18;
}
dp[0]=0;
for(int i=0;i<m;i++){
for(int j=0;j<m;j++){
if(i==j){
continue;
}
int su=0;
for(int ii=0;ii<n;ii++){
pre4[i][j][ii+1]=pre4[i][j][ii];
if(it[ii]==i){
pre4[i][j][ii+1]+=su;
// su++;
}
else if(it[ii]==j){
su+=2;
}
}
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
pre3[j][i+1]=pre3[j][i];
if(it[i]==j){
pre3[j][i+1]+=2;
}
}
}
for(int i=0;i<m;i++){
for(int j=0;j<m;j++){
if(i==j){
continue;
}
int su=0;
for(int ii=n-1;ii>=0;ii--){
pre2[i][j][ii]=pre2[i][j][ii+1];
if(it[ii]==i){
pre2[i][j][ii]+=su;
//su++;
}
else if(it[ii]==j){
su+=2;
}
}
}
}
//cout<<pre[1].size()<<endl;
for(llo i=0;i<(1<<m);i++){
vector<int> ss;
for(int j=0;j<m;j++){
if((1<<j)&i){
ss.pb(j);
}
}
/*for(llo j=0;j<n;j++){
if((1<<it[j])&i){
vis[j+1]=2;
}
else{
vis[j+1]=0;
}
vis[j+1]+=vis[j];
}*/
for(llo j=0;j<m;j++){
if((1<<j)&i){
continue;
}
if(pre[j].size()==0){
dp[i+(1<<j)]=min(dp[i+(1<<j)],dp[i]);
continue;
}
llo low=-1;
llo xx=pre[j].size();
for(int jj=17;jj>=0;jj--){
if(low+(1<<jj)<(llo)pre[j].size()){
//check if left<=right
int ind=pre[j][low+(1<<jj)];
llo su=0;
llo su2=0;
for(auto ii:ss){
su+=pre3[ii][ind];
su2+=pre3[ii][n]-pre3[ii][ind];
}
su+=low+(1<<jj);
su2+=(xx-(low+(1<<jj))-1);
if(su<=su2){
low+=(1<<jj);
}
}
}
llo cur=0;
cur+=((low*(low+1)))/2;
llo low2=xx-low-1;
cur+=((low2-1)*(low2))/2;
llo ind=-1;
// cout<<cur<<":"<<low+1<<":"<<low2<<endl;
if(low>=0){
ind=pre[j][low];
}
for(auto jj:ss){
cur+=pre4[j][jj][ind+1];
cur+=pre2[j][jj][ind+1];
}
//cout<<i<<":"<<j<<":"<<low<<endl;
dp[i+(1<<j)]=min(dp[i+(1<<j)],dp[i]+cur);
/*llo le=pre[j].size();
llo cur2=dp[i];
llo su=0;
for(auto jj:pre[j]){
llo cur3=min(su+vis[jj],le-su-1+vis[n]-vis[jj]);
cur2+=cur3;
su++;
//cout<<j<<":"<<cur3<<endl;
}
*/
}
}
llo aa=dp[(1<<m)-1];
// cout<<aa<<endl;
if(aa%2==0){
cout<<(aa/2)<<endl;
}
else{
cout<<(aa/2)<<".5"<<endl;
/*long double xx=aa;
xx/=(long double)2;
cout<<setprecision(2)<<xx<<endl;*/
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
2428 KB |
Output is correct |
7 |
Correct |
3 ms |
2828 KB |
Output is correct |
8 |
Correct |
3 ms |
2956 KB |
Output is correct |
9 |
Correct |
3 ms |
2956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
852 KB |
Output is correct |
7 |
Correct |
1 ms |
852 KB |
Output is correct |
8 |
Correct |
1 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
852 KB |
Output is correct |
10 |
Correct |
1 ms |
852 KB |
Output is correct |
11 |
Correct |
1 ms |
852 KB |
Output is correct |
12 |
Correct |
1 ms |
852 KB |
Output is correct |
13 |
Correct |
1 ms |
852 KB |
Output is correct |
14 |
Correct |
1 ms |
852 KB |
Output is correct |
15 |
Correct |
1 ms |
852 KB |
Output is correct |
16 |
Correct |
1 ms |
852 KB |
Output is correct |
17 |
Correct |
1 ms |
852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
852 KB |
Output is correct |
7 |
Correct |
1 ms |
852 KB |
Output is correct |
8 |
Correct |
1 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
852 KB |
Output is correct |
10 |
Correct |
1 ms |
852 KB |
Output is correct |
11 |
Correct |
1 ms |
852 KB |
Output is correct |
12 |
Correct |
1 ms |
852 KB |
Output is correct |
13 |
Correct |
1 ms |
852 KB |
Output is correct |
14 |
Correct |
1 ms |
852 KB |
Output is correct |
15 |
Correct |
1 ms |
852 KB |
Output is correct |
16 |
Correct |
1 ms |
852 KB |
Output is correct |
17 |
Correct |
1 ms |
852 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
852 KB |
Output is correct |
21 |
Correct |
1 ms |
852 KB |
Output is correct |
22 |
Correct |
1 ms |
852 KB |
Output is correct |
23 |
Correct |
1 ms |
852 KB |
Output is correct |
24 |
Correct |
1 ms |
852 KB |
Output is correct |
25 |
Correct |
1 ms |
852 KB |
Output is correct |
26 |
Correct |
1 ms |
852 KB |
Output is correct |
27 |
Correct |
1 ms |
852 KB |
Output is correct |
28 |
Correct |
1 ms |
852 KB |
Output is correct |
29 |
Correct |
1 ms |
852 KB |
Output is correct |
30 |
Correct |
1 ms |
852 KB |
Output is correct |
31 |
Correct |
1 ms |
852 KB |
Output is correct |
32 |
Correct |
1 ms |
852 KB |
Output is correct |
33 |
Correct |
1 ms |
852 KB |
Output is correct |
34 |
Correct |
1 ms |
852 KB |
Output is correct |
35 |
Correct |
7 ms |
10708 KB |
Output is correct |
36 |
Correct |
7 ms |
10708 KB |
Output is correct |
37 |
Correct |
12 ms |
16460 KB |
Output is correct |
38 |
Correct |
13 ms |
16468 KB |
Output is correct |
39 |
Correct |
13 ms |
16632 KB |
Output is correct |
40 |
Correct |
12 ms |
16340 KB |
Output is correct |
41 |
Correct |
12 ms |
16212 KB |
Output is correct |
42 |
Correct |
14 ms |
16136 KB |
Output is correct |
43 |
Correct |
12 ms |
16212 KB |
Output is correct |
44 |
Correct |
14 ms |
16212 KB |
Output is correct |
45 |
Correct |
16 ms |
16212 KB |
Output is correct |
46 |
Correct |
13 ms |
16176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
2428 KB |
Output is correct |
7 |
Correct |
3 ms |
2828 KB |
Output is correct |
8 |
Correct |
3 ms |
2956 KB |
Output is correct |
9 |
Correct |
3 ms |
2956 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 |
852 KB |
Output is correct |
13 |
Correct |
1 ms |
852 KB |
Output is correct |
14 |
Correct |
1 ms |
852 KB |
Output is correct |
15 |
Correct |
1 ms |
852 KB |
Output is correct |
16 |
Correct |
1 ms |
852 KB |
Output is correct |
17 |
Correct |
1 ms |
852 KB |
Output is correct |
18 |
Correct |
1 ms |
852 KB |
Output is correct |
19 |
Correct |
1 ms |
852 KB |
Output is correct |
20 |
Correct |
1 ms |
852 KB |
Output is correct |
21 |
Correct |
1 ms |
852 KB |
Output is correct |
22 |
Correct |
1 ms |
852 KB |
Output is correct |
23 |
Correct |
1 ms |
852 KB |
Output is correct |
24 |
Correct |
1 ms |
852 KB |
Output is correct |
25 |
Correct |
1 ms |
852 KB |
Output is correct |
26 |
Correct |
1 ms |
852 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
852 KB |
Output is correct |
30 |
Correct |
1 ms |
852 KB |
Output is correct |
31 |
Correct |
1 ms |
852 KB |
Output is correct |
32 |
Correct |
1 ms |
852 KB |
Output is correct |
33 |
Correct |
1 ms |
852 KB |
Output is correct |
34 |
Correct |
1 ms |
852 KB |
Output is correct |
35 |
Correct |
1 ms |
852 KB |
Output is correct |
36 |
Correct |
1 ms |
852 KB |
Output is correct |
37 |
Correct |
1 ms |
852 KB |
Output is correct |
38 |
Correct |
1 ms |
852 KB |
Output is correct |
39 |
Correct |
1 ms |
852 KB |
Output is correct |
40 |
Correct |
1 ms |
852 KB |
Output is correct |
41 |
Correct |
1 ms |
852 KB |
Output is correct |
42 |
Correct |
1 ms |
852 KB |
Output is correct |
43 |
Correct |
1 ms |
852 KB |
Output is correct |
44 |
Correct |
7 ms |
10708 KB |
Output is correct |
45 |
Correct |
7 ms |
10708 KB |
Output is correct |
46 |
Correct |
12 ms |
16460 KB |
Output is correct |
47 |
Correct |
13 ms |
16468 KB |
Output is correct |
48 |
Correct |
13 ms |
16632 KB |
Output is correct |
49 |
Correct |
12 ms |
16340 KB |
Output is correct |
50 |
Correct |
12 ms |
16212 KB |
Output is correct |
51 |
Correct |
14 ms |
16136 KB |
Output is correct |
52 |
Correct |
12 ms |
16212 KB |
Output is correct |
53 |
Correct |
14 ms |
16212 KB |
Output is correct |
54 |
Correct |
16 ms |
16212 KB |
Output is correct |
55 |
Correct |
13 ms |
16176 KB |
Output is correct |
56 |
Correct |
21 ms |
3080 KB |
Output is correct |
57 |
Correct |
23 ms |
3028 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
0 ms |
340 KB |
Output is correct |
60 |
Correct |
0 ms |
340 KB |
Output is correct |
61 |
Correct |
0 ms |
340 KB |
Output is correct |
62 |
Correct |
1 ms |
340 KB |
Output is correct |
63 |
Correct |
3 ms |
2444 KB |
Output is correct |
64 |
Correct |
3 ms |
2828 KB |
Output is correct |
65 |
Correct |
3 ms |
2956 KB |
Output is correct |
66 |
Correct |
3 ms |
2956 KB |
Output is correct |
67 |
Correct |
1 ms |
340 KB |
Output is correct |
68 |
Correct |
1 ms |
340 KB |
Output is correct |
69 |
Correct |
1 ms |
852 KB |
Output is correct |
70 |
Correct |
1 ms |
852 KB |
Output is correct |
71 |
Correct |
1 ms |
852 KB |
Output is correct |
72 |
Correct |
1 ms |
852 KB |
Output is correct |
73 |
Correct |
1 ms |
852 KB |
Output is correct |
74 |
Correct |
1 ms |
852 KB |
Output is correct |
75 |
Correct |
1 ms |
852 KB |
Output is correct |
76 |
Correct |
1 ms |
852 KB |
Output is correct |
77 |
Correct |
1 ms |
852 KB |
Output is correct |
78 |
Correct |
1 ms |
852 KB |
Output is correct |
79 |
Correct |
1 ms |
852 KB |
Output is correct |
80 |
Correct |
1 ms |
888 KB |
Output is correct |
81 |
Correct |
1 ms |
852 KB |
Output is correct |
82 |
Correct |
1 ms |
852 KB |
Output is correct |
83 |
Correct |
1 ms |
852 KB |
Output is correct |
84 |
Correct |
7 ms |
10708 KB |
Output is correct |
85 |
Correct |
7 ms |
10708 KB |
Output is correct |
86 |
Correct |
16 ms |
16468 KB |
Output is correct |
87 |
Correct |
12 ms |
16504 KB |
Output is correct |
88 |
Correct |
11 ms |
16596 KB |
Output is correct |
89 |
Correct |
13 ms |
16460 KB |
Output is correct |
90 |
Correct |
16 ms |
16180 KB |
Output is correct |
91 |
Correct |
12 ms |
16200 KB |
Output is correct |
92 |
Correct |
12 ms |
16180 KB |
Output is correct |
93 |
Correct |
13 ms |
16212 KB |
Output is correct |
94 |
Correct |
12 ms |
16212 KB |
Output is correct |
95 |
Correct |
12 ms |
16160 KB |
Output is correct |
96 |
Correct |
384 ms |
343208 KB |
Output is correct |
97 |
Correct |
14 ms |
2644 KB |
Output is correct |
98 |
Correct |
293 ms |
342988 KB |
Output is correct |
99 |
Correct |
254 ms |
343104 KB |
Output is correct |
100 |
Correct |
31 ms |
3028 KB |
Output is correct |
101 |
Correct |
317 ms |
343204 KB |
Output is correct |
102 |
Correct |
292 ms |
343332 KB |
Output is correct |
103 |
Correct |
322 ms |
343156 KB |
Output is correct |
104 |
Correct |
298 ms |
343260 KB |
Output is correct |
105 |
Correct |
308 ms |
343184 KB |
Output is correct |
106 |
Correct |
312 ms |
343128 KB |
Output is correct |
107 |
Correct |
291 ms |
343116 KB |
Output is correct |
108 |
Correct |
234 ms |
298464 KB |
Output is correct |
109 |
Correct |
320 ms |
343288 KB |
Output is correct |