#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 maxi boarding on the left
double cost(int bor, int gr, int maxi)
{
double sum = 0;
int firstr = maxi+1;
sum+=f(pos[gr].size()-firstr) + f(firstr);
vector<int>otherpos;
rep(i,n) if(((1<<(s[i]-'A'))&bor)==0) otherpos.push_back(i);
rep(i,firstr)
{
sum+=upper_bound(all(otherpos),pos[gr][i])-otherpos.begin();
}
for(int i = firstr; i < pos[gr].size(); i++) sum+=otherpos.end()-upper_bound(all(otherpos),pos[gr][i]);
return sum;
}
vector<int>lookup;
double dfs(int bor)
{
if(mem[bor]>-0.9) return mem[bor];
double sol = 1e18;
rep(ck,g)
{
int i=lookup[ck];
if(!((1<<i)&bor)) continue;
int nbor = bor^(1<<i);
int start = -1, end = pos[i].size()-1;
while(end-start>1)
{
int c = (start+end)/2;
double l =cost(bor,i,c), r=cost(bor,i,c+1);
if(r>=l) end=c;
else start=c;
};
// double minn = 1e18;
// rep(j,pos[i].size()) if(cost(bor,i,j)<minn) {minn=cost(bor,i,j); end=j;}
sol=min(sol, cost(bor,i,end)+dfs(nbor));
}
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=maxx-'A'+1;
pos.resize(g);
rep(i,n) pos[(s[i]-'A')].push_back(i);
lookup.assign(g,0);
rep(i,g) lookup[i]=i;
mem.assign(1<<g,-1.0);
mem[0]=0;
cout<<dfs((1<<g)-1)<<'\n';
}
Compilation message
passes.cpp: In function 'long double cost(long long int, long long int, long long int)':
passes.cpp:39:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int i = firstr; i < pos[gr].size(); i++) sum+=otherpos.end()-upper_bound(all(otherpos),pos[gr][i]);
| ~~^~~~~~~~~~~~~~~~
passes.cpp: In function 'int main()':
passes.cpp:79:12: warning: unused variable 'sum' [-Wunused-variable]
79 | double sum = f(n/2) + f((n+1)/2);
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 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 |
212 KB |
Output is correct |
6 |
Correct |
33 ms |
1484 KB |
Output is correct |
7 |
Correct |
38 ms |
1612 KB |
Output is correct |
8 |
Correct |
42 ms |
1612 KB |
Output is correct |
9 |
Correct |
42 ms |
1612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
6 ms |
212 KB |
Output is correct |
4 |
Correct |
3 ms |
212 KB |
Output is correct |
5 |
Correct |
2 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
3 ms |
212 KB |
Output is correct |
8 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
6 ms |
212 KB |
Output is correct |
4 |
Correct |
3 ms |
212 KB |
Output is correct |
5 |
Correct |
2 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
3 ms |
212 KB |
Output is correct |
8 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 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 |
212 KB |
Output is correct |
6 |
Correct |
33 ms |
1484 KB |
Output is correct |
7 |
Correct |
38 ms |
1612 KB |
Output is correct |
8 |
Correct |
42 ms |
1612 KB |
Output is correct |
9 |
Correct |
42 ms |
1612 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
6 ms |
212 KB |
Output is correct |
13 |
Correct |
3 ms |
212 KB |
Output is correct |
14 |
Correct |
2 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
3 ms |
212 KB |
Output is correct |
17 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |