https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4100

题意:给出房间的宽度和挂坠的重量,设计一个尽量宽的天平,挂着所有挂坠,当然不可以超过房间宽度。

这道题我真的是一点想法都没有,也不知道怎么去枚举二叉树好,下面都是参考别人的代码,我自己也思考了很久,总算是搞清楚了,用二进制法来枚举子集,太妙了。

这样一来也就可以不用特别考虑右子树的左子树比左子树的左子树长的情况了。

 #include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std; const int maxn = << ;
int s;
double r;
int vis[maxn];
double sum[maxn];
double w[]; struct Node
{
double l, r;
Node(double x, double y) { l = x; r = y; }
Node() {}
}; vector<Node> node[maxn]; int bitcount(int x)
{
if (x == ) return ;
return bitcount(x / ) + (x & );
} void dfs(int k)
{
if (vis[k]) return; //如果该状态已访问,则直接返回
vis[k] = ;
if (bitcount(k) == ) //如果此时只有一个1,则只有一个子集,说明是叶子节点,天平左右都为0
{
node[k].push_back(Node(, ));
return;
}
for (int l = (k - )&k; l > ; l = (l - )&k) //二进制法枚举左右子集
{
int r = k^l; //求出补集,即右子集
dfs(l); //继续枚举左子集
dfs(r); //继续枚举右子集
for (int i = ; i < node[l].size(); i++)
{
for (int j = ; j < node[r].size(); j++)
{
double ll = min(-sum[r] / (sum[l] + sum[r]) + node[l][i].l, sum[l] / (sum[l] + sum[r]) + node[r][j].l);
double rr = max(sum[l] / (sum[l] + sum[r]) + node[r][j].r, -sum[r] / (sum[l] + sum[r]) + node[l][i].r);
node[k].push_back(Node(ll, rr)); //将该节点左右臂长加入数组
}
}
}
} void solve()
{
double width = -;
int k = ( << s) - ;
dfs(k);
for (int i = ; i < node[k].size();i++)
if (node[k][i].r - node[k][i].l<r && node[k][i].r - node[k][i].l>width)
width = node[k][i].r - node[k][i].l;
if (width == -) cout << "-1" << endl;
else printf("%.16lf\n", width);
} int main()
{
int n;
cin >> n;
while (n--)
{
memset(vis, , sizeof(vis));
memset(sum, , sizeof(sum));
memset(node, , sizeof(node));
cin >> r >> s;
for (int i = ; i < s; i++)
{
cin >> w[i];
}
for (int i = ; i < ( << s); i++) //计算出每个子集的总重量
{
for (int j = ; j < s; j++)
{
if (i & ( << j))
sum[i] += w[j];
}
}
solve();
}
return ;
}

UVa 1354 天平难题的更多相关文章

  1. UVa 1354 天平难题 (枚举二叉树)

    题意: 分析: 其实刚看到这题的时候觉得很难, 以至于结束了第七章然后去做了一遍第六章树的部分.现在再做这题觉得思路并不是太难,因为总共就只有六个结点,那么只要枚举二叉树然后算出天平然后再从叶子往上推 ...

  2. UVa 1354 天平难题 Mobile Computing

    整个题考虑起来 最主要要计算的状态 是树的状态 于是要计算出所有可能挂坠可能组成的树的所有形态 tree 用于保存这些状态 考虑不要重复计算,有一个vis 数组 预处理可以先计算出一棵树的重量,简化计 ...

  3. 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/Probl ...

  4. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

  5. UVa 839 天平

    原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  6. Uva 1354 Mobile Computing

    题目链接 题意: 在一个宽为r 的房间里, 有s个砝码, 每个天平的一端要么挂砝码, 要么挂另一个天平, 并且每个天平要保持平衡. 求使得所有砝码都放在天平上, 且总宽度不超过房间宽度的最大值. 思路 ...

  7. uva1354 天平难题 【位枚举子集】||【huffman树】

    题目链接:https://vjudge.net/contest/210334#problem/G 转载于:https://blog.csdn.net/todobe/article/details/54 ...

  8. UVa 1354 枚举子集 Mobile Computing

    只要枚举左右两个子天平砝码的集合,我们就能算出左右两个悬挂点到根悬挂点的距离. 但是题中要求找尽量宽的天平但是不能超过房间的宽度,想不到要怎样记录结果. 参考别人代码,用了一个结构体的vector,保 ...

  9. Uva 839天平(二叉树dfs, 递归建树)

    题意: 给定一个天平长度 输入格式为 wl dl wr dr 分别代表天平左边长度,左边重量, 右边长度, 右边重量. 如果重量为0, 说明下面还有一个天平, 递归给出. 样例输入:10 2 0 40 ...

随机推荐

  1. java-基础-【一】枚举

    enum 的全称为 enumeration, 是 JDK 1.5  中引入的新特性,存放在 java.lang 包中. 一.static final定义[jdk1.5之前] public static ...

  2. ABAP抓取异常的方法:try,endtry

    部分转自:http://www.cnblogs.com/jiangzhengjun/p/4292428.html +4.Funcion函数异常处理 说明: 异常处理机制可以有效避免 dump发生, : ...

  3. 使用navicat mysql 远程连接数据库

    远程连接数据库,假设两台主机上都有navicat 客户端 远程主机A    ip地址:192.168.100.91  ,port  3306,数据库用户名 rootA   密码 123456A 本地主 ...

  4. requesMapping注解

    java类 package org.springframework.web.bind.annotation; import java.lang.annotation.Documented; impor ...

  5. 使用LinkedList模拟栈数据结构的集合

    封装MyStack类 public class MyStack { private LinkedList link; //调用MyStack创建对象的时候其实是调用的LinkedList创建的是Lin ...

  6. uboot中fdt命令的使用

    转载:https://blog.csdn.net/voice_shen/article/details/7441894 依linux community的要求,从linux-3.5后,新提交的code ...

  7. php中&运算符的理解与使用

    php的引用(就是在变量或者函数.对象等前面加上&符号) 在PHP 中引用的意思是:不同的名字访问同一个变量内容. 变量的引用 PHP 的引用允许你用两个变量来指向同一个内容 例一: < ...

  8. Log Parser 2.2 + Log Parser Lizard GUI 分析IIS日志示例

    Log Parser 日志分析工具,用命令行操作,可以分析 IIS logs,event logs,active directory,log4net,file system,t-sql Log Par ...

  9. linux系统安装 dig和nslookup命令

    Fedora / Centos:1.yum install bind-utils Ubuntu: 1.sudo apt-get install dnsutils Debian: 1.2 apt-get ...

  10. Servlet—生命周期

    首次访问 重复访问 换浏览器访问(说明在一次项目运行期间,servlet实例只会创建一个)