#include <bits/stdc++.h>
using namespace std;
using ll = long long;
double add(vector<int>& arr, vector<int>& points) {
double exp = 0;
int j = 0;
for (auto& i : points) {
int left = upper_bound(begin(arr), end(arr), i) - begin(arr);
int right = arr.size() - left;
double l = left + (1.0 * j / 2.0);
double r = right + (points.size() - j - 1) / 2.0;
exp += min(l, r);
j++;
}
for (auto& i : points) {
arr.push_back(i);
}
return exp;
}
int main(){
// random_device rd;
// mt19937 rng(rd());
ios::sync_with_stdio(false);
cin.tie(nullptr);
string s;
cin >> s;
int n = s.size();
vector<vector<int>> points(7);
vector<int> pool;
for (int i = 0; i < n; ++i) {
int j = s[i] - 'A';
pool.push_back(j);
points[j].push_back(i);
}
sort(begin(pool), end(pool));
pool.erase(unique(begin(pool), end(pool)), end(pool));
double res = 1e18;
do {
vector<int> now;
double curr = 0;
for (auto& u : pool) {
curr += add(now, points[u]);
sort(begin(now), end(now));
}
res = min(res, curr);
} while (next_permutation(begin(pool), end(pool)));
cout << fixed << setprecision(10) << res << endl;
}
# | 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... |