只要枚举左右两个子天平砝码的集合,我们就能算出左右两个悬挂点到根悬挂点的距离。

但是题中要求找尽量宽的天平但是不能超过房间的宽度,想不到要怎样记录结果。

参考别人代码,用了一个结构体的vector,保存每个集合合法方案的左右两端最长的距离。

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>
#define MP make_pair
#define Ft first
#define Sd second
using namespace std; typedef pair<double, double> PDD; const int maxn = ;
const int maxs = ; vector<PDD> tree[maxs]; int n;
double r;
double a[maxn], w[maxs];
bool vis[maxs]; int bitcount(int x)
{
int ans = ;
while(x) { ans += (x & ); x >>= ; }
return ans;
} void dfs(int S)
{
if(vis[S]) return ;
vis[S] = true;
if(bitcount(S) == ) { tree[S].push_back(MP(, )); return ; } PDD t = MP(, );
for(int s1 = (S-)&S; s1; s1 = (s1-)&S)
{
int s2 = S ^ s1;
dfs(s1); dfs(s2);
double x1 = w[s2] / w[S], x2 = w[s1] / w[S];
for(int i = ; i < tree[s1].size(); i++)
for(int j = ; j < tree[s2].size(); j++)
{
t.Ft = max(x1 + tree[s1][i].Ft, tree[s2][j].Ft - x2);
t.Sd = max(x2 + tree[s2][j].Sd, tree[s1][i].Sd - x1);
if(t.Ft + t.Sd < r) tree[S].push_back(t);
}
}
} int main()
{
int T; scanf("%d", &T);
while(T--)
{
scanf("%lf%d", &r, &n);
for(int i = ; i < n; i++) scanf("%lf", a + i);
int all = ( << n) - ; for(int i = ; i <= all; i++)
{
w[i] = ;
tree[i].clear();
for(int j = ; j < n; j++) if(i & ( << j))
w[i] += a[j];
} memset(vis, false, sizeof(vis));
dfs(all);
double ans = -;
for(int i = ; i < tree[all].size(); i++) ans = max(ans, tree[all][i].Ft + tree[all][i].Sd);
if(ans < ) puts("-1");
else printf("%.9f\n", ans);
} return ;
}

代码君

UVa 1354 枚举子集 Mobile Computing的更多相关文章

  1. UVa 1354 天平难题 Mobile Computing

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

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

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

  3. UVA1354-Mobile Computing(二进制枚举子集)

    Problem UVA1354-Mobile Computing Accept:267  Submit:2232 Time Limit: 3000 mSec  Problem Description ...

  4. UVa 11825 - Hackers' Crackdown DP, 枚举子集substa = (substa - 1)&sta 难度: 2

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

  5. UVA 1508 - Equipment 状态压缩 枚举子集 dfs

    UVA 1508 - Equipment 状态压缩 枚举子集 dfs ACM 题目地址:option=com_onlinejudge&Itemid=8&category=457& ...

  6. UVA 11825 - Hackers&#39; Crackdown 状态压缩 dp 枚举子集

    UVA 11825 - Hackers' Crackdown 状态压缩 dp 枚举子集 ACM 题目地址:option=com_onlinejudge&Itemid=8&page=sh ...

  7. 紫书 例题 11-3 UVa 1151 (有边集的最小生成树+二进制枚举子集)

    标题指的边集是说这道题的套餐, 是由几条边构成的. 思路是先做一遍最小生成树排除边, 因为如果第一次做没有加入的边, 到后来新加入了很多权值为0的边,这些边肯定排在最前面,然后这条边的前面的那些边肯定 ...

  8. UVa 11025 The broken pedometer【枚举子集】

    题意:给出一个矩阵,这个矩阵由n个数的二进制表示,p表示用p位二进制来表示的一个数 问最少用多少列就能将这n个数区分开 枚举子集,然后统计每一种子集用了多少列,维护一个最小值 b[i]==1代表的是选 ...

  9. UVA - 1151 Buy or Build (买还是建)(并查集+二进制枚举子集)

    题意:平面上有n个点(1<=n<=1000),你的任务是让所有n个点连通.可以新建边,费用等于两端点欧几里德距离的平方.也可以购买套餐(套餐中的点全部连通).问最小费用. 分析: 1.先将 ...

随机推荐

  1. MYSQL查询~ 存在一个表而不在另一个表中的数据

    A.B两表,找出ID字段中,存在A表,但是不存在B表的数据.A表总共13w数据,去重后大约3W条数据,B表有2W条数据,且B表的ID字段有索引. 方法一 使用 not in ,容易理解,效率低  ~执 ...

  2. TestNG ABC

    TestNG ABC 资源 官网 :http://testng.org/doc/index.html Maven示例 <dependency>             <groupI ...

  3. Java输入输出流简单案例

    package com.jckb; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io. ...

  4. ASPCTJ

    ublic interface ISomeService { public void doSome(); public String dade(); } public class SomeServic ...

  5. vue2.0:(六)、移动端像素border的实现和整合引入less文件

    知识点一.如何在手机上看我们制作的移动端页面. 正常我们在电脑上都是按如下图来制作手机页面的: 如果要在手机上面看就不能用localhost了.所以,进入命令行,输入ipconfig查看本地ip地址: ...

  6. ECshop安装提示cls_image::gd_version() 和不支持JPEG

    ecshop版本:ECShop_V2.7.3_UTF8_release1106php 版本 5.5--------------------------------------------------- ...

  7. HTML_5 (1 2 3的代码总结)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. pycharm 使用技巧

    格式化代码为pep8: ctrl+alt+l http://edu.51cto.com//index.php?do=lession&id=163794

  9. C#数组协方差

    对于任意两个“引用类型”A和B,如果存在从A到B的隐式引用转换或显式引用转换,则也一定存在从数组类型A[R]到数组类型B[R]的相同的引用转换,其中R可以是任何给定的“秩说明符”,但是这两个数组类型必 ...

  10. HTML5<nav>元素

    HTML5中<nav>元素定义页面导航链接的部分区域,但并不是所有的链接都放到nav元素里面. 实例: <header id="pageHeader"> & ...