uva10012(圆)

来源:互联网 发布:阿尔法淘宝宝贝下载 编辑:程序博客网 时间:2024/05/16 15:38

 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).

Input 

The first line of input contains a single positive decimal integer n,n<=50. This indicates the number of lines which follow. Thesubsequentn lines each contain a series of numbers separated byspaces. The first number on each of these lines is a positive integerm,m<=8, which indicates how many other numbers appear on thatline. The nextm 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 containingn, 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 

33 2.0 1.0 2.04 2.0 2.0 2.0 2.03 2.0 1.0 4.0

Sample Output 

9.65716.00012.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;}




0 0
原创粉丝点击