hoj2798 Globulous Gumdrops
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的更多相关文章
- 学习笔记:状态压缩DP
我们知道,用DP解决一个问题的时候很重要的一环就是状态的表示,一般来说,一个数组即可保存状态.但是有这样的一些题 目,它们具有DP问题的特性,但是状态中所包含的信息过多,如果要用数组来保存状态的话需要 ...
- 智课雅思词汇---二十五、形容词后缀-ate-fic-ose-ulent-olent-ous-ulous-y
智课雅思词汇---二十五.形容词后缀-ate-fic-ose-ulent-olent-ous-ulous-y 一.总结 一句话总结: 1.形容词后缀-ate(determinate)? determi ...
随机推荐
- sort_action
li, r = [23,8, 45, 5, 0, -6, 745,8, 8], [] while (len(li) > 0): loop_, min, tag = len(li), li[0], ...
- the algebra of modulo-2 sums disk failure recovery
x=y x_+_y=0 The bit in any position is the modulo-2 sum of all the bits in the corresponding positio ...
- Android笔记之dp与px之间的转换以及LayoutParams
dp与px之间的转换公式 px = dp * (dpi / 160) dp = px / (dpi / 160) 其中dpi的获取方式如下 private void getDpi() { Displa ...
- Mac 操作系统安装 SVN server教程(Subversion With Mac OS X Tutorial)
Find recent articles on my github page: rubyrobot.github.io © 2006-2014 Imagine Ecommerce Subversion ...
- sdut oj 排队买饭
数据结构实验之队列一:排队买饭 Time Limit: 1000MS Memory limit: 65536K 题目描述 中午买饭的人特多,食堂真是太拥挤了,买个饭费劲,理工大的小孩还是很聪明的,直接 ...
- html5--5-11 绘制文字
html5--5-11 绘制文字 学习要点 掌握文字的绘制方法 文字的绘制方法 strokeText("文字",x,y,maxWith) 绘制(描边)空心文字 fillText(& ...
- html5--3.17 新增的表单重写
html5--3.17 新增的表单重写 学习要点 对form元素的属性做一个小结,对个别属性进行一点补充 重点掌握新增的表单重写 form元素的属性小结 action/method/enctype/n ...
- double转int时精度不一致问题
float和double类型的主要设计目的是为了科学计算和工程计算.它们执行二进制浮点运算,这是为了在广域数值范围上提供较为精确的快速近似计算而精心设计的.然而,它们没有提供完全精确的结果,所以不应该 ...
- php数组合并
php的数合并函数: array_merge($arr1, $arr2, ..., $arr{$n}); 如果数组的键名有重复,后面的会覆盖前面的. 如果键名是数字索引,则会重新排列索引,往后累加. ...
- css3计算属性(calc)
如果有固定头部高度和底部高度,内容的高度或者宽度想要根据浏览器屏幕自适应的话,可以用到css3的计算属性,即calc. 用法如下: 内容区域高/宽 = calc(100% - 头部高宽 - 底部高宽) ...