Globulous Gumdrops

My Tags   (Edit)
  Source : 2008 Stanford Programming Contest
  Time limit : 1 sec   Memory limit : 64 M

Submitted : 174, Accepted : 94

Description

Gwen just bought a bag of gumdrops! However, she does not like carrying gumdrops in plastic bags; instead, she wants to pack her gumdrops in a cylindrical tube of diameter d. Given that each of her gumdrops are perfect spheres of radii r1, r2, . . . , rn, find the shortest length tube Gwen can use to store her gumdrops.

You should assume that the gumdrop radii are sufficiently large that no three gumdrops can be simultaneously in contact with each other while fitting in the tube. Given this restriction, it may be helpful to realize that the gumdrops will always be packed in such a way that their centers lie on a single two-dimensional plane containing the axis of rotation of the tube.

Input

The input file will contain multiple test cases. Each test case will consist of two lines. The first line of each test case contains an integer n (1 <= n <= 15) indicating the number of gumdrops Gloria has, and a floating point value d (2.0 <= d <= 1000.0) indicating the diameter of the cylindrical tube, separated by a space. The second line of each test case contains a sequence of n space-separated floating point numbers, r1 r2 . . . rn (1.0 <= ri <= d/2) are the radii of the gum drops in Gloria's bag. A blank line separates input test cases. A single line with the numbers "0 0" marks the end of input; do not process this case.

Output

For each input test case, print the length of the shortest tube, rounded to the nearest integer.

Sample Input

2 98.1789
42.8602 28.7622
3 747.702
339.687 191.953 330.811
0 0

Sample Output

138
1628
/*
可以用dp[state][i]表示所用球状态为state,且最上面一个是第i个球,要放入这些球所需要的最大高度,那么每次加入一个球就可以转移了,状态转移方程为dp[state1][j]=min(dp[state1 ][j],jisuan(dp[state][i],i,j) );state1=state|( 1<<(j-1) );
*/
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<cstdio>
#include<cstring>
using namespace std;
double r[],d,dp[<<][];
int n;
double count(double pre,int now,int top){
double x=r[top]+r[now];
double y=d-r[top]-r[now];
return pre+r[top]-r[now]+sqrt(x*x-y*y);
}
int main(){
while(scanf("%d%lf",&n,&d)!=EOF){
if(n==&&d==)return ;
for(int i=;i<=n;i++)scanf("%lf",&r[i]);
//memset(dp,127/3,sizeof(dp));
for(int i=;i<(<<n);i++){
for(int j=;j<=n;j++){
dp[i][j]=0x7fffffff;
}
}
for(int i=;i<=n;i++)dp[<<(i-)][i]=r[i]*;
for(int state=;state<(<<n);state++){
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(state&(<<(j-)))continue;
int state1=state|(<<(j-));
//cout<<count(dp[state][i],i,j)<<endl;
dp[state1][j]=min(dp[state1][j],count(dp[state][i],i,j));
}
}
}
double ans=0x7fffffff;
for(int i=;i<=n;i++){
ans=min(ans,dp[(<<n)-][i]);
}
int Ans=(int)(ans+0.5);
printf("%d\n",Ans);
}
}

hoj2798 Globulous Gumdrops的更多相关文章

  1. 学习笔记:状态压缩DP

    我们知道,用DP解决一个问题的时候很重要的一环就是状态的表示,一般来说,一个数组即可保存状态.但是有这样的一些题 目,它们具有DP问题的特性,但是状态中所包含的信息过多,如果要用数组来保存状态的话需要 ...

  2. 智课雅思词汇---二十五、形容词后缀-ate-fic-ose-ulent-olent-ous-ulous-y

    智课雅思词汇---二十五.形容词后缀-ate-fic-ose-ulent-olent-ous-ulous-y 一.总结 一句话总结: 1.形容词后缀-ate(determinate)? determi ...

随机推荐

  1. 51NOD 1962 区间计数 单调栈+二分 / 线段树+扫描线

     区间计数   基准时间限制:1.5 秒 空间限制:262144 KB 分值: 80   两个数列 {An} , {Bn} ,请求出Ans, Ans定义如下: Ans:=Σni=1Σnj=i[max{ ...

  2. Geoffrey E. Hinton

    https://www.cs.toronto.edu/~hinton/ Geoffrey E. Hinton I am an Engineering Fellow at Google where I ...

  3. Mixtures of Gaussians and the EM algorithm

    http://cs229.stanford.edu/ http://cs229.stanford.edu/notes/cs229-notes7b.pdf

  4. docker: docker安装和镜像下载

    1 安装docker的apt源 apt-get install apt-transport-https ca-certificates curl software-properties-common ...

  5. protoc: error while loading shared libraries: libprotoc.so.9: cannot open shared object

    使用protobuf出错:protoc: error while loading shared libraries: libprotoc.so.9: cannot open shared object ...

  6. 在win7系统下安装把Ubuntu17.04安装在另一个硬盘开机无法进入Ubuntu问题的一种解决办法。【转】

    本文转载自:http://blog.csdn.net/u012879090/article/details/74937762 在win7系统下安装把Ubuntu17.04安装在另一个硬盘开机无法进入U ...

  7. git多人协作冲突解决方法

    http://www.trinea.cn/dev-tools/git-skill/ http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361 ...

  8. Java接口测试之使用有道翻译API

    写接口测试框架,找了有道翻译API来当测试数据 package com.httpGetTest; import java.beans.Encoder; import java.net.URLEncod ...

  9. 从数据的角度带你深入了解IPFS

    IPFS 和区块链有着非常紧密的联系, 随着区块链的不断发展,对数据的存储需求也越来越高.本文从IPFS 的底层设计出发, 结合源代码, 分析了IPFS 的一些技术细节. 一.概述 IPFS 和区块链 ...

  10. syslog格式

    转自:http://wly719.iteye.com/blog/1827394 1.syslog格式介绍 在Unix类操作系统上,syslog广泛 应用于系统日志.syslog日志消息既可以记录在本地 ...