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. 互联网时代的精准招聘-Uber新手游有感

    找工作难.招人也难.漫天的简历,全是求职者广撒网式的复制粘贴,如何找到合适的人.会认真对待职位的人?或许你须要换换思路,看看Uber新出的手机游戏能够咱啥启发. Uber在过去5年已经蹭蹭成长为估值5 ...

  2. php 批量删除数据

    php 批量删除数据 :比如我们在看邮箱文件的时候,积攒了一段时间以后,看到有些文件没有用了 这时候我们就会想到把这些 没用的文件删除,这时候就用到了批量删除数据的功能,这里我是用了数据库原有的一个表 ...

  3. [Phoenix] 二、数据类型

    目前Phoenix支持24种简单数据类型和1个一维Array的复杂类型.以下是对支持数据类型的说明: 1. INTEGER 2. UNSIGNED_INT 3. BIGINT 4. UNSIGNED_ ...

  4. kong

    https://docs.konghq.com/install/centos/ wget https://bintray.com/kong/kong-community-edition-rpm/dow ...

  5. JavaScript精粹

    序:好书是需要不断品读的!再读语言精粹,顺便做点笔记. 1.NaN是一个数值,它表示一个不能产生正常结果的运算结果.NaN不等于任何值,包括它自己.检测是否为NaN:  isNaN(number). ...

  6. 杭电 2553 N皇后问题

    http://acm.hdu.edu.cn/showproblem.php?pid=2553 N皇后问题 Time Limit: 2000/1000 MS (Java/Others)    Memor ...

  7. Django中如何实现数据库路由?

    虽然我们提供了数据库的信息,它知道怎么连接数据库,但问题是我们保存里面有很多模型,它不知道哪个模型存到哪个数据库.这就要求我们自己来指定,也就是我们自己来实现一个数据库路由.一个数据库路由是一个拥有4 ...

  8. ThinkPHP RBAC权限管理机制

    RBAC是ThinkPHP很好用的后台权限管理的,话不多说,实现方法如下,也方便以后自己查询使用: 1.新建4个数据库表 self_role权限表 CREATE TABLE `self_role` ( ...

  9. user版本如何永久性开启adb 的root权限【转】

    本文转载自:http://blog.csdn.net/o0daxu0o/article/details/52933926 [Solution]* adb 的root 权限是在system/core/a ...

  10. Facebook图片存储系统Haystack——存小文件,本质上是将多个小文件合并为一个大文件来降低io次数,meta data里存偏移量

    转自:http://yanyiwu.com/work/2015/01/04/Haystack.html 一篇14页的论文Facebook-Haystack, 看完之后我的印象里就四句话: 因为[传统文 ...