UVa 1354 枚举子集 Mobile Computing
只要枚举左右两个子天平砝码的集合,我们就能算出左右两个悬挂点到根悬挂点的距离。
但是题中要求找尽量宽的天平但是不能超过房间的宽度,想不到要怎样记录结果。
参考别人代码,用了一个结构体的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的更多相关文章
- UVa 1354 天平难题 Mobile Computing
整个题考虑起来 最主要要计算的状态 是树的状态 于是要计算出所有可能挂坠可能组成的树的所有形态 tree 用于保存这些状态 考虑不要重复计算,有一个vis 数组 预处理可以先计算出一棵树的重量,简化计 ...
- UVa 1354 Mobile Computing[暴力枚举]
**1354 Mobile Computing** There is a mysterious planet called Yaen, whose space is 2-dimensional. Th ...
- UVA1354-Mobile Computing(二进制枚举子集)
Problem UVA1354-Mobile Computing Accept:267 Submit:2232 Time Limit: 3000 mSec Problem Description ...
- 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 ...
- UVA 1508 - Equipment 状态压缩 枚举子集 dfs
UVA 1508 - Equipment 状态压缩 枚举子集 dfs ACM 题目地址:option=com_onlinejudge&Itemid=8&category=457& ...
- UVA 11825 - Hackers' Crackdown 状态压缩 dp 枚举子集
UVA 11825 - Hackers' Crackdown 状态压缩 dp 枚举子集 ACM 题目地址:option=com_onlinejudge&Itemid=8&page=sh ...
- 紫书 例题 11-3 UVa 1151 (有边集的最小生成树+二进制枚举子集)
标题指的边集是说这道题的套餐, 是由几条边构成的. 思路是先做一遍最小生成树排除边, 因为如果第一次做没有加入的边, 到后来新加入了很多权值为0的边,这些边肯定排在最前面,然后这条边的前面的那些边肯定 ...
- UVa 11025 The broken pedometer【枚举子集】
题意:给出一个矩阵,这个矩阵由n个数的二进制表示,p表示用p位二进制来表示的一个数 问最少用多少列就能将这n个数区分开 枚举子集,然后统计每一种子集用了多少列,维护一个最小值 b[i]==1代表的是选 ...
- UVA - 1151 Buy or Build (买还是建)(并查集+二进制枚举子集)
题意:平面上有n个点(1<=n<=1000),你的任务是让所有n个点连通.可以新建边,费用等于两端点欧几里德距离的平方.也可以购买套餐(套餐中的点全部连通).问最小费用. 分析: 1.先将 ...
随机推荐
- ruby 数组array 排序sort 和sort!
1. sort → new_ary click to toggle source sort { |a, b| block } → new_ary Returns a new array created ...
- Dubbo封装rest服务返回结果
由于Dubbo服务考虑到一个是给其他系统通过RPC调用,另外一个是提供HTTP协议本身系统的后台管理页面,因此Dubbo返回参数在rest返回的时候配置拦截器进行处理. 在拦截器中,对返回参数封装成如 ...
- gitinore修改不生效
.gitignore只能忽略那些尚未被被track的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore是无效的.一个简单的解决方法就是先把本地缓存删除(改变成未track状态),然后 ...
- GetRelativePath获取相对路径
public static string GetRelativePath(string baseDirPath, string subFullPath) { // ForceBasePath to a ...
- SharePoint 2013 安装配置(3-1)
在第二部分中,我向您展示了如何在Windows Server 2012 R2 for SharePoint 2013上设置Active Directory域服务.现在我们应该能够在Active Dir ...
- redmine安装详解
1.Linux:centos6.4(32位)2.Gcc的编译环境.使用make命令编辑.yum install gcc-c++ 3.PCRE PCRE(Perl Compatible Regular ...
- 安装新版REDIS
http://redis.io/ # wget http://download.redis.io/redis-stable.tar.gz tar zxvf redis-stable.tar.gz -C ...
- python——动态类型
1.变量,对象,引用 变量是一个系统表的元素,拥有指向对象的连接的空间: 对象是分配的一块内存: 引用是自动形成的从变量到对象的指针: 类型属于对象,而不是变量 a=3; 变量a变成对象3的一个引用 ...
- MovieReview—Despicable Me 3(神偷奶爸3)
Minions&Unicorn The film focuses on the story of Grew and the bastard Bled. A variety of ...
- Python-OpenCV中的cv2.inpaint()函数
概念 修复是图像插值.数字修复算法在图像插值,照片恢复,缩放和超分辨率等方面具有广泛的应用. 大多数人会在家里放一些旧的退化照片,上面有一些黑点,一些笔画等.你有没有想过恢复它?我们不能简单地在绘画工 ...