UVA - 1354 Mobile Computing dfs
来源:互联网 发布:cnn tensorflow 编辑:程序博客网 时间:2024/05/24 06:38
题目大意:有n个砝码,要求放在天平上,每个天平的长度是1,要求放上砝码后,使得天平的水平长度达到最大
解题思路:因为砝码的数量只有6个,所以可以用一个二进制数来表示状态
枚举每种状态下的结果,左端的长度用负数表示,右端的长度用正数表示
枚举该状态时,先把当下的子状态都枚举了,然后取其最大值或者最小值即可。。。好难解释,具体看代码
#include<cstdio>#include<cmath>#include<algorithm>#include<vector>using namespace std;#define maxn (1 << 6)struct Node{ double l, r; Node() {} Node(double ll, double rr) { l = ll, r = rr; }};vector<Node> node[maxn];double w[maxn], sum[maxn], len;int n, vis[maxn];int bitcount(int x) { return x ? x % 2 + bitcount(x/2): 0;}void dfs(int s) { if(vis[s]) return ; vis[s] = 1; if(bitcount(s) == 1) { node[s].push_back(Node(0,0)); return ; } for(int l = (s - 1) & s; l > 0; l = (l - 1) & s) { int r = s ^ l; double Sum = sum[l] + sum[r]; dfs(l), dfs(r); for(int i = 0; i < node[l].size(); i++) for(int j = 0; j < node[r].size(); j++) { double ll = min(-sum[r]/Sum + node[l][i].l, sum[l] / Sum + node[r][j].l); double rr = max(-sum[r]/Sum + node[l][i].r, sum[l] / Sum + node[r][j].r); node[s].push_back(Node(ll,rr)); } }}void solve() { double ans = -1; int s = (1 << n) - 1; dfs(s); for(int i = 0; i < node[s].size(); i++) if(node[s][i].r - node[s][i].l < len) ans = max(ans, node[s][i].r - node[s][i].l); if(ans == -1) printf("-1\n"); else printf("%.16lf\n", ans);}void init() { scanf("%lf%d", &len, &n); for(int i = 0; i < n; i++) scanf("%lf", &w[i]); for(int i = 0; i < (1 << n); i++) { sum[i] = 0; vis[i] = 0; node[i].clear(); for(int j = 0; j < n; j++) if(i & (1 << j)) sum[i] += w[j]; }}int main() { int test; scanf("%d", &test); while(test--) { init(); solve(); } return 0;}
0 0
- uva 1354 - Mobile Computing(dfs)
- UVA - 1354 Mobile Computing dfs
- uva 1354 - Mobile Computing
- uva 1354 Mobile Computing
- uva 1354 Mobile Computing
- UVa 1354 - Mobile Computing(二叉树 + DFS)
- UVA 1354(p197)----Mobile Computing
- uva 1354 Mobile Computing code2
- UVA1354 Mobile Computing(DFS)
- UVA 1354 - Mobile Computing(暴力枚举)
- UVA 1354 - Mobile Computing(暴力枚举子集)
- UVa 1354 - Mobile Computing <枚举+二叉树>
- uva 1354——Mobile Computing
- 天平难题(Mobile Computing,UVa 1354)
- uva 1354 Mobile Computing ——yhx
- uva 1354 Mobile Computing ——yhx
- UVA 1354 Mobile Computing 枚举二叉树 *
- UVA 1354 Mobile Computing(暴力)
- ubuntu下安装svn服务器并创建分支
- 获得url中的参数的两种方法
- P122.第20题
- Spring异常:org.xml.sax.SAXParseException: cvc-complex-type.2.4.c
- JVM加载class文件的原理机制
- UVA - 1354 Mobile Computing dfs
- 道德经
- android获取string.xml的值
- BZOJ4034【树链剖分】【线段树】
- 转)21分钟 MySQL 入门教程
- 实现Singleton 模式——七种实现方式
- 黑马程序员---------继承、抽象、接口、多态、内部类
- HDU 2072 单词数 (STL map)
- casperjs学习入门记录