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;
// Input
long long G;
long long N, A[1 << 19];
string S;
// DP
long long Cost[1 << 10][10];
long long Left[1 << 19];
long long Rigt[1 << 19];
long long dp[1 << 19];
int main() {
// Step 1. Input
cin >> S;
N = S.size();
for (int i = 0; i < (int)S.size(); i++) {
A[i] = (S[i] - 'A');
G = max(G, A[i] + 1);
}
// Step 2. Precount
for (int i = 0; i < (1 << N); i++) {
for (int j = 0; j < N; j++) {
if ((i >> j) & 1) continue;
// Left Side
int cur1 = 0;
for (int k = 0; k < N; k++) {
Left[k] = cur1;
if (A[k] == j) cur1 += 1;
if ((i >> A[k]) & 1) cur1 += 2;
}
// Rigt Side
int cur2 = 0;
for (int k = N - 1; k >= 0; k--) {
Rigt[k] = cur2;
if (A[k] == j) cur2 += 1;
if ((i >> A[k]) & 1) cur2 += 2;
}
// Optimal 1
long long Current = 0;
for (int k = 0; k < N; k++) {
if (A[k] == j) Current += Rigt[k];
}
Cost[i][j] = Current;
for (int k = 0; k < N; k++) {
if (A[k] == j) Current += (Left[k] - Rigt[k]);
Cost[i][j] = min(Cost[i][j], Current);
}
}
}
// Step 3. DP
for (int i = 0; i < (1 << N); i++) dp[i] = (1LL << 60);
dp[0] = 0;
for (int i = 0; i < (1 << N); i++) {
for (int j = 0; j < N; j++) {
if ((i >> j) & 1) continue;
dp[i + (1 << j)] = min(dp[i + (1 << j)], dp[i] + Cost[i][j]);
}
}
// Step 4. Output
printf("%.12lf\n", 0.5 * dp[(1 << N) - 1]);
return 0;
}
# | 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... |