UVa 1354 Mobile Computing | GOJ 1320 不加修饰的天平问题 (例题 7-7)
传送门1(UVa): https://uva.onlinejudge.org/external/13/1354.pdf
传送门2(GOJ): http://acm.gdufe.edu.cn/Problem/read/id/1320
题意: 长度限制 r (1 < r < 10), 给 n (1 <= n <= 6) 个砝码,组成平衡(考虑重量和力臂)的天平,求天平最长能多长。
2015个人选拔赛#6 1004
比赛的时候完全不知道怎么做,比赛完两天重新看一遍有点思路就是敲不出来(弱渣...)=_=
跟着Wenjun师兄的代码学了一下
caodan的是最近在写多重for循环的时候总是在里层写错变量........找半天啊还好几个啊我这是怎么了................
二进制枚举,类似线段树从底层一层一层处理
#include <bits/stdc++.h>
using namespace std; struct Tree{
double l, r;
Tree(double ll = 0.0, double rr = 0.0): l(ll), r(rr) {}
}; const int MAXN = ;
int n;
bool vis[<<MAXN]; // 是否访问过该子集
double r, w[MAXN], sum[<<MAXN];
vector<Tree> tree[<<MAXN]; // 保存各子集符合题意的解 // 计算该子集包含的砝码个数,当为1时相当于到达二叉树结点
int count(int x){
int ans = ;
for(int i = ; i < n; ++i)
if(x & (<<i)) ++ans;
return ans;
} void dfs(int subset){
if(vis[subset]) return ;
vis[subset] = true;
if(count(subset) == ){
tree[subset].push_back(Tree());
return;
} //枚举该集合的所有子集
for(int left = (subset-) & subset; left; left = (left-) & subset){
int right = subset ^ left; //根据公式求当前左右集合对应力臂长度
double leftlen = sum[right] / sum[subset];
double rightlen = sum[left] / sum[subset];
dfs(left); dfs(right); for(int i = ; i < tree[left].size(); ++i){
for(int j = ; j < tree[right].size(); ++j){
double ll = max(tree[left][i].l + leftlen, tree[right][j].l - rightlen);
double rr = max(tree[right][j].r + rightlen, tree[left][i].r - leftlen);
if(ll + rr < r) tree[subset].push_back(Tree(ll, rr));
}
}
}
} int main(){
int t;
scanf("%d", &t);
while(t--){
scanf("%lf %d", &r, &n);
for(int i = ; i < n; ++i) scanf("%lf", &w[i]);
for(int i = ; i < (<<n); ++i){
sum[i] = ;
tree[i].clear();
for(int j = ; j < n; ++j){
if((i>>j) & ) sum[i] += w[j]; //二进制枚举各个子集的重量和
}
}
int root = (<<n) - ; //整个天平
memset(vis, false, sizeof(vis));
dfs(root);
double ans = -;
for(int i = ; i < tree[root].size(); ++i)
ans = max(ans, tree[root][i].l + tree[root][i].r);
if(ans == -) printf("-1\n");
else printf("%.15lf\n", ans);
}
return ;
}
UVa 1354 Mobile Computing | GOJ 1320 不加修饰的天平问题 (例题 7-7)的更多相关文章
- UVa 1354 Mobile Computing[暴力枚举]
**1354 Mobile Computing** There is a mysterious planet called Yaen, whose space is 2-dimensional. Th ...
- uva 1354 Mobile Computing ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5
- Uva 1354 Mobile Computing
题目链接 题意: 在一个宽为r 的房间里, 有s个砝码, 每个天平的一端要么挂砝码, 要么挂另一个天平, 并且每个天平要保持平衡. 求使得所有砝码都放在天平上, 且总宽度不超过房间宽度的最大值. 思路 ...
- Mobile Computing: the Next Decade论文 cloudlet薄云
1 Introduction “Information at your fingertips anywhere, anytime” has been the driving vision of mob ...
- UVa 1354 天平难题 Mobile Computing
整个题考虑起来 最主要要计算的状态 是树的状态 于是要计算出所有可能挂坠可能组成的树的所有形态 tree 用于保存这些状态 考虑不要重复计算,有一个vis 数组 预处理可以先计算出一棵树的重量,简化计 ...
- UVa 1354 枚举子集 Mobile Computing
只要枚举左右两个子天平砝码的集合,我们就能算出左右两个悬挂点到根悬挂点的距离. 但是题中要求找尽量宽的天平但是不能超过房间的宽度,想不到要怎样记录结果. 参考别人代码,用了一个结构体的vector,保 ...
- 【例题 7-7 UVA - 1354】Mobile Computing
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 秤砣都是在叶子节点. 可以把它看成一个二叉树. 则我们每次只需要选择任意两个"节点",让他们组成一棵二叉树就可以 ...
- UVa 536 Tree Recovery | GOJ 1077 Post-order (习题 6-3)
传送门1: https://uva.onlinejudge.org/external/5/536.pdf 传送门2: http://acm.gdufe.edu.cn/Problem/read/id/1 ...
- UVa 1354 天平难题
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
随机推荐
- TMCache
TMCache设计的目的是用于存储临时持久化对象的开源iOS/OS key/value缓存类库,减少重复创建像下载数据.缓慢的处理结果这样的昂贵性能花销.TMCache由两个本身相似的存储组成,一个 ...
- orm2
数据库连接 var orm = require("orm"); orm.connect("mysql://username:password@host/database& ...
- 遍历HashMap的最佳方式
public static void printMap(Map mp) { Iterator it = mp.entrySet().iterator(); while (it.hasNext()) { ...
- yarn的调度器
三种调度器 1.FIFO Scheduler 把应用按提交的顺序排成一个队列,这是一个先进先出队列,在进行资源分配的时候,先给队列中最头上的应用进行分配资源,等最前面的应用需求满足后再给下一个分配,以 ...
- 在Freeplane中显示与隐藏层级图标
如下图所示,当选择了Edit → Icons → Show icons hierarchically时,给子节点Subsection 1.2.1添加的图标也同样会显示在其所有的父节点上,并且,它们并不 ...
- JavaScript中常用的Document了解
在我们使用js的时候经常会用到document 例如: document.write(""); document.getElementById(''); document.crea ...
- Java 彩色图转灰度图
1. 方法1 BufferedImage grayImage = new BufferedImage(width, height, colorImage.TYPE_BYTE_GRAY); Graphi ...
- HDU 5886 Tower Defence
树的直径. 比赛的时候想着先树$dp$处理子树上的最长链和次长链,然后再从上到下进行一次$dfs$统计答案,和$CCPC$网络赛那个树$dp$一样,肯定是可以写的,但会很烦.......后来写崩了. ...
- FZU 2240 Daxia & Suneast's problem
博弈,$SG$函数,规律,线段树. 这个问题套路很明显,先找求出$SG$函数值是多少,然后异或起来,如果是$0$就后手赢,否则先手赢.修改操作和区间查询的话可以用线段树维护一下区间异或和. 数据那么大 ...
- ios UIImageView处理图片大小问题
UIImageView视图可以显示图片 实例化UIImageView有两种方法 第一种方法: UIImageView *myImageView = [[ UIImageView alloc] init ...