#include<bits/stdc++.h>
using namespace std;
#define int long long
int inf=1e18;
int cost(int last, int mask, vector<int>& group, int n){
vector<int> prefix(n+1);
vector<int> suffix(n+1);
for(int i=0; i<n; i++){
prefix[i+1]=prefix[i];
if(group[i]==last){
prefix[i+1]+=1;
}
else if((mask>>group[i])%2==1){
prefix[i+1]+=2;
}
}
for(int i=n-1; i>=0; i--){
suffix[i]=suffix[i+1];
if(group[i]==last){
suffix[i]+=1;
}
else if((mask>>group[i])%2==1){
suffix[i]+=2;
}
}
int ans=0;
for(int i=0; i<n; i++){
if(group[i]!=last) continue;
//cout << i << " " << prefix[i] << " " << suffix[i+1] << " " << ans << "\n";
ans+=min(prefix[i], suffix[i+1]);
}
//cout << ans << "\n";
return ans;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
string s;
cin >> s;
map<int, int> b; int ind=1;
int n=s.size();
vector<int> group(n);
for(int i=0; i<n; i++){
if(b[s[i]]==0){
b[s[i]]=ind;
ind++;
}
group[i]=b[s[i]];
}
int g=ind;
vector<int> dp((1<<g), inf);
dp[0]=0;
for(int i=1; i<(1<<g); i++){
for(int last=0; last<n; last++){
if((i>>last)%2==0) continue;
dp[i]=min(dp[i], dp[i-(1<<last)]+cost(last, i, group, n));
}
//cout << i << " " << dp[i] << "\n";
}
double ans=(double)(dp[(1<<g)-1])/2;
cout.precision(100);
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |