이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#ifndef _DEBUG
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#endif
using namespace std;
signed main()
{
cin.tie(0);
ios_base::sync_with_stdio(false);
string s;
cin >> s;
int n = s.size();
int g = *max_element(s.begin(), s.end()) - 'A' + 1;
vector<int> a(n);
vector<vector<int>> pos(g);
for (int i = 0; i < n; i++)
pos[a[i] = s[i] - 'A'].push_back(i);
vector<vector<int>> prefix(g, vector<int>(n));
vector<vector<int>> suffix(g, vector<int>(n));
for (int k = 0; k < g; k++)
{
for (int i = 1; i < n; i++)
prefix[k][i] = prefix[k][i - 1] + bool(a[i - 1] == k);
for (int i = n - 2; i >= 0; i--)
suffix[k][i] = suffix[k][i + 1] + bool(a[i + 1] == k);
}
vector<double> dp(1 << g, 1e20);
dp[0] = 0;
vector<vector<int>> preSum(g, vector<int>(g));
vector<vector<int>> sufSum(g, vector<int>(g));
for (int i = 0; i < g; i++)
{
for (int j = 0; j < g; j++)
{
int pre = 0;
int suf = 0;
for (int p : pos[i])
pre += prefix[j][p], suf += suffix[j][p];
preSum[i][j] = pre;
sufSum[i][j] = suf;
}
}
for (int b = 1; b < 1 << g; b++)
{
for (int i = 0; i < g; i++)
{
if (!(b & (1 << i)))
continue;
double cost = 0;
int pre = 0;
int suf = 0;
for (int j = 0; j < g; j++)
{
if (i != j && (b & j))
{
pre += preSum[i][j];
suf += sufSum[i][j];
}
}
cost += min(pre + 0.5 * preSum[i][i], suf + 0.5 * sufSum[i][i]);
dp[b] = min(dp[b], dp[b ^ (1 << i)] + cost);
}
}
cout << setprecision(15) << dp.back();
}
# | 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... |