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;
typedef long long ll;
#define all(x) (x).begin(), (x).end()
#define MP make_pair
const int N = 100500;
long double exp_inv(int n){
return (long double) n * (n - 1) / 4.0;
}
vector<int> pos[140];
void solve(){
string s;
cin >> s;
int n = (int)s.size();
string alphabet;
for (int i = 0; i < n; i++){
if (pos[s[i]].empty()) alphabet += s[i];
pos[s[i]].push_back(i);
}
sort(all(alphabet));
int m1 = (n - 1) / 2;
int m2 = m1 + 1;
long double ans = 1e18;
do {
long double tot = 0;
vector<bool> used(n, false);
vector<int> cnt(n, 0);
for (char c : alphabet){
ll sum_l = 0;
vector<int> diff;
int sz = (int) pos[c].size();
for (int i : pos[c]){
int l = cnt[i];
int r = cnt.back() - cnt[i];
sum_l += l;
diff.push_back(r - l);
}
sort(all(diff));
long double cur = 1e18;
for (int half = 0; half <= sz; half++){
if (half > 0) sum_l += diff[half - 1];
cur = min(cur, sum_l + exp_inv(half) + exp_inv(sz - half));
}
int j = 0;
for (int i = 0; i < n; i++){
while (j < sz && pos[c][j] <= i) j++;
cnt[i] += j;
}
tot += cur;
}
ans = min(ans, tot);
} while (next_permutation(all(alphabet)));
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(false);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
solve();
return 0;
}
Compilation message (stderr)
passes.cpp: In function 'void solve()':
passes.cpp:23:21: warning: array subscript has type 'char' [-Wchar-subscripts]
23 | if (pos[s[i]].empty()) alphabet += s[i];
| ^
passes.cpp:24:17: warning: array subscript has type 'char' [-Wchar-subscripts]
24 | pos[s[i]].push_back(i);
| ^
passes.cpp:43:32: warning: array subscript has type 'char' [-Wchar-subscripts]
43 | int sz = (int) pos[c].size();
| ^
passes.cpp:45:30: warning: array subscript has type 'char' [-Wchar-subscripts]
45 | for (int i : pos[c]){
| ^
passes.cpp:62:38: warning: array subscript has type 'char' [-Wchar-subscripts]
62 | while (j < sz && pos[c][j] <= i) j++;
| ^
passes.cpp:30:9: warning: unused variable 'm2' [-Wunused-variable]
30 | int m2 = m1 + 1;
| ^~
# | 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... |