#include "bits/stdc++.h"
using namespace std;
#define ar array
typedef int64_t ll;
#define int ll
const int G = 15;
int dp[1 << G];
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
string s; cin>>s;
int n = s.size(), g = 0;
for(int i=0;i<n;i++){
g = max(g, (ll)s[i] - 'A' + 1);
}
memset(dp, 63, sizeof dp);
dp[0] = 0;
int pref[n + 5];
for(int mask=0;mask < (1 << g);mask++){
for(int j=0;j<g;j++){
if(mask >> j & 1) continue;
memset(pref, 0, sizeof pref);
int cnt = 0;
for(int k=0;k<n;k++){
if(k) pref[k] += pref[k-1];
if(s[k] - 'A' == j){
pref[k] += cnt;
cnt++;
}
if(mask >> (s[k] - 'A') & 1) cnt += 2;
}
int suff = 0, res = pref[n - 1]; cnt = 0;
for(int i=n-1;~i;i--){
if(s[i] - 'A' == j){
suff += cnt, cnt++;
}
res = min(res, suff + (i ? pref[i-1] : 0));
if(mask >> (s[i] - 'A') & 1) cnt += 2;
}
dp[mask | (1 << j)] = min(dp[mask | (1 << j)], dp[mask] + res);
}
}
double res = dp[(1 << g) - 1] / 2.;
cout<<fixed<<setprecision(1)<<res<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |