首页 > 其他 > 详细

[2016-02-22][UVA][1354][Mobile Computing]

时间:2016-02-22 20:44:55      阅读:367      评论:0      收藏:0      [点我收藏+]
[2016-02-22][UVA][1354][Mobile Computing]

技术分享
  • 时间:2016-02-22 17:09:41 星期一
  • 题目编号:UVA 1354
  • 题目大意:
    • 给定若干个物品的重量,把物品放在天平上(天平一端可以是天平),求所有可能天平中,最大的天平长度
    • 要求:   天平长度 不能 超过指定长度
    • 每个天平总长为1,
  • 输入:
    • 样例数目cntcase
    • 每组样例:
    • r  s  w1 ... ws
  • 输出:最大天平长度,不存在输出-1
  • 分析:
    • 求所有情况的最大值,那么,枚举所有的情况(枚举每个物品的位置),求最大值
  • 方法:
    • 枚举二叉树方法:每次选择两个节点当做字数,递归s-1层
    • 计算长度:递归过程保存当前左端的长度和右短的程度,递归一层,更新一次
  • 解题过程遇到问题:
    • 特殊情况:右边节点的左子树的长度可能大于,左边节点的长度,即右子树的枝条可能超过左子树最左端
    • 写代码的过程中,忘记考虑房间的宽度(r),o(︶︿︶)o 
    • 下面这种方法,如果重量一样的话,会重复枚举,  

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef long long LL;
#define CLR(x,y) memset((x),(y),sizeof((x)))
#define FOR(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define FORD(x,y,z) for(int (x)=(y);(x)>=(z);(x)--)
#define FOR2(x,y,z) for((x)=(y);(x)<(z);(x)++)
#define FORD2(x,y,z) for((x)=(y);(x)>=(z);(x)--)
const int maxs = 10;
int s,vis[maxs];
double r,maxres,w[maxs],lenl[maxs],lenr[maxs];
int qhy(int cur){
        if(cur == s - 1){//枚举完成,
                FOR(i,0,s){
                        if(!vis[i]){//剩下那个节点就是
                                //更新信息
                                double tmp = lenl[i] + lenr[i];
                                if(tmp > maxres && r > tmp)  maxres = tmp;
                                return 0;
                        }
                }
        }
        //枚举两个节点
        FOR(n,0,s){
                if(vis[n])      continue;
                FOR(m,0,s){
                        if(n != m && !vis[m]){
                                //合并两个节点,并把合并后的点,保存在m对应的位置
                                vis[n] = 1;
                                double tmp = w[m],a = w[m] / (w[n] + w[m]),b = 1. - a;
                                w[m] = w[n] + w[m];
                                double tmpl = lenl[m],tmpr = lenr[m];
                                lenl[m] = max(lenl[n] + a,lenl[m] - b);
                                lenr[m] = max(lenr[m] + b,lenr[n] - a);
                                qhy(cur + 1);
                                //恢复m点
                                lenl[m] = tmpl;
                                lenr[m] = tmpr;
                                w[m] = tmp;
                                vis[n] = 0;
                        }
                
                }
        }
        return 0;
}
int main(){
        int cntcase;
        scanf("%d",&cntcase);
        while(cntcase--){
                scanf("%lf%d",&r,&s);
                FOR(i,0,s){
                        scanf("%lf",&w[i]);
                }
                //初始化
                CLR(vis,0);CLR(lenl,0);CLR(lenr,0);maxres = -1.0;
                qhy(0);
                printf("%.14f\n",maxres);
        }
    return 0;
}  











附件列表

     

    [2016-02-22][UVA][1354][Mobile Computing]

    原文:http://www.cnblogs.com/qhy285571052/p/5207992.html

    (0)
    (0)
       
    举报
    评论 一句话评论(0
    关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
    © 2014 bubuko.com 版权所有
    打开技术之扣,分享程序人生!