#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(10);
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));
int k = pool.size();
vector<vector<int>> arr((1 << k));
for (int i = 0; i < (1 << k); ++i) {
vector<int> now;
for (int j = 0; j < k; ++j) {
if (i >> j & 1) {
for (auto& u : points[j]) {
now.push_back(u);
}
}
}
arr[i] = now;
}
vector<double> dp((1 << k), 1e15);
dp[0] = 0;
for (int mask = 0; mask < (1 << k); ++mask) {
for (int j = 0; j < k; ++j) {
if (mask >> j & 1) continue;
double ndp = dp[mask] + add(arr[mask], points[j]);
dp[mask | (1 << j)] = min(dp[mask | (1 << j)], ndp);
}
}
cout << fixed << setprecision(10) << dp[(1 << k) - 1] << 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... |