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;
const int N = 1e5 + 5, G = 15;
int n, g;
double dp[1 << G], pans[N], lft[N], rght[N];
bool vis[N];
string str;
map<char, char> cmprs;
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
for(int i = 2; i < N; i++) pans[i] = pans[i - 1] + (i - 1) / 2.0;
for(int i = 0; i < (1 << G); i++) dp[i] = DBL_MAX;
dp[0] = 0;
cin >> str;
n = str.size();
for(auto c : str) cmprs[c] = '.';
g = 0;
for(auto &p : cmprs) p.second = 'A' + (++g - 1);
for(auto &c : str) c = cmprs[c];
for(int m = 0; m < (1 << g); m++){
for(int cur = 0; cur < g; cur++){
if(m & (1 << cur)) continue;
else{
memset(vis, 0, sizeof(vis));
for(int i = 0; i < n; i++) if(m & (1 << (str[i] - 'A'))) vis[i] = true;
int totalcnt = count(str.begin(), str.end(), 'A' + cur);
int curcnt = 0, ptr = 0;
for(int i = 1; i <= totalcnt; i++){
while(str[ptr] != 'A' + cur){
if(vis[ptr]) curcnt++;
ptr++;
}
lft[i] = lft[i - 1] + curcnt;
ptr++;
}
curcnt = 0, ptr = n - 1;
for(int i = 1; i <= totalcnt; i++){
while(str[ptr] != 'A' + cur){
if(vis[ptr]) curcnt++;
ptr--;
}
rght[i] = rght[i - 1] + curcnt;
ptr--;
}
for(int i = 1; i <= totalcnt; i++){
lft[i] += pans[i];
rght[i] += pans[i];
}
for(int i = 0; i <= totalcnt; i++){
dp[m | (1 << cur)] = min(dp[m | (1 << cur)], dp[m] + lft[i] + rght[totalcnt - i]);
}
}
}
}
cout << fixed << dp[(1 << g) - 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... |