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>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 1e5 + 2;
const int M = 15;
const int K = 1 << M;
const ll linf = 1e18;
vector<ll> pref[M][M], suff[M][M];
vector<int> pos[M];
ll dp[K];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string s; cin >> s;
int n = s.size();
for (int i = 0; i < n; i++) pos[s[i] - 'A'].push_back(i);
for (int i = 0; i < M; i++) {
for (int j = 0; j < M; j++) {
if (i == j) continue;
pref[i][j].resize(pos[i].size());
int jj = 0;
for (int ii = 0; ii < pos[i].size(); ii++) {
while (jj < pos[j].size() && pos[j][jj] < pos[i][ii]) jj += 1;
pref[i][j][ii] = 2 * jj;
if (ii > 0) pref[i][j][ii] += pref[i][j][ii - 1];
}
}
}
for (int i = 0; i < M; i++) {
for (int j = 0; j < M; j++) {
if (i == j) continue;
suff[i][j].resize(pos[i].size());
int jj = pos[j].size() - 1;
for (int ii = pos[i].size() - 1; ii >= 0; ii--) {
while (jj >= 0 && pos[j][jj] > pos[i][ii]) jj -= 1;
suff[i][j][ii] = 2 * (pos[j].size() - jj - 1);
if (ii < pos[i].size() - 1) suff[i][j][ii] += suff[i][j][ii + 1];
}
}
}
for (int mask = 1; mask < K; mask++) {
dp[mask] = linf;
for (int i = 0; i < M; i++) {
if (!((1 << i) & mask)) continue;
int uk = pos[i].size();
auto F = [&] (ll kol) {
ll ans = kol * (kol - 1) / 2;
ans += (uk - kol) * (uk - kol - 1) / 2;
for (int j = 0; j < M; j++) {
if (i != j && ((1 << j) & mask)) {
if (kol > 0) ans += pref[i][j][kol - 1];
if (kol < uk) ans += suff[i][j][kol];
}
}
return ans;
};
int l = 0, r = uk - 1, gde = uk;
while (l <= r) {
int mid = l + r >> 1;
if (F(mid) < F(mid + 1)) {
gde = mid;
r = mid - 1;
}
else l = mid + 1;
}
smin(dp[mask], dp[mask ^ (1 << i)] + F(gde));
}
}
cout << dp[K - 1] / 2;
if (dp[K - 1] % 2 == 1) cout << ".5" << en;
return 0;
}
Compilation message (stderr)
passes.cpp: In function 'int main()':
passes.cpp:28:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | for (int ii = 0; ii < pos[i].size(); ii++) {
| ~~~^~~~~~~~~~~~~~~
passes.cpp:29:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | while (jj < pos[j].size() && pos[j][jj] < pos[i][ii]) jj += 1;
| ~~~^~~~~~~~~~~~~~~
passes.cpp:43:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | if (ii < pos[i].size() - 1) suff[i][j][ii] += suff[i][j][ii + 1];
| ~~~^~~~~~~~~~~~~~~~~~~
passes.cpp:65:29: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
65 | int mid = l + r >> 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... |