#include<bits/stdc++.h>
#define pb push_back
#define pf push_front
//#define mp make_pair
#define F first
#define S second
#define ff first
#define ss second
#define str string
#define ll long long
#define ull unsigned ll
#define ld long double
#define pll pair<ll,ll>
#define plll pair<pll,ll>
#define pllll pair<pll,pll>
#define pii pair<int,int>
#define pld pair<ld,ld>
#define vl vector<ll>
#define vll vector<pll>
#define vlll vector<plll>
#define vllll vector<pllll>
#define vb vector<bool>
#define vi vector<int>
#define vii vector<pii>
#define sz(x) (ll)(x).size()
#define fr front()
#define tp top()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bk back();
#pragma GCC target( "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("Ofast,unroll-loops,fast-math,O3")
using namespace std;
const ld pi=acos(-1.0);
const ll e5=1e5;
const ll e6=1e6;
const ll e9=1e9;
const ll inf=1e18;
const ll mod=1e9+7;
const ll mod2=998244353;
const ll MOD=106710087877475399;
const ll N=2e5+10;
const ll M=1e6+10;
const ld eps=1e-9;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll bpm(ll x,ll y,ll m){if(y==0)return 1%m;ll p=bpm(x,y/2,m);
if(y%2==0)return p*p%m;else return p*p%m*x%m;}
ll bp(ll x,ll y){if(y==0)return 1;ll p=bp(x,y/2);
if(y%2==0)return p*p;else return p*p*x;}
ll n,g,a[N];
str s;
ll pr[N][16][16],sf[N][16][16];
ll dp[N];
bool u[20];
void solve(){
cin>>s;
n=sz(s);
s=' '+s;
vector<char> v;
for(ll i=1;i<=n;i++) v.pb(s[i]);
sort(all(v));
v.erase(unique(all(v)),v.end());
g=sz(v);
map<char,ll> mp;
for(ll i=0;i<g;i++) mp[v[i]]=i;
for(ll i=1;i<=n;i++) a[i]=mp[s[i]];
for(ll x=0;x<g;x++){
for(ll y=0;y<g;y++){
for(ll i=1,s=0;i<=n;i++){
pr[i][x][y]=pr[i-1][x][y];
if(a[i]==x) pr[i][x][y]+=s;
if(a[i]==y) s++;
}
for(ll i=n,s=0;i>=1;i--){
sf[i][x][y]=sf[i+1][x][y];
if(a[i]==x) sf[i][x][y]+=s;
if(a[i]==y) s++;
}
if(x!=y){
for(ll i=1;i<=n;i++){
pr[i][x][y]*=2ll;
sf[i][x][y]*=2ll;
}
}
}
}
for(ll m=1;m<(1<<g);m++){
dp[m]=inf;
for(ll i=0;i<g;i++){
if((m>>i&1)==0) continue;
ll l=0,r=n,m1,m2;
while(l+2<r){
m1=l+(r-l)/3;
m2=r-(r-l)/3;
ll s1=0,s2=0;
for(ll j=0;j<g;j++){
if(m>>j&1){
s1+=pr[m1][i][j], s1+=sf[m1+1][i][j];
s2+=pr[m2][i][j], s2+=sf[m2+1][i][j];
}
}
if(s1>s2) l=m1;
else if(s1<s2) r=m2;
else l++, r--;
}
//cout<<m<<' '<<l<<' '<<r<<'\n';
for(ll k=l;k<=r;k++){
ll s=0;
for(ll j=0;j<g;j++){
if(m>>j&1){
s+=pr[k][i][j]+sf[k+1][i][j];
}
}
dp[m]=min(dp[m],dp[m-(1<<i)]+s);
}
//cout<<m<<' '<<dp[m]<<'\n';
}
}
cout<<(ld)dp[(1<<g)-1]/2;
return;
}
int main(/*Aldk*/){
ios_base::sync_with_stdio(false); cin.tie(NULL);
//freopen("01.in", "r", stdin);
//freopen("01.out ", "w", stdout);
cout<<setprecision(6)<<fixed;
int test=1;
//cin>>test;
for(int i=1;i<=test;i++){
solve();
cout<<'\n';
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |