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;
ll exp_inv(ll x) {
return x * (x-1);
}
int main() {
char a = 'A';
string s; cin >> s;
const int G = 10;
int n = s.size();
if (n == 1) {
cout << "0\n";
return 0;
}
vector suml(G, vector(G, vector(n, 0ll)));
vector sumr(G, vector(G, vector(n, 0ll)));
for (int c1 = 0; c1 < G; c1++) {
for (int c2 = 0; c2 < G; c2++) {
ll res = 0, cur = 0;
for (int i = 0; i < n; i++) {
if (s[i] == c1+a)
res += cur;
else if (s[i] == c2+a)
cur++;
suml[c1][c2][i] = res;
}
}
}
for (int c1 = 0; c1 < G; c1++) {
for (int c2 = 0; c2 < G; c2++) {
ll res = 0, cur = 0;
for (int i = n-1; i >= 0; i --) {
if (s[i] == c1+a)
res += cur;
else if (s[i] == c2+a)
cur++;
sumr[c1][c2][i] = res;
}
}
}
vector memo(1<<G, (ll)1e18);
memo[(1<<G)-1] = 0;
for (int used = (1<<G)-1-1; used >= 0; used--) {
ll& res = memo[used];
for (int i = 0 ;i < G; i++) {
if (used&1<<i) continue;
vector<ll> left(n), right(n);
ll sum = 0, c = 0;
for (int j = 0; j < n; j++) {
if (s[j] == i + a)
c++;
sum = 0;
for (int c2 = 0; c2 < G; c2++)
if (used&1<<c2)
sum += suml[i][c2][j];
left[j] = sum * 4 + exp_inv(c);
}
sum = c = 0;
for (int j = n-1; j >= 0; j--) {
if (s[j] == i + a)
c++;
sum = 0;
for (int c2 = 0; c2 < G; c2++)
if (used&1<<c2)
sum += sumr[i][c2][j];
right[j] = sum * 4 + exp_inv(c);
}
ll cur = left[n-1];
for (int mid = 1; mid < n; mid++) {
cur = min(cur, left[mid-1] + right[mid]);
}
res = min(res, cur + memo[used |1<<i]);
}
};
ll ans;
if (n > 10000) ans = exp_inv(n / 2) + exp_inv(n - n / 2);
else ans = memo[0];
cout << ans / 4;
if (ans%4 == 1) cout << ".25";
if (ans%4 == 2) cout << ".5";
if (ans%4== 3) cout << ".75";
}
# | 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... |