POJ2743Mobile Computing[DFS 状态压缩]
| Time Limit: 2000MS | Memory Limit: 65536K | |||
| Total Submissions: 666 | Accepted: 224 | Special Judge | ||
Description
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
r
s
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
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
题意见白书
感觉好神,我太弱了
//
// 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-)⊂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 状态压缩]的更多相关文章
- uva10160(dfs+状态压缩)
题意:给出n个点,以及m条边,这些边代表着这些点相连,修一个电力站,若在某一点修一个站,那么与这个点相连的点都可以通电,问所有的点都通电的话至少要修多少个电力站........ 思路:最多给出的是35 ...
- 2101 可达性统计(拓扑排序/dfs+状态压缩)
[题目描述] 给定一张N个点M条边的有向无环图,分别统计从每个点出发能够到达的点的数量.N,M≤30000. [题目链接] 2101 可达性统计 [算法] 拓扑排序之后逆序计算(感觉dfs更好写而且应 ...
- HDU 4921 Map DFS+状态压缩+乘法计数
算最多十条链,能截取某前缀段,每种方案都可以算出一个权值,每种方案的概率都是总数分之一,问最后能构成的所有可能方案数. 对计数原理不太敏感,知道是DFS先把链求出来,但是想怎么统计方案的时候想了好久, ...
- POJ 1632 Vase collection【状态压缩+搜索】
题目传送门:http://poj.org/problem?id=1632 Vase collection Time Limit: 1000MS Memory Limit: 10000K Total ...
- codeforces B - Preparing Olympiad(dfs或者状态压缩枚举)
B. Preparing Olympiad You have n problems. You have estimated the difficulty of the i-th one as inte ...
- 最大联通子数组之和(dfs,记忆化搜索,状态压缩)
最大联通子数组,这次的题目,我采用的方法为dfs搜索,按照已经取到的数v[][],来进行搜索过程的状态转移,每次对v[][]中标记为1的所有元素依次取其相邻的未被标记为1的元素,将其标记为1,然而,这 ...
- 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 ...
- UVA 1508 - Equipment 状态压缩 枚举子集 dfs
UVA 1508 - Equipment 状态压缩 枚举子集 dfs ACM 题目地址:option=com_onlinejudge&Itemid=8&category=457& ...
- hihocoder 1334 - Word Construction - [hiho一下第170周][状态压缩+DFS]
题目链接:https://hihocoder.com/problemset/problem/1334 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Given N wo ...
随机推荐
- 自动适应iframe右边的高度
在开发项目过程中,用iframe嵌套,会发现一个问题,用iframe嵌套的html结构右边不会自动适应高度. 这时候找到了一个解决方法: <iframe name="my_iframe ...
- 移动端H5-第一课css篇
1.移动端开发视窗口的添加 h5端开发下面这段话是必须配置的 meta name="viewport" content="width=device-width, init ...
- php获取数组第一个值 current()
获取数组第一个元素的值,如果是数字索引那还好,直接$array[0],如果键名是字符串,你又未知这个字符串呢?用current()函数就可以做到. current() 函数返回数组中的当前元素(单元) ...
- JS常用的function集合
1.把字符串转为日期格式 (1) var str ='2012-08-12 23:13:15';str = str.replace(/-/g,"/");var date = ne ...
- JavaScriptSerializer序列化时间处理
JavaScriptSerializer序列化时间后会把时间序列化成N进制的鬼数据,于是查了下质料坐下记录 假设list = News List<Text>(){new Text(){id ...
- Facebook开源动画库 POP-POPSpringAnimation运用
POPSpringAnimation也许是大多数人使用POP的理由 其提供一个类似弹簧一般的动画效果:实例源代码已经上传至gitHub,地址:https://github.com/wujunyang/ ...
- 内存管理范围和@property
管理范围: 管理任何继承NSObject的对象,对其他的基本数据类型无 效 本质原因是因为对象和其他数据类型在系统中的存储空间不一样,其它局部变量主要存放于 栈中,而对象存储于堆中,当代码块结束时这个 ...
- jQuery删除节点和追加节点
for (var i in checkedBoxIds) { var $td = $("#" + checkedBoxIds[i]).parent().parent().detac ...
- iOS-UITableView的优化(纯手打原创)
TableView的优化 一:什么是TableView的优化以及为什么要优化 1)CPU(中央处理器)和GPU(图形处理器) CPU主要从事逻辑计算的一些工作 GPU主要从事图形处理方面的工作 2 ...
- 源码编译安装gcc-5.3.0
系统环境:Amazon Linux AMI 2015.09.2 (HVM)---Fedora 23 Server 1.下载gcc-5.3.0安装包并将gcc-5.3.0.tar.gz放到/opt目录下 ...