This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
#define double long double
#define rep(i,n) for(int i =0; i<n; i++)
#define all(a) a.begin(),a.end()
using namespace std;
string s;
int n;
double f(int n)
{
return ((double) n)*((double) (n-1))/4.0;
}
string ord;
vector<double>mem;
vector<vector<int>>pos;
int g;
// cost to add the group gr with all members of that group with and index <= to i boarding on the left
double cost(int bor, int gr, int i)
{
vector<int>pref;
double sum = 0;
int firstr = upper_bound(all(pos[gr]),i) - pos[gr].begin();
sum+=f(pos[gr].size()-firstr) + f(firstr);
return sum;
}
double dfs(int bor)
{
if(mem[bor]>-0.9) return mem[bor];
int tmp = 0;
rep(i,g) if((1<<i)&bor) tmp++;
if(tmp==1) return f(n/2) + f((n+1)/2);
double sol = 1e18;
rep(i,g)
{
if(!((1<<i)&bor)) continue;
int nbor = bor^(1<<i);
int start = 0, end = n;
while (end-start>1)
{
int c = (start+end)/2;
if(cost(nbor,i,c)>cost(nbor,i,c+1)) start=c;
else end=c;
}
sol=min(sol, cost(nbor,i,start));
}
return mem[bor] = sol;
}
signed main()
{
cin>>s;
s.size();
n=s.size();
double sum = f(n/2) + f((n+1)/2);
cout.precision(300);
char maxx=0;
rep(i,n) maxx=max(maxx, s[i]);
g='A'-maxx+1;
pos.resize(g);
mem.assign(1<<g,-1.0);
rep(i,n) pos[(s[i]-'A')].push_back(i);
cout<<dfs((1<<g)-1)<<'\n';
}
Compilation message (stderr)
passes.cpp: In function 'int main()':
passes.cpp:65:12: warning: unused variable 'sum' [-Wunused-variable]
65 | double sum = f(n/2) + f((n+1)/2);
| ^~~
# | 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... |