이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
#define pb push_back
#define sz(x) int(x.size())
using str = string;
using db = long double;
template<class T> using V = vector<T>;
using vi = V<int>;
using vd = V<db>;
const db EPS = 1e-14;
const int G = 15;
int main() {
cin.tie(0)->sync_with_stdio(0);
str S; cin >> S;
int N = sz(S);
V<vi> P(G, {0}); V<vi> oc(G);
for(int i = 0; i < N; i++) {
int c = S[i] - 'A'; oc[c].pb(i);
for(int g = 0; g < G; g++) P[g].pb(P[g].back() + (g == c));
}
auto qry = [&](int g, int l, int r) {
return P[g][r + 1] - P[g][l];
};
auto EXP = [&](int N) { return db(N) / db(2); };
cout << fixed << setprecision(10);
V<db> dp(1 << G, 1e10);
dp[0] = 0;
for(int msk = 0; msk < (1 << G); msk++) {
// cout << msk << " => " << dp[msk] << endl;
for(int g = 0; g < G; g++) if (((msk >> g) & 1) ^ 1) {
db all = dp[msk];
for(auto& x : oc[g]) {
db F = EXP(qry(g, 0, x - 1)), B = EXP(qry(g, x + 1, N - 1));
for(int c = 0; c < G; c++) if ((msk >> c) & 1) {
F += qry(c, 0, x), B += qry(c, x, N - 1);
}
// cout << x << " ======> " << F << " " << B << endl;
all += min(F, B);
}
// cout << g << " ===> " << all << endl;
int nmsk = msk | (1 << g);
if (dp[nmsk] > all + EPS) dp[nmsk] = all;
}
}
cout << dp[(1 << G) - 1] << nl;
exit(0-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... |