uva10012(圆)

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




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值