题目链接

题意:

  在一个宽为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的更多相关文章

  1. UVa 1354 Mobile Computing[暴力枚举]

    **1354 Mobile Computing** There is a mysterious planet called Yaen, whose space is 2-dimensional. Th ...

  2. 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 ...

  3. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

  4. Mobile Computing: the Next Decade论文 cloudlet薄云

    1 Introduction “Information at your fingertips anywhere, anytime” has been the driving vision of mob ...

  5. UVa 1354 天平难题 Mobile Computing

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

  6. UVa 1354 枚举子集 Mobile Computing

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

  7. 【例题 7-7 UVA - 1354】Mobile Computing

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 秤砣都是在叶子节点. 可以把它看成一个二叉树. 则我们每次只需要选择任意两个"节点",让他们组成一棵二叉树就可以 ...

  8. UVa 1354 天平难题

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

  9. uva 688 - Mobile Phone Coverage

    经典问题,矩形面积并. 解法:一.矩形分割,每个矩形的两个横坐标和两个纵坐标排序,这样得到2n*2n个区间,对这些区间依次判断是否包含在n个矩形中间即可.      二.扫描线.具体还没实现过. 详见 ...

随机推荐

  1. iOS开发中常用到的加密方式

    1 base64 1.1 简介 Base64编码的思想是是采用64个基本的ASCII码字符对数据进行重新编码.它将需要编码的数据拆分成字节数组.以3个字节为一组.按顺序排列24位数据,再把这24位数据 ...

  2. Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解(一)

    Spring更新到3.0之后,其MVC框架加入了一个非常不错的东西——那就是REST.它的开放式特性,与Spring的无缝集成,以及Spring框架的优秀表现,使得现在很多公司将其作为新的系统开发框架 ...

  3. 一种通用数据采集的schema定义形式

    { "name": "凤凰金融", "notice": { "data": "attribute", ...

  4. onitemcommand 的作用以及onitemdatabound的具体作用

    Repeater控件.DataList控件的属性:OnItemCommand 当在ItemTemplate 中所宣告的Button 或LinkButton 控件触发事件时,如果该控件CommandNa ...

  5. Container.ItemIndex 获取到行的序号

    如果在ASP.NET中应用了Repeater.Gridview,想获取到行的序号,很简单,使用Container.ItemIndex即可.在Gridview中使用<%# Container.Da ...

  6. MFC对话框程序EDIT类控件的自动换行,垂直滚动条自动下移

    1.新建一个Edit Control,将其Multiline属性设置为True,Auto HScroll属性设置False,这样就可以实现每一行填满后自动换行了.   2.再将Vetrical Scr ...

  7. C++:memset ,memcpy 和strcpy 的根本区别!

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h&g ...

  8. 周末充电之WPF(二 ) .窗口的布局

    登录窗口布局:[ Grid 布局 -Grid.RowDefinitions / Grid.ColumnDefinitions] 代码如下: <Window x:Class="login ...

  9. Token 的作用

    Token,就是令牌,最大的特点就是随机性,不可预测.一般黑客或软件无法猜测出来. 那么,Token有什么作用?又是什么原理呢? Token一般用在两个地方: 1)防止表单重复提交. 2)anti c ...

  10. Codevs 1958 刺激

    1958 刺激 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description saffah的一个朋友S酷爱滑雪,并且追求刺激(exitement, ...