#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;
set<int> b;
int n=s.size();
vector<int> group(n);
for(int i=0; i<n; i++){
b.insert(s[i]);
}
int g=b.size();
int ind=0;
map<int,int> num;
for(auto e: b){
num[e]=ind;
ind++;
}
for(int i=0; i<n; i++){
group[i]=num[s[i]];
}
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";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |