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. 【再探backbone 02】集合-Collection

    前言 昨天我们一起学习了backbone的model,我个人对backbone的熟悉程度提高了,但是也发现一个严重的问题!!! 我平时压根没有用到model这块的东西,事实上我只用到了view,所以昨 ...

  2. 错误 1 “System.Data.DataRow.DataRow(System.Data.DataRowBuilder)”不可访问,因为它受保护级别限制

    new DataRow 的方式: DataTable pDataTable = new DataTable(); DataRow pRow = new DataRow(); 正确的方式: DataRo ...

  3. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q36-Q39)

    Question 36 You are designing a SharePoint 2010 application. You need to design the application so t ...

  4. 5个强大的Java分布式缓存框架推荐

    在开发中大型Java软件项目时,很多Java架构师都会遇到数据库读写瓶颈,如果你在系统架构时并没有将缓存策略考虑进去,或者并没有选择更优的 缓存策略,那么到时候重构起来将会是一个噩梦.本文主要是分享了 ...

  5. 普通View的measure流程

    对于普通的view,其测量在ViewGroup中的measureChildWithMargins函数中调用child view的measure开始测量. 1:从measure函数开始 public f ...

  6. iOS学习路线

    这个学习路线必须发布到首页候选区.这个学习路线必须发布到首页候选区.这个学习路线必须发布到首页候选区.这个学习路线必须发布到首页候选区.这个学习路线必须发布到首页候选区.这个学习路线必须发布到首页候选 ...

  7. iOS中如何知道app版本已更新

    主要用于程序升级,开启程序后是否显示新特性两个方面. 1.苹果app版本 苹果规定,程序的版本只能升不能降.例如1.0->1.1可以,1.1->1.0就不可以,不允许上架. 2.app版本 ...

  8. [转]setValue和setObject的区别

    在使用NSMutableDictionary的时候经常会使用setValue forKey与setObject forKey,他们经常是可以交互使用的,代码中经常每一种的使用都有. 1,先看看setV ...

  9. servlet 学习(二)

    一.ServletConfig讲解 1.1.配置Servlet初始化参数 在Servlet的配置文件web.xml中,可以使用一个或多个<init-param>标签为servlet配置一些 ...

  10. WebService的介绍概念 收藏

    WebService学习总结(二)——WebService相关概念介绍 一.WebService是什么? 1. 基于Web的服务:服务器端整出一些资源让客户端应用访问(获取数据) 2. 一个跨语言.跨 ...