#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>>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];
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:84: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]
84 | for(int firstr=1; firstr <= pos[gr].size(); firstr++)
| ~~~~~~~^~~~~~~~~~~~~~~~~
passes.cpp:87: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]
87 | while (insleft[gr][i][firstr]<pos[i].size()&&pos[i][insleft[gr][i][firstr]]<pos[gr][firstr-1]) insleft[gr][i][firstr]++;
passes.cpp:92: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]
92 | 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++)
......
94 | rep(j,pos[gr].size()) insleft[gr][i][j+1]+=insleft[gr][i][j];
| ~~~~~~~~~~~~~~~~
passes.cpp:94:13: note: in expansion of macro 'rep'
94 | rep(j,pos[gr].size()) insleft[gr][i][j+1]+=insleft[gr][i][j];
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
4 ms |
1612 KB |
Output is correct |
7 |
Correct |
4 ms |
1612 KB |
Output is correct |
8 |
Correct |
4 ms |
1612 KB |
Output is correct |
9 |
Correct |
4 ms |
1612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 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 |
1 ms |
212 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 |
232 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
312 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 |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
2 ms |
1492 KB |
Output is correct |
36 |
Correct |
2 ms |
1492 KB |
Output is correct |
37 |
Correct |
61 ms |
1740 KB |
Output is correct |
38 |
Correct |
41 ms |
1756 KB |
Output is correct |
39 |
Correct |
4 ms |
1748 KB |
Output is correct |
40 |
Correct |
46 ms |
1724 KB |
Output is correct |
41 |
Correct |
40 ms |
1836 KB |
Output is correct |
42 |
Correct |
40 ms |
1868 KB |
Output is correct |
43 |
Correct |
44 ms |
1824 KB |
Output is correct |
44 |
Correct |
41 ms |
1716 KB |
Output is correct |
45 |
Correct |
57 ms |
1832 KB |
Output is correct |
46 |
Correct |
37 ms |
1740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
4 ms |
1612 KB |
Output is correct |
7 |
Correct |
4 ms |
1612 KB |
Output is correct |
8 |
Correct |
4 ms |
1612 KB |
Output is correct |
9 |
Correct |
4 ms |
1612 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
0 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 |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
232 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
312 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 |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
2 ms |
1492 KB |
Output is correct |
45 |
Correct |
2 ms |
1492 KB |
Output is correct |
46 |
Correct |
61 ms |
1740 KB |
Output is correct |
47 |
Correct |
41 ms |
1756 KB |
Output is correct |
48 |
Correct |
4 ms |
1748 KB |
Output is correct |
49 |
Correct |
46 ms |
1724 KB |
Output is correct |
50 |
Correct |
40 ms |
1836 KB |
Output is correct |
51 |
Correct |
40 ms |
1868 KB |
Output is correct |
52 |
Correct |
44 ms |
1824 KB |
Output is correct |
53 |
Correct |
41 ms |
1716 KB |
Output is correct |
54 |
Correct |
57 ms |
1832 KB |
Output is correct |
55 |
Correct |
37 ms |
1740 KB |
Output is correct |
56 |
Correct |
1 ms |
852 KB |
Output is correct |
57 |
Correct |
50 ms |
852 KB |
Output is correct |
58 |
Correct |
0 ms |
212 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 |
1 ms |
212 KB |
Output is correct |
63 |
Correct |
4 ms |
1612 KB |
Output is correct |
64 |
Correct |
4 ms |
1612 KB |
Output is correct |
65 |
Correct |
5 ms |
1572 KB |
Output is correct |
66 |
Correct |
4 ms |
1612 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 |
212 KB |
Output is correct |
70 |
Correct |
1 ms |
212 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 |
1 ms |
212 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 |
1492 KB |
Output is correct |
86 |
Correct |
48 ms |
1828 KB |
Output is correct |
87 |
Correct |
41 ms |
1736 KB |
Output is correct |
88 |
Correct |
4 ms |
1748 KB |
Output is correct |
89 |
Correct |
53 ms |
1808 KB |
Output is correct |
90 |
Correct |
52 ms |
1824 KB |
Output is correct |
91 |
Correct |
38 ms |
1816 KB |
Output is correct |
92 |
Correct |
38 ms |
1800 KB |
Output is correct |
93 |
Correct |
40 ms |
1716 KB |
Output is correct |
94 |
Correct |
41 ms |
1800 KB |
Output is correct |
95 |
Correct |
50 ms |
1868 KB |
Output is correct |
96 |
Execution timed out |
2088 ms |
13520 KB |
Time limit exceeded |
97 |
Halted |
0 ms |
0 KB |
- |