Mobile Computing
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 666   Accepted: 224   Special Judge

Description

There is a mysterious planet called Yaen, whose space is 2-dimensional. There are many beautiful stones on the planet, and the Yaen people love to collect them. They bring the stones back home and make nice mobile arts of them to decorate their 2-dimensional living rooms. 
In their 2-dimensional world, a mobile is defined recursively as follows: 

  • a stone hung by a string, or
  • a rod of length 1 with two sub-mobiles at both ends; the rod is hung by a string at the center of gravity of sub-mobiles. When the weights of the sub-mobiles are n and m, and their distances from the center of gravity are a and b respectively, the equation n * a = m * b holds.

For example, if you got three stones with weights 1, 1, and 2, here are some possible mobiles and their widths: 
 
Given the weights of stones and the width of the room, your task is to design the widest possible mobile satisfying both of the following conditions.

  • It uses all the stones.
  • Its width is less than the width of the room.

You should ignore the widths of stones. 
In some cases two sub-mobiles hung from both ends of a rod might overlap (see the figure on the right). Such mobiles are acceptable. The width of the example is (1/3) + 1 + (1/4).

Input

The first line of the input gives the number of datasets. Then the specified number of datasets follow. A dataset has the following format. 


w1 ... 
ws 
r is a decimal fraction representing the width of the room, which satisfies 0 < r < 10. s is the number of the stones. You may assume 1 <= s <= 6. wi is the weight of the i-th stone, which is an integer. You may assume 1 <= wi <= 1000. 
You can assume that no mobiles whose widths are between r - 0.00001 and r + 0.00001 can be made of given stones.

Output

For each dataset in the input, one line containing a decimal fraction should be output. The decimal fraction should give the width of the widest possible mobile as defined above. An output line should not contain extra characters such as spaces. 
In case there is no mobile which satisfies the requirement, answer -1 instead. 
The answer should not have an error greater than 0.00000001. You may output any number of digits after the decimal point, provided that the above accuracy condition is satisfied.

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

Sample Output

-1
1.3333333333333335
1.6666666666666667
1.5833333333333335
1.7142857142857142

Source


题意见白书

感觉好神,我太弱了
用到了集合表示状态,每个状态保存所有二叉树的方案(开一个vector保存结构体)
dfs时枚举左右集合的元素避免重复
更新时枚举左右子树用到了哪种二叉树方案
有点类似记忆化吧,每种状态只保存了一次,只计算一次
 
代码抄的白书
//
// main.cpp
// poj2743
//
// Created by Candy on 9/28/16.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
const int N=;
double r,sum[<<N];
int n,w[N],T,vis[<<N];
struct node{
double l,r;
int ls,rs;
node():l(),r(){}
};
vector<node> tree[<<N];
void dfs(int subset){//printf("dfs %d\n",subset);
if(vis[subset]) return;
vis[subset]=;
int child=;
for(int left=(subset-)&subset;left;left=(left-)&subset){
child=;
int right=left^subset;
dfs(left);dfs(right); double dl=sum[right]/sum[subset],dr=sum[left]/sum[subset];
for(int i=;i<tree[left].size();i++)
for(int j=;j<tree[right].size();j++){
node t;
t.l=max(tree[left][i].l+dl,tree[right][j].l-dr);
t.r=max(tree[left][i].r-dl,tree[right][j].r+dr);
if(t.l+t.r<r) tree[subset].push_back(t);
}
}
if(!child) tree[subset].push_back(node());//leaf
}
int main(int argc, const char * argv[]) {
scanf("%d",&T);
while(T--){
scanf("%lf%d",&r,&n);
for(int i=;i<n;i++) scanf("%d",&w[i]);
for(int i=;i<(<<n);i++){
sum[i]=vis[i]=;
tree[i].clear();
for(int j=;j<n;j++) if(i&(<<j)) sum[i]+=w[j];
}
//for(int i=0;i<(1<<n);i++) printf("sum %d\n",sum[i]);
int root=(<<n)-;
dfs(root); double ans=-;
for(int i=;i<tree[root].size();i++)
ans=max(ans,tree[root][i].l+tree[root][i].r);
printf("%.10f\n",ans);
}
return ;
}

POJ2743Mobile Computing[DFS 状态压缩]的更多相关文章

  1. uva10160(dfs+状态压缩)

    题意:给出n个点,以及m条边,这些边代表着这些点相连,修一个电力站,若在某一点修一个站,那么与这个点相连的点都可以通电,问所有的点都通电的话至少要修多少个电力站........ 思路:最多给出的是35 ...

  2. 2101 可达性统计(拓扑排序/dfs+状态压缩)

    [题目描述] 给定一张N个点M条边的有向无环图,分别统计从每个点出发能够到达的点的数量.N,M≤30000. [题目链接] 2101 可达性统计 [算法] 拓扑排序之后逆序计算(感觉dfs更好写而且应 ...

  3. HDU 4921 Map DFS+状态压缩+乘法计数

    算最多十条链,能截取某前缀段,每种方案都可以算出一个权值,每种方案的概率都是总数分之一,问最后能构成的所有可能方案数. 对计数原理不太敏感,知道是DFS先把链求出来,但是想怎么统计方案的时候想了好久, ...

  4. POJ 1632 Vase collection【状态压缩+搜索】

    题目传送门:http://poj.org/problem?id=1632 Vase collection Time Limit: 1000MS   Memory Limit: 10000K Total ...

  5. codeforces B - Preparing Olympiad(dfs或者状态压缩枚举)

    B. Preparing Olympiad You have n problems. You have estimated the difficulty of the i-th one as inte ...

  6. 最大联通子数组之和(dfs,记忆化搜索,状态压缩)

    最大联通子数组,这次的题目,我采用的方法为dfs搜索,按照已经取到的数v[][],来进行搜索过程的状态转移,每次对v[][]中标记为1的所有元素依次取其相邻的未被标记为1的元素,将其标记为1,然而,这 ...

  7. poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)

    Description Flip game squares. One side of each piece is white and the other one is black and each p ...

  8. UVA 1508 - Equipment 状态压缩 枚举子集 dfs

    UVA 1508 - Equipment 状态压缩 枚举子集 dfs ACM 题目地址:option=com_onlinejudge&Itemid=8&category=457& ...

  9. hihocoder 1334 - Word Construction - [hiho一下第170周][状态压缩+DFS]

    题目链接:https://hihocoder.com/problemset/problem/1334 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Given N wo ...

随机推荐

  1. angularjs 指令—— 绑定策略(@、=、&)

    angularjs 指令—— 绑定策略(@.=.&) 引入主题背景:angular 的指令配置中的template可以直接使用硬编码写相应的代码,不过也可以根据变量,进行动态更新.那么需要用到 ...

  2. 系统安装LOL等游戏后出现VS调试报错"无法调试""拒绝访问"之类的调试错误

    一个问题抠得脑壳痛,度娘一番各种各样的答案.基本属于 1,调试权限被清0 2,文件权限问题   其中看到很多解决方案中提到"重启电脑"的说法.我也试了几次不行甚至游戏都卸载了.后来 ...

  3. ZedGraph饼图---傻瓜版

    GraphPane pGraphPane=this.zedGraphControl1.GraphPane;//调用饼图类 pGraphPane.Title.Text = "重金属含量分析图& ...

  4. 奇葩问题:This file could not be checked in because the original version of the file on the server was moved or deleted. A new version of this file has been saved to the server, but your check-in comments were not saved

    "This file could not be checked in because the original version of the file on the server was m ...

  5. 深入浅出-iOS程序性能优化 (转载)

    iOS应用是非常注重用户体验的,不光是要求界面设计合理美观,也要求各种UI的反应灵敏,我相信大家对那种一拖就卡卡卡的 TableView 应用没什么好印象. iOS应用是非常注重用户体验的,不光是要求 ...

  6. Git 分支管理策略

    分支管理策略 下面我们来说一下一般企业中开发一个项目的分支策略: 主分支 master 开发分支 develop 功能分支 feature 预发布分支  release bug 分支 fixbug 其 ...

  7. JNI输出log信息

    1.修改Android.mk 如生成的库文件是“.so文件”,则在Android.mk中添加如下内容: LOCAL_LDLIBS:=-L$(SYSROOT)/usr/lib -llog 如生成的库文件 ...

  8. ios 颜色转图片

    - (UIImage *)imageWithColor:(UIColor*) color{    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);    ...

  9. 我的Android六章:Android中SQLite数据库操作

    今天学习的内容是Android中的SQLite数据库操作,在讲解这个内容之前小编在前面有一篇博客也是讲解了SQLite数据库的操作,而那篇博客的讲解是讲述了 如何在Window中通过DOM来操作数据库 ...

  10. 自定义标签 与 JSTL(JSP Standard Tag Library)

    1.自定义标签 [理解]     [1]简介            > 在JSP2.0以后,在jsp页面中不建议使用脚本片段<% %>和JSP表达式<%= %>     ...