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<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 99999999
#define pi acos(-1.0)
double dp[1<<16][20],r[20],d;
double jisuan(double len,int i,int j)
{
double t1=r[j]+r[i],t2=d-r[i]-r[j];
return r[j]+len-r[i]+sqrt( t1*t1-t2*t2 );
}
int main()
{
int n,m,i,j,state,state1,len1;
while(scanf("%d%lf",&n,&d)!=EOF)
{
if(n==0 && d==0)break;
for(i=1;i<=n;i++){
scanf("%lf",&r[i]);
}
for(state=1;state<=( ( 1<<n )-1);state++){
for(i=1;i<=n;i++){
dp[state][i]=inf;
}
}
for(i=1;i<=n;i++){
dp[1<<(i-1)][i]=2*r[i];
}
for(state=1;state<=( ( 1<<n )-1);state++){
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
if( (state&(1<<(j-1)) )==0 ){
state1=state|( 1<<(j-1) );
dp[state1][j]=min(dp[state1 ][j],jisuan(dp[state][i],i,j) );
}
}
}
}
double len=inf;
for(i=1;i<=n;i++){
len=min(len,dp[(1<<n)-1][i]);
}
len1=(int)(len+0.5);
printf("%d
",len1);
}
return 0;
}