/*
* File created on 07/19/2022 at 15:24:35.
* Link to problem:
* Description:
* Time complexity: O()
* Space complexity: O()
* Status: ---
* Copyright: Ⓒ 2022 Francois Vogel
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define pii pair<int, int>
#define f first
#define s second
#define vi vector<int>
#define all(x) x.begin(), x.end()
#define size(x) (int)((x).size())
#define pb push_back
#define ins insert
#define cls clear
#define int ll
#define ll long long
#define ld long double
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const int siz = 1e6+40;
ld fact [siz];
signed main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
fact[0] = 1;
for (int i = 1; i < siz; i++) fact[i] = fact[i-1] * i;
cout << fixed << setprecision(3);
string b;
cin >> b;
const int n = size(b);
ld bin [n+1][n+1]; // bin[i][j] = binomial coefficient (i, j) = i! / (j! * (i-j)!) = i * (i-1) * ... * (i-j+1) / j! = ways to choose j elements from i elements
for (int i = 0; i <= n; i++) for (int j = 0; j <= n; j++) {
if (j > i) {
bin[i][j] = 0;
continue;
}
else if (i-j < j) {
bin[i][j] = bin[i][i-j];
continue;
}
bin[i][j] = 1;
for (int k = i-j+1; k <= i; k++) bin[i][j] *= k;
for (int k = 1; k <= j; k++) bin[i][j] /= k;
}
int gK = 0;
for (char i : b) if (i-'A'+1 > gK) gK = i-'A'+1;
vi toPermute;
for (int i = 0; i < gK; i++) toPermute.pb(i);
ld offMin = n*n;
do {
for (int i = 0; i < n; i++) {
b[i] = char('A'+toPermute[b[i]-'A']);
}
ld res = 0;
for (int i = 0; i < gK; i++) {
vi pos;
for (int j = 0; j < n; j++) if (b[j]-'A' == i) {
pos.pb(j);
}
if (pos.empty()) continue;
int pref [n];
for (int j = 0; j < n; j++) pref[j] = (b[j]-'A' < i);
for (int j = 1; j < n; j++) pref[j] += pref[j-1];
int suf [n];
for (int j = 0; j < n; j++) suf[j] = (b[j]-'A' < i);
for (int j = n-2; j >= 0; j--) suf[j] += suf[j+1];
for (int j = 0; j < size(pos); j++) {
int dl = 0;
if (pos[j]) dl = pref[pos[j]-1];
int dr = 0;
if (pos[j]+1 < n) dr = suf[pos[j]+1];
int cl = j;
int cr = size(pos)-1-j;
for (int ul = 0; ul <= cl; ul++) for (int ur = 0; ur <= cr; ur++) {
ld loc = bin[cl][ul]*bin[cr][ur];
loc *= fact[ul+ur];
ld mul = min(dl+ul, dr+ur);
int lft = size(pos)-1-ul-ur;
res += loc*mul*fact[lft]/fact[size(pos)];
}
}
}
offMin = min(offMin, res);
vi invToPermute(gK);
for (int i = 0; i < gK; i++) invToPermute[toPermute[i]] = i;
for (int i = 0; i < n; i++) {
b[i] = char('A'+invToPermute[b[i]-'A']);
}
} while (next_permutation(all(toPermute)));
cout << offMin;
cout.flush();
int d = 0;
d++;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1132 ms |
28664 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
9 ms |
15956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
9 ms |
15956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1132 ms |
28664 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |