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. Error: Error setting TTL index on collection : sessions

    Error: Error setting TTL index on collection : sessions 一.步骤一: 这个问题一般是直接升级 mongodb和connect-mongo的版本为 ...

  2. javascript --- 原型初探七日谈(一)

    在javascript中,像原型,闭包这样的概念,只要我们能领悟其中的原理,一切都会显得格外清晰与明了. 原型属性(prototype): 下面我们简单定义一个函数 function her(a, b ...

  3. TABLE CONTROL隐藏列和固定列的实现

    一.设置固定列 需求:为了方便对主要关心信息地查看,用户希望TABLE CONTROL左边的一列或者几列在屏幕上固定.针对用户这样子的需求, 我们首先会想到类似与屏幕编辑/可见等字段属性设置,但是此方 ...

  4. Atitit.复合文档的格式 标准化格式

    Atitit.复合文档的格式 标准化格式 1. Docfile1 2. Iso   Cdf  cd file1 3. Zip1 4. Ooxml1 5. Odf  :OpenDocument Form ...

  5. 无插件的大模型浏览器Autodesk Viewer开发培训-武汉-2014年8月28日 9:00 – 12:00

    武汉附近的同学们有福了,这是全球第一次关于Autodesk viewer的教室培训. :) 你可能已经在各种场合听过或看过Autodesk最新推出的大模型浏览器,这是无需插件的浏览器模型,支持几十种数 ...

  6. Microsoft Azure 的负载平衡器的Session Sticky

    Microsoft Azure 的负载平衡器是一种 Layer-4负载平衡器.Microsoft Azure 负载平衡器通过针对给定输入端点上接收到的流量计算哈希函数,在一组可用的服务器(虚拟机)之间 ...

  7. yii 项目根目录下需要有assets目录

    yii 项目根目录下需要有assets目录,如果没有,会导致gii失效

  8. UITextFiled,UITextView长度限制

    长度限制用到的地方很多,但是需求都不一样.有的要求全部字符按一个处理,有的要求英文字母按一个,中文按两个,emoji按四个.这样就会遇到各种各样奇怪的问题,再被虐了无数次后,终于解决掉了.下面就来写写 ...

  9. 私有Pods封装个推SDK功能(解决方案)

    一:运用场景 公司中同时有好几个APP在开发,而且每个APP都有使用到集成个推SDK来处理消息的功能,以前的做法是每个APP都去集成并在AppDelegate处理一些SDK的代码,包含个推基础配置.消 ...

  10. 如何正确使用Cocoapods

    ➠更多技术干货请戳:听云博客 一.介绍Cocoapods Cocoapods是引入为项目引入新血液的接口,只有引入了新血液,功能才可以多样化,进而满足不同的消费群体.使用Cocoapods可以方便日后 ...