This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
ld exp_inv(ll x) {
return x * (x-1) / 2.0l;
}
int main() {
char a = 'A';
string s; cin >> s;
const int G = 7;
int n = s.size();
vector memo(1<<G, (ld)-1);
auto dp = [&](auto& dp, int used)->ld{
if (memo[used] >= 0) return memo[used];
if (__builtin_popcount(used) == G) return 0;
ld res = 1e18;
for (int i = 0 ;i < G; i++) {
ld cur = 1e18;
if (used&1<<i) continue;
vector has(n, 0);
for (int j = 0; j < n; j++) {
if (used& 1<<(s[j] - a))
has[j] = 1;
}
for (int mid = 1; mid < n; mid++) {
ll sum = 0;
ll t= 0, c1{}, c2{};
for (int j = 0; j < mid; j++) {
if (s[j] == i + a)
sum += t, c1++;
else t += has[j];
}
t = 0;
for (int j = n-1; j >= mid; j--) {
if (s[j] == i + a)
sum += t, c2++;
else t += has[j];
}
cur = min(cur, sum + (exp_inv(c1) + exp_inv(c2)) / 2);
}
res = min(res, cur + dp(dp, used |1<<i));
}
return memo[used] = res;
};
cout << dp(dp, 0) << '\n';
}
# | 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... |