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 ...
随机推荐
- HBase GC日志
HBase依靠ZooKeeper来感知集群成员及其存活性.假设一个server暂停了非常长时间,它将无法给ZooKeeper quorum发送心跳信息,其他server会觉得这台server已死亡.这 ...
- 简单label控件 自制
using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using ...
- SAM4E单片机之旅——7、LED闪烁之TC中断
RTT主要用做一个全局的定时器,而且不太通用.现在尝试使用一个更为通用的定时器进行定时:定时计数器(Timer Counter, TC). TC提供了广泛的功能,主要可以分为对输入的测量,以及波形的输 ...
- 拜托,面试请不要再问我TCC分布式事务的实现原理!(转)
一.写在前面 之前网上看到很多写分布式事务的文章,不过大多都是将分布式事务各种技术方案简单介绍一下.很多朋友看了不少文章,还是不知道分布式事务到底怎么回事,在项目里到底如何使用. 所以咱们这篇文章,就 ...
- Machine Learning in Action(0) 开篇
现在貌似In Action很流行,各种技术抽象成工程商的Action,可以避开繁琐的内部原理,这本书从实践出发,通俗易懂的解释那些常用的机器学习算法,类似跟<集体智慧编程>.这本书中文出版 ...
- css position弹性盒子测试
总结: 1.利用样式height:100%设置div高度为全屏时候必须设置所有的父元素,但是父元素那么多,不可控,所以此法不可行: 2.设置父框架的padding为100px,div进行float,p ...
- mysql学习笔记(七)—— MySQL内连接和外连接
MySQL内连接(inner join on) MySQL的内连接使用inner join on,它的效果跟使用where是一样的,如果联结的是两个表,那么需要左右的条件或者说字段是需要完全匹 ...
- html5--3.7 input元素(6)
html5--3.7 input元素(6) 学习要点 input元素及其属性 input元素 用来设置表单中的内容项,比如输入内容的文本框,按钮等 不仅可以布置在表单中,也可以在表单之外的元素使用 i ...
- Python:循环
循环语句:while循环,for循环 例1:求1-100的所有数的和 n = 100 sum = 0 count = 1 while count <= n: sum = sum + count ...
- tcpdump 探测器分析
注:默认情况下,tcpdump临听它遇见的第一个网络接口,如果它选择了错误的接口,可以-i标志强行指定接口,如果DNS不能用,或者只是不希望tcpdump进行名字查找,请使用-n选项,这个选项(-n) ...