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

【链接】 我是链接,点我呀:)
【题意】

在这里输入题意

【题解】

秤砣都是在叶子节点。 可以把它看成一个二叉树。 则我们每次只需要选择任意两个"节点",让他们组成一棵二叉树就可以了。 然后虚拟出来一个节点,代表这个子树的根节点。 每次维护一下每个子树的左子树最左端离树的中心距离以及最右端离树的中心的距离即可。 虚拟生成的节点作为一个新的节点动态加入即可。(它的子孙节点们不再可用) (这样的搜索策略,可以保证所给节点最后都在叶子节点处) (注意不一定新的虚拟节点的最右端就由右子树得到。因为可能相交,所以也可能是左子树的某个

【代码】

/*
  	1.Shoud it use long long ?
  	2.Have you ever test several sample(at least therr) yourself?
  	3.Can you promise that the solution is right? At least,the main ideal
  	4.use the puts("") or putchar() or printf and such things?
  	5.init the used array or any value?
  	6.use error MAX_VALUE?
  	7.use scanf instead of cin/cout?
  	8.whatch out the detail input require
*/
#include <bits/stdc++.h>
using namespace std;

const int N = 30;

struct node{
  	double x,y;
  	int weight;
  	node(double X,double Y,int Weight){x = X;y = Y;weight = Weight;}
};

vector <node> v;
bool bo[N];
int n;
double r,ans = -1;


void dfs(int dep){
	if (dep==n){
		double width = v.back().x+v.back().y;
		if (width > r) return;
		ans = max(ans,width);	 	
		return;
	}
 	for (int i = 0;i < (int)v.size();i++)
 		if (!bo[i]){
     	 	for (int j = 0;j < (int) v.size();j++)
     	 		if (i!=j && !bo[j]){
					bo[i] = bo[j] = true;
					double wl = v[i].weight,wr = v[j].weight; 	 	
					double x,y;
					//x*wl == y*wr
					//x+y==1
					//x = 1-y
					//wl-y*wl == y*wr
					//y*(wl+wr)==wl
					//y = wl/(wl+wr)
					y = wl/(wl+wr);
					x = 1- y;
					v.push_back(node(max(x+v[i].x,v[j].x-y),max(v[j].y+y,v[i].y-x),wl+wr));
					
					dfs(dep+1);

					bo[i] = bo[j] = false;
					v.pop_back();
    	 	 	}
	 	}
}

int main(){
	#ifdef LOCAL_DEFINE
	    freopen("F:\\c++source\\rush_in.txt", "r", stdin);
	#endif
	ios::sync_with_stdio(0),cin.tie(0);
	int T;
	cin >> T;
	while (T--){
		v.clear();
		cin >> r;
		cin >> n;
		for (int i = 0;i < n;i++){
			int x;
			cin >> x;
			v.push_back(node(0,0,x));
		}
		memset(bo,0,sizeof bo);
		ans = -1;
		dfs(1);
		if (ans<0){
		 	cout <<"-1"<<endl;
		}else{
		 	cout << fixed << setprecision(10) << ans << endl;
		}
	}
	return 0;
}
posted @ 2017-12-06 20:19  AWCXV  阅读(254)  评论(0编辑  收藏  举报