【例题 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;
}

转载于:https://www.cnblogs.com/AWCXV/p/7994601.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值