UVa 1354 天平难题 Mobile Computing
整个题考虑起来 最主要要计算的状态 是树的状态 于是要计算出所有可能挂坠可能组成的树的所有形态 tree 用于保存这些状态 考虑不要重复计算,有一个vis 数组 预处理可以先计算出一棵树的重量,简化计算 #include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm> using namespace std;
const int maxn=6;
int w[6];//各个重量
int sum[1<<maxn]; //储存在i集合树的情况下总重量
int n,s;
double r;
int vis[1<<maxn];//探究当前组成的树是否被构造过 struct Tree{
double r,l;
Tree(): r(0),l(0){
}
};
vector<Tree>tree[1<<maxn];//下标的树组成的全部可能形态
void dfs(int root){ //计算以root 组成一棵树的所有可能形态,并标记已记录
if(vis[root])return;
vis[root]=true;
bool ok=false;
for(int left=root&(root-1);left;left=(left-1)&root){
int right=left^root;
ok=true;
dfs(left);
dfs(right);
double d1=(sum[left]*1.0)/(sum[root]*1.0);
double d2=(sum[right]*1.0)/(sum[root]*1.0);
for(int i=0;i<tree[left].size();i++){
for(int j=0;j<tree[right].size();j++){
Tree t;
t.l=max(tree[left][i].l+d1,tree[right][j].l-d2);
t.r=max(tree[left][i].r-d2,tree[right][j].r+d2);
if(t.l+t.r<r)tree[root].push_back(t);
}
}
}
if(!ok)tree[root].push_back(Tree());//如果当前树只有一个挂坠,那么这是一个空树
}
int main()
{
freopen("t.in","r",stdin);
freopen("t.out","w",stdout);
cin>>n;
while(n--){
cin>>r>>s;
for(int i=0;i<s;i++){
cin>>w[i]; }
for(int i=0;i< 1<<s;i++){
vis[i]=false;
tree[i].clear();
sum[i]=0;
for(int j=0;j<s;j++){
if(i&(1<<j))sum[i]+=w[j];
}
}//存储重量的预处理 利用一个二进制数存储了所有的信息;
int root=(1<<s)-1;
dfs(root);
long double ans=-1;
for(int i=0;i<tree[root].size();i++){
if(tree[root][i].l+tree[root][i].r>ans)ans=tree[root][i].l+tree[root][i].r;
}
cout<<ans<<endl;
}
}
UVa 1354 天平难题 Mobile Computing的更多相关文章
- UVa 1354 天平难题
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVa 1354 枚举子集 Mobile Computing
只要枚举左右两个子天平砝码的集合,我们就能算出左右两个悬挂点到根悬挂点的距离. 但是题中要求找尽量宽的天平但是不能超过房间的宽度,想不到要怎样记录结果. 参考别人代码,用了一个结构体的vector,保 ...
- UVa 1354 天平难题 (枚举二叉树)
题意: 分析: 其实刚看到这题的时候觉得很难, 以至于结束了第七章然后去做了一遍第六章树的部分.现在再做这题觉得思路并不是太难,因为总共就只有六个结点,那么只要枚举二叉树然后算出天平然后再从叶子往上推 ...
- UVa 1354 Mobile Computing[暴力枚举]
**1354 Mobile Computing** There is a mysterious planet called Yaen, whose space is 2-dimensional. Th ...
- UVA.839 Not so Mobile ( 二叉树 DFS)
UVA.839 Not so Mobile ( 二叉树 DFS) 题意分析 给出一份天平,判断天平是否平衡. 一开始使用的是保存每个节点,节点存储着两边的质量和距离,但是一直是Runtime erro ...
- UVa 839 -- Not so Mobile(树的递归输入)
UVa 839 Not so Mobile(树的递归输入) 判断一个树状天平是否平衡,每个测试样例每行4个数 wl,dl,wr,dr,当wl*dl=wr*dr时,视为这个天平平衡,当wl或wr等于0是 ...
- 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 | GOJ 1320 不加修饰的天平问题 (例题 7-7)
传送门1(UVa): https://uva.onlinejudge.org/external/13/1354.pdf 传送门2(GOJ): http://acm.gdufe.edu.cn/Probl ...
- uva 1354 Mobile Computing ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5
随机推荐
- eclipse项目debug方法
属性查看代码在哪里存 本地项目启动 1,2步骤需要success
- Scale和Resolution的含义及转换算法
当我们在用arcgis server 构建切片时,我们会发现在缓存生成的conf.xml中有这样的片段: 在上述片段中<LODInfo>代表了每一级切片的信息,<LevelID> ...
- usb-serial驱动问题
pl2303 prolific usb-serial驱动,驱动安装后,win10下仍然有问题,选择更新驱动程序-从计算机选择-列表选择-尝试不同版本程序.
- mac上启动和停止mysql
因调试需要,在mac上安装了mysql,安装方法网上大把,此处不赘述.启动和停止命令每次要手工敲,因此写个小脚本方便自己: startmysql.sh(/Applications/Develop/my ...
- IntelliJ IDEA - 代码辅助功能
Eclipse 和 IntelliJ IDEA 都提供了写代码的辅助功能,包括代码补全.代码生成.快速修饰和动态模板等功能. 1. 快速修复(Quick-fixes) 快捷键:Alt+Enter 所有 ...
- Java基础(二) ---- 继承(Inheritance)
- appium + maven +jenkins 基本入门之二 新建maven 的java项目
1: 下载maven : 1.0 :设置maven的环境变量: 1.1: 设置maven本地仓库: 在下载好的maven文件夹找到 apache-maven-3.3.9/conf 文件夹下的setti ...
- CPU阿甘:函数调用的秘密
个人感言:真正的知识是深入浅出的,码农翻身" 公共号将苦涩难懂的计算机知识,用形象有趣的生活中实例呈现给我们,让我们更好地理解.感谢"码农翻身" 公共号,感谢你们的成果, ...
- Linux:history命令记录操作时间、操作用户、操作IP
[步骤] 1./etc/profile文件中加入以下内容 2.执行:source /etc/profile [效果]
- cs11_c++_lab7
wcount.cc #include <iostream> #include <map> #include <string> #include <algori ...