Problem UVA1354-Mobile Computing

Accept:267  Submit:2232

Time Limit: 3000 mSec

UVA1354-Mobile Computing(二进制枚举子集)-LMLPHP Problem Description

UVA1354-Mobile Computing(二进制枚举子集)-LMLPHP

UVA1354-Mobile Computing(二进制枚举子集)-LMLPHP Input

UVA1354-Mobile Computing(二进制枚举子集)-LMLPHP

UVA1354-Mobile Computing(二进制枚举子集)-LMLPHP Output

UVA1354-Mobile Computing(二进制枚举子集)-LMLPHP

UVA1354-Mobile Computing(二进制枚举子集)-LMLPHP Sample Input

5
1.3 3
1 2 1
1.4 3
1 2 1
2.0 3
1 2 1
1.59 4
2 1 1 3
1.7143 4
1 2 3 5
 

UVA1354-Mobile Computing(二进制枚举子集)-LMLPHP Sample Ouput

-1

1.3333333333333335

1.6666666666666667

1.5833333333333335

1.7142857142857142

题解:感觉这个题挺难的。把一个天平看作一棵树,叶子节点是砝码,当确定了这棵树的形状及叶子节点的值之后这个天平的长度就是确定的,思路就来自于此。

下面的事情就是枚举子集,以我目前的能力实现起来确实有困难,参考了lrj的代码,这种二进制枚举子集的方式值得学习。

P.S.0有可能是合法输出,而我一开始设Max = 0.0,当没有更新时输出-1,WAWAWAWAWA......

 #include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std; const int maxn = ;
int n;
double r,sum[<<maxn];
double w[maxn]; struct Tree{
double L,R;
Tree(double L = 0.0,double R = 0.0) :
L(L),R(R) {}
}; vector< vector<Tree> > tree(<<maxn);
bool vis[<<maxn]; void dfs(int subset){
if(vis[subset]) return;
vis[subset] = true;
bool have_child = false;
for(int left = (subset-)&subset;left;left = (left-)&subset){
have_child = true;
int right = subset^left;
double d1 = sum[right]/sum[subset],d2 = sum[left]/sum[subset];
dfs(left),dfs(right);
for(int i = ;i < tree[left].size();i++){
for(int j =;j < tree[right].size();j++){
Tree t;
t.L = max(tree[left][i].L+d1,tree[right][j].L-d2);
t.R = max(tree[right][j].R+d2,tree[left][i].R-d1);
if(t.R+t.L < r) tree[subset].push_back(t);
}
}
}
if(!have_child) tree[subset].push_back(Tree());
} int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
int iCase;
scanf("%d",&iCase);
while(iCase--){
scanf("%lf%d",&r,&n);
for(int i = ;i < n;i++){
scanf("%lf",&w[i]);
}
memset(vis,false,sizeof(vis));
for(int i = ;i < (<<n);i++){
sum[i] = 0.0;
tree[i].clear();
for(int j = ;j < n;j++){
if(i&(<<j)) sum[i] += w[j];
}
}
int root = (<<n)-;
dfs(root);
double Max = -;
for(int i = ;i < tree[root].size();i++){
Max = max(Max,tree[root][i].L+tree[root][i].R);
}
printf("%.10lf\n",Max);
}
return ;
}
05-02 14:45