#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;
vector<vector<vector<int>>>insleft(15,vector<vector<int>>(15)), insright(15,vector<vector<int>>(15));
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 firstr)
{
double sum = 0;
sum+=f(pos[gr].size()-firstr) + f(firstr);
rep(i,g) if(((1<<i)&bor)==0) sum+=insleft[gr][i][firstr]+insright[gr][i][firstr];
return sum;
}
vector<int>lookup;
double dfs(int bor)
{
if(mem[bor]>-0.9) return mem[bor];
double sol = 1e18;
rep(i,g)
{
if(!((1<<i)&bor)) continue;
int nbor = bor^(1<<i);
if(!pos[i].size()) return mem[bor]=dfs(nbor);
int start = -1, end = pos[i].size();
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;
};
sol=min(sol, cost(bor,i,end)+dfs(nbor));
}
return mem[bor] = sol;
}
signed main()
{
cin.tie(0);
ios_base::sync_with_stdio(false);
cin>>s;
s.size();
n=s.size();
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);
rep(gr,g)
{
rep(i,g)
{
if(i==gr) continue;
insleft[gr][i].assign(pos[gr].size()+1,0);
insright[gr][i].assign(pos[gr].size()+1,0);
for(int firstr=1; firstr <= pos[gr].size(); firstr++)
{
insleft[gr][i][firstr]=insleft[gr][i][firstr-1];
while (insleft[gr][i][firstr]<pos[i].size()&&pos[i][insleft[gr][i][firstr]]<pos[gr][firstr-1]) insleft[gr][i][firstr]++;
}
for(int firstr=pos[gr].size()-1; firstr >= 0; firstr--)
{
insright[gr][i][firstr]=insright[gr][i][firstr+1];
while (insright[gr][i][firstr]<pos[i].size()&&pos[i][pos[i].size()-1-insright[gr][i][firstr]]>pos[gr][firstr]) insright[gr][i][firstr]++;
}
rep(j,pos[gr].size()) insleft[gr][i][j+1]+=insleft[gr][i][j];
for(int j=pos[gr].size()-2; j>=0; j--) insright[gr][i][j]+=insright[gr][i][j+1];
}
}
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 'int main()':
passes.cpp:87:38: 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]
87 | for(int firstr=1; firstr <= pos[gr].size(); firstr++)
| ~~~~~~~^~~~~~~~~~~~~~~~~
passes.cpp:90:46: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
90 | while (insleft[gr][i][firstr]<pos[i].size()&&pos[i][insleft[gr][i][firstr]]<pos[gr][firstr-1]) insleft[gr][i][firstr]++;
passes.cpp:95:47: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | while (insright[gr][i][firstr]<pos[i].size()&&pos[i][pos[i].size()-1-insright[gr][i][firstr]]>pos[gr][firstr]) insright[gr][i][firstr]++;
passes.cpp:5:33: 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]
5 | #define rep(i,n) for(int i =0; i<n; i++)
......
97 | rep(j,pos[gr].size()) insleft[gr][i][j+1]+=insleft[gr][i][j];
| ~~~~~~~~~~~~~~~~
passes.cpp:97:13: note: in expansion of macro 'rep'
97 | rep(j,pos[gr].size()) insleft[gr][i][j+1]+=insleft[gr][i][j];
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
2 ms |
1680 KB |
Output is correct |
7 |
Correct |
2 ms |
1680 KB |
Output is correct |
8 |
Correct |
2 ms |
1680 KB |
Output is correct |
9 |
Correct |
2 ms |
1680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 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 |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 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 |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 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 |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
260 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
2 ms |
1492 KB |
Output is correct |
36 |
Correct |
2 ms |
1492 KB |
Output is correct |
37 |
Correct |
6 ms |
1876 KB |
Output is correct |
38 |
Correct |
5 ms |
1748 KB |
Output is correct |
39 |
Correct |
3 ms |
1748 KB |
Output is correct |
40 |
Correct |
7 ms |
1748 KB |
Output is correct |
41 |
Correct |
5 ms |
1876 KB |
Output is correct |
42 |
Correct |
6 ms |
1748 KB |
Output is correct |
43 |
Correct |
5 ms |
1748 KB |
Output is correct |
44 |
Correct |
5 ms |
1748 KB |
Output is correct |
45 |
Correct |
4 ms |
1748 KB |
Output is correct |
46 |
Correct |
4 ms |
1760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
2 ms |
1680 KB |
Output is correct |
7 |
Correct |
2 ms |
1680 KB |
Output is correct |
8 |
Correct |
2 ms |
1680 KB |
Output is correct |
9 |
Correct |
2 ms |
1680 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 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 |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
1 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 |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
260 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
1 ms |
212 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
2 ms |
1492 KB |
Output is correct |
45 |
Correct |
2 ms |
1492 KB |
Output is correct |
46 |
Correct |
6 ms |
1876 KB |
Output is correct |
47 |
Correct |
5 ms |
1748 KB |
Output is correct |
48 |
Correct |
3 ms |
1748 KB |
Output is correct |
49 |
Correct |
7 ms |
1748 KB |
Output is correct |
50 |
Correct |
5 ms |
1876 KB |
Output is correct |
51 |
Correct |
6 ms |
1748 KB |
Output is correct |
52 |
Correct |
5 ms |
1748 KB |
Output is correct |
53 |
Correct |
5 ms |
1748 KB |
Output is correct |
54 |
Correct |
4 ms |
1748 KB |
Output is correct |
55 |
Correct |
4 ms |
1760 KB |
Output is correct |
56 |
Correct |
1 ms |
852 KB |
Output is correct |
57 |
Correct |
54 ms |
852 KB |
Output is correct |
58 |
Correct |
0 ms |
340 KB |
Output is correct |
59 |
Correct |
1 ms |
212 KB |
Output is correct |
60 |
Correct |
0 ms |
212 KB |
Output is correct |
61 |
Correct |
0 ms |
212 KB |
Output is correct |
62 |
Correct |
0 ms |
340 KB |
Output is correct |
63 |
Correct |
2 ms |
1680 KB |
Output is correct |
64 |
Correct |
2 ms |
1680 KB |
Output is correct |
65 |
Correct |
3 ms |
1680 KB |
Output is correct |
66 |
Correct |
2 ms |
1680 KB |
Output is correct |
67 |
Correct |
0 ms |
212 KB |
Output is correct |
68 |
Correct |
1 ms |
212 KB |
Output is correct |
69 |
Correct |
1 ms |
340 KB |
Output is correct |
70 |
Correct |
1 ms |
340 KB |
Output is correct |
71 |
Correct |
1 ms |
212 KB |
Output is correct |
72 |
Correct |
1 ms |
212 KB |
Output is correct |
73 |
Correct |
1 ms |
212 KB |
Output is correct |
74 |
Correct |
1 ms |
212 KB |
Output is correct |
75 |
Correct |
1 ms |
212 KB |
Output is correct |
76 |
Correct |
1 ms |
212 KB |
Output is correct |
77 |
Correct |
1 ms |
212 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
1 ms |
212 KB |
Output is correct |
80 |
Correct |
1 ms |
212 KB |
Output is correct |
81 |
Correct |
1 ms |
212 KB |
Output is correct |
82 |
Correct |
0 ms |
340 KB |
Output is correct |
83 |
Correct |
1 ms |
212 KB |
Output is correct |
84 |
Correct |
2 ms |
1492 KB |
Output is correct |
85 |
Correct |
2 ms |
1480 KB |
Output is correct |
86 |
Correct |
7 ms |
1876 KB |
Output is correct |
87 |
Correct |
5 ms |
1752 KB |
Output is correct |
88 |
Correct |
3 ms |
1752 KB |
Output is correct |
89 |
Correct |
5 ms |
1864 KB |
Output is correct |
90 |
Correct |
5 ms |
1748 KB |
Output is correct |
91 |
Correct |
5 ms |
1748 KB |
Output is correct |
92 |
Correct |
5 ms |
1748 KB |
Output is correct |
93 |
Correct |
5 ms |
1748 KB |
Output is correct |
94 |
Correct |
5 ms |
1848 KB |
Output is correct |
95 |
Correct |
7 ms |
1748 KB |
Output is correct |
96 |
Correct |
269 ms |
23888 KB |
Output is correct |
97 |
Correct |
25 ms |
596 KB |
Output is correct |
98 |
Correct |
259 ms |
23736 KB |
Output is correct |
99 |
Correct |
71 ms |
23852 KB |
Output is correct |
100 |
Correct |
29 ms |
852 KB |
Output is correct |
101 |
Correct |
218 ms |
23904 KB |
Output is correct |
102 |
Correct |
280 ms |
23828 KB |
Output is correct |
103 |
Correct |
238 ms |
23872 KB |
Output is correct |
104 |
Correct |
234 ms |
23796 KB |
Output is correct |
105 |
Correct |
243 ms |
23832 KB |
Output is correct |
106 |
Correct |
248 ms |
23776 KB |
Output is correct |
107 |
Correct |
247 ms |
23808 KB |
Output is correct |
108 |
Correct |
129 ms |
21840 KB |
Output is correct |
109 |
Correct |
242 ms |
23756 KB |
Output is correct |