UVA 1354 - Mobile Computing(暴力枚举)

There is a mysterious planet called Yaen, whose space is 2-dimensional. There are many beautiful stones on the planet, and the Yaen people love to collect them. They bring the stones back home and make nice mobile arts of them to decorate their 2-dimensional living rooms.

In their 2-dimensional world, a mobile is defined recursively as follows:

  • a stone hung by a string, or
  • a rod of length 1 with two sub-mobiles at both ends; the rod is hung by a string at the center of gravity of sub-mobiles. When the weights of the sub-mobiles are n and m , and their distances from the center of gravity are a and b respectively, the equation n x a = m x b holds.

For example, if you got three stones with weights 1, 1, and 2, here are some possible mobiles and their widths:

Given the weights of stones and the width of the room, your task is to design the widest possible mobile satisfying both of the following conditions.

  • It uses all the stones.
  • Its width is less than the width of the room.

You should ignore the widths of stones. In some cases two sub-mobiles hung from both ends of a rod might overlap (see the figure on the right). Such mobiles are acceptable. The width of the example is (1/3) + 1 + (1/4) .

Input 

The first line of the input gives the number of datasets. Then the specified number of datasets follow. A dataset has the following format.


r

s

w1

ws


r is a decimal fraction representing the width of the room, which satisfies 0 < r < 10 . s is the number of the stones. You may assume 1s6 . wi is the weight of the i -th stone, which is an integer. You may assume 1wi1000 .

You can assume that no mobiles whose widths are between r - 0.00001 and r + 0.00001 can be made of given stones.

Output 

For each dataset in the input, one line containing a decimal fraction should be output. The decimal fraction should give the width of the widest possible mobile as defined above. An output line should not contain extra characters such as spaces.

In case there is no mobile which satisfies the requirement, answer `-1' instead.

The answer should not have an error greater than 0.00000001. You may output any numb er of digits after the decimal point, provided that the ab ove accuracy condition is satisfied.

Sample Input 

5 
1.3 
3 
1 
2 
1 
1.4 
3 
1 
2 
1 
2.0 
3 
1 
2 
1 
1.59 
4 
2 
1 
1 
3 
1.7143 
4 
1 
2 
3 
5

Sample Output 

-1 
1.3333333333333335 
1.6666666666666667 
1.5833333333333335 
1.7142857142857142

题意:一个房间宽是r,现在你有一些砝码,要制造一个天平,天平宽度不能超过房间宽度,要求制造出来的天平尽量宽。

思路:暴力枚举,枚举所有情况去判断即可,枚举的过程中利用位运算的集合表示方法,去递归出左集合和右集合的情况,在由这些情况去得到根节点的情况,然后要加一步记忆化,就是如果该状态s已经找出过了,下次就不用在去找一遍了。

代码:

#include <stdio.h>
#include <string.h>
#include <vector>
#define INF 0x3f3f3f3f
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
using namespace std;
const int N = 6;
const int MAXN = (1<<N);
int t, n, i, j, vis[MAXN];
double w[N], sumw[MAXN], r;
struct Node {
	double l, r;
	Node() {}
	Node(double ll, double rr) {l = ll; r = rr;}
};
vector<Node> node[MAXN];

int bitcount(int x) {
	if (x == 0) return 0;
	return bitcount(x/2) + (x&1);
}

void dfs(int s) {
	if (vis[s]) return;
	vis[s] = 1;
	if (bitcount(s) == 1) {
		node[s].push_back(Node(0, 0));
		return;
	}
	for (int l = (s - 1)&s ; l > 0; l = (l - 1)&s) {
		int r = s^l;
		dfs(l); dfs(r);
		for (int i = 0; i < node[l].size(); i++) {
			for (int j = 0; j < node[r].size(); j++) {
				double ll = min(-sumw[r] / (sumw[l] + sumw[r]) + node[l][i].l, sumw[l] / (sumw[l] + sumw[r]) + node[r][j].l);
				double rr = max(sumw[l] / (sumw[l] + sumw[r]) + node[r][j].r, -sumw[r] / (sumw[l] + sumw[r]) + node[l][i].r);
				node[s].push_back(Node(ll, rr));
			}
		}
	}	
}

void solve() {
	double ans = -1;
	int s = (1<<n) - 1;
	dfs(s);
	for (int i = 0; i < node[s].size(); i++) {
		if (node[s][i].r - node[s][i].l < r) {
			if (node[s][i].r - node[s][i].l > ans)
				ans = node[s][i].r - node[s][i].l;
		}
	}
	if (ans == -1) printf("-1\n");
	else printf("%.10lf\n", ans);
}

int main() {
	scanf("%d", &t);
	while (t--) {
		memset(vis, 0, sizeof(vis));
		memset(node, 0, sizeof(node));
		scanf("%lf%d", &r, &n);
		for (i = 0; i < n; i++)
			scanf("%lf", &w[i]);
		for (i = 0; i < (1<<n); i++) {
			sumw[i] = 0;
			for (j = 0; j < n; j++) {
				if (i&(1<<j))
					sumw[i] += w[j];
			}
		}
		solve();
	}
	return 0;
}


你可能感兴趣的:(UVA 1354 - Mobile Computing(暴力枚举))