이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 42, G = 15, INF = 1e18 + 42;
string str;
vector<int> pos[G];
int n, g = 0, sum[N], val[N], dp[1 << G];
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> str;
n = str.size();
for(int i = 0; i < n; i++)
g = max(g, (int)(str[i] - 'A' + 1));
for(int i = 0; i < n; i++)
pos[str[i] - 'A'].push_back(i);
for(int i = 1; i < (1 << g); i++) {
dp[i] = INF;
for(int j = 0; j < n; j++)
sum[j] = 0;
for(int j = 0; j < g; j++)
if(i & (1 << j))
for(int p : pos[j])
sum[p]++;
for(int j = 1; j < n; j++)
sum[j] += sum[j-1];
for(int j = 0; j < g; j++)
if(i & (1 << j)) {
int sz = pos[j].size(), tot = 0, mini = INF;
for(int k = 0; k < sz; k++)
tot += sum[pos[j][k]] - (k+1);
mini = tot*4 + (sz*(sz-1));
for(int k = sz-1; k > -1; k--) {
tot += sum[n-1] - sum[pos[j][k]] * 2 - sz + 2 * (k+1);
mini = min(mini, tot*4 + (k*(k-1)+(sz-k)*(sz-k-1)));
}
dp[i] = min(dp[i], dp[i ^ (1 << j)] + mini);
}
}
cout << setprecision(30) << dp[(1 << g) - 1]/4.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... |