UVa 10012 How Big Is It? 解题报告(枚举排列)

56 篇文章 0 订阅

 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


    解题报告:将m个球同时放到地上,每个球都接触地面,最短是多长。

    因为m比较小,我们可以求出所有可能的球的排列顺序,计算长度,取最小值即可。使用next_permutation求所有的排列。

    求长度的时候注意,如果前面一个球的半径是1, 后面一个球的半径是100,那么影响宽度的就是1前面的那个球了,1完全可以放在100那个球的下面。

    代码如下:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <string>
#include <iostream>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define ff(i, n) for(int i=0;i<(n);i++)
#define fff(i, n, m) for(int i=(n);i<=(m);i++)
#define dff(i, n, m) for(int i=(n);i>=(m);i--)
void work();
int main()
{
#ifdef ACM
    freopen("in.txt", "r", stdin);
#endif // ACM

    work();
}

/****************************************/

void work()
{
    int T;
    scanf("%d", &T);
    fff(cas, 1, T)
    {
        int n;
        scanf("%d", &n);

        double num[10];
        double cen[10];
        ff(i, n) scanf("%lf", num+i);
        sort(num, num+n);

        double ans = 1e20;
        do
        {
            cen[0] = num[0];
            fff(i, 1, n-1)
            {
                cen[i] = 0;
                ff(j, i)
                {
                    double c = num[i] + num[j];
                    double a = abs(num[i] - num[j]);
                    cen[i] = max(cen[i], cen[j] + sqrt(c*c - a*a));
                }
                cen[i] = max(cen[i], num[i]);
            }

            double sum = 0;
            ff(i, n) sum = max(sum, cen[i] + num[i]);
            ans = min(ans, sum);
        } while(next_permutation(num, num+n));

        printf("%.3lf\n", ans);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值