How Big Is It?
Ian's going to California, and he has to pack his things, including hiscollection of circles. Given a set of circles, your program mustfind the smallest rectangular box in which they fit.All circles must touch the bottom of the box. The figure below showsan acceptable packing for a set of circles (although this may not bethe optimal packing for these particular circles). Note that in anideal packing, each circle should touch at least one other circle(but you probably figured that out).
How Big Is It? |
![](http://uva.onlinejudge.org/external/100/p10012.gif)
Input
The first line of input contains a single positive decimal integer n, n<=50. This indicates the number of lines which follow. Thesubsequent n lines each contain a series of numbers separated byspaces. The first number on each of these lines is a positive integer m, m<=8, which indicates how many other numbers appear on thatline. The next m numbers on the line are the radii of the circleswhich must be packed in a single box. These numbers need not beintegers.Output
For each data line of input, excluding the first line of input containing n, your program must output the size of the smallest rectangle which canpack the circles. Each case should be output on a separate line by itself,with three places after the decimal point. Do not output leading zeroesunless the number is less than 1, e.g. 0.543
.
Sample Input
3 3 2.0 1.0 2.0 4 2.0 2.0 2.0 2.0 3 2.0 1.0 4.0
Sample Output
9.657 16.000 12.657
刚开始读错题了,想了半天没想出来。。。
题意:是把圆放在水平线上
思路:详见代码
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
double r[10], M, sum;
int n, t;
double cdis(double a, double b)
{
return 2 * sqrt(a * b);//(r1+r2)^2-(r1-r2)^2两个圆心之间的距离
}
double pdis(double x, double y)
{
return sqrt(x * x + y * y);
}
double check()
{
int i, j;
double x[10], y[10], left = 0;
for (i = 0; i < t; i++)
{
y[i] = r[i];
if (i == 0)
x[i] = r[i];
else
x[i] = x[i - 1] + cdis(r[i], r[i - 1]);//每个圆的初始位置是与前一个圆相切
if (x[i] < r[i])//处理左边界
x[i] = r[i];
for (j = 0; j < i; j++)//处理重叠的圆
if (r[i] + r[j] > pdis(x[i] - x[j], y[i] - y[j]))
x[i] = x[j] + cdis(r[i], r[j]);
if (left < x[i] + r[i])
left = x[i] + r[i];
}
return left;
}
int main()
{
scanf("%d", &n);
while (n--)
{
M = 1000000000;
scanf("%d", &t);
for (int i = 0; i < t; i++)
scanf("%lf", &r[i]);
check();
sort (r, r + t);
do
{
sum = check();
if (sum < M)
M = sum;
}
while (next_permutation(r, r + t));
printf("%.3lf\n", M);
}
return 0;
}