Uva 1354 Mobile Computing
题意:
在一个宽为r 的房间里, 有s个砝码, 每个天平的一端要么挂砝码, 要么挂另一个天平, 并且每个天平要保持平衡。
求使得所有砝码都放在天平上, 且总宽度不超过房间宽度的最大值。
思路:
每个节点只能有两个子节点, 这是一棵二叉树的形式。
通过枚举二叉树的形态, 再枚举每一个叶子节点所放砝码, 最后再计算每个方案的宽度并计算答案。
每增加一个天平, 那么可以放砝码数 + 1。
note:
坑在0的输出了, 用primtf("%.9lf\n", 0)输出来的是0 用0.0来输出才是0.000000 惨wa三发。
代码:
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <fstream>
#include <iterator>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define eps 1e-5
#define MAXN 110
#define MAXM 100
#define dd {cout<<"debug"<<endl;}
#define pa {system("pause");}
#define p(x) {cout<<x<<endl;}
#define pd(x) {printf("%.7lf\n", x);}
#define k(x) {printf("Case %d: ", ++x);}
#define s(x) {scanf("%d", &x);}
#define sd(x) {scanf("%lf", &x);}
#define mes(x, d) {memset(x, d, sizeof(x));}
#define do(i, x) for(i = 0; i < x; i ++)
#define dod(i, x, l) for(i = x; i >= l; i --)
#define doe(i, x) for(i = 1; i <= x; i ++)
int n;
double r, ans;
double w[MAXN], v[MAXN];
double ll[MAXN], rr[MAXN];
bool vis[MAXN];
int order[MAXN];
void read()
{
scanf("%lf", &r);
scanf("%d", &n);
for (int i = ; i <= n; i ++)
scanf("%lf", &w[i]);
}
void get_ans(int u)
{
memset(ll, , sizeof(ll));
memset(rr, , sizeof(rr));
memset(v, , sizeof(v)); for (int i = u; i > ; i --)
{
if (order[i] == -)
{
int x = i * ;
int y = i * + ;
v[i] = v[x] + v[y];
double li = v[y] / v[i];
double ri = v[x] / v[i]; ll[i] = min(-li + ll[x], ri + ll[y]);
rr[i] = max(-li + rr[x], ri + rr[y]);
}
else if (order[i])
{
v[i] = w[order[i]];
}
} double temp = rr[] - ll[];
//printf("%.9lf\n", temp);
if (temp - r < eps && temp > ans)
ans = temp;
} void dfs(int u, int num, int count)
{
//printf("%d %d %d\n", u, num, count);
if (count == )
{
get_ans(u - );
return ;
}
else if (order[u / ] != -)
{
dfs(u + , num, count);
}
else
{
if (count > num)
{
order[u] = -;
dfs(u + , num + , count);
order[u] = ;
} if (num == && count > )
return ;
for (int i = ; i <= n; i ++)
if (!vis[i])
{
vis[i] = true;
order[u] = i;
dfs(u + , num - , count - );
order[u] = ;
vis[i] = false;
}
}
}
void solve()
{
memset(vis, false, sizeof(vis));
memset(order, , sizeof(order));
ans = -;
if (n == ) printf("%.10lf\n", 0.0);
else
{
order[] = -;
dfs(, , n);
printf(ans == -? "-1\n" : "%.10lf\n", ans);
}
} int main()
{
int T;
scanf("%d", &T);
while (T --)
{
read();
solve();
}
return ;
}
Uva 1354 Mobile Computing的更多相关文章
- UVa 1354 Mobile Computing[暴力枚举]
**1354 Mobile Computing** There is a mysterious planet called Yaen, whose space is 2-dimensional. Th ...
- 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
- 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 1354 天平难题
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- uva 688 - Mobile Phone Coverage
经典问题,矩形面积并. 解法:一.矩形分割,每个矩形的两个横坐标和两个纵坐标排序,这样得到2n*2n个区间,对这些区间依次判断是否包含在n个矩形中间即可. 二.扫描线.具体还没实现过. 详见 ...
随机推荐
- PHP【第一篇】安装
一.准备 1.环境 系统平台:Red Hat Enterprise Linux Server release 7.3 (Maipo) 内核版本:3.10.0-514.el7.x86_64 2.下载安装 ...
- 两个常用的功能,将shp数据属性转成TXT和Excel(转)
//转成txt public static void ConvertTable2Txt(ITable pTable, string pFilePath) { int pIndex = 0 ...
- sizeof strlen strncpy用法总结 结构体实际所占内存大小 以及memset用法
sizeof测类型(数组名除外) strlen测实际长度 strncpy返回指针类型 #include <stdio.h> #include <stdlib.h> #inclu ...
- 深入理解计算机系统第二版习题解答CSAPP 2.5
分别写出十六进制数0x87654321在大端法机器和小端法机器上第1个.前2个.前3个字节. A.小端法:21 大端法:87 B.小端法:21 43 大端法:87 65 C.小端法:2 ...
- Ubuntu14.04 固定IP
Ubuntu 固定IP 1. 更改/etc/network/interfaces的内容 1.1 注释掉原来的内容 1.2 添加以下内容 auto eth0 iface eth0 inet static ...
- jasper
package jasper; import java.util.ArrayList;import java.util.HashMap;import java.util.Map; import net ...
- Android和.net API的数据交互
一..net API 一般的页面都是.aspx文件,由于.aspx文件都带有HTML的格式,我们传递的都是json格式的数据,所以html页面格式对json格式有影响,故而我们写Web-API都不会采 ...
- angularjs + springmvc 上传和下载
jsp: <form ng-submit="uploadFile()" class="form-horizontal" enctype="mul ...
- SQL server 2012 如何取上个月的最后一天
好吧 QQ群里被问到这种问题,还是这里写一下吧. DECLARE @date DATETIME = getdate(); SELECT EOMONTH (@date) AS 'Last Day Of ...
- PHP session 失效不传递的解决办法
PHP中,session不能传递到下一个页面去,一般有两种情况: 我们先写个php文件:<?=phpinfo()?>, 传到服务器去看看服务器的参数配置. 转到session部分,看到se ...