How Big Is It?行吧,学不会的dp

8024: How Big Is It?

时间限制: 1 Sec  内存限制: 128 MB
提交: 50  解决: 13
[提交] [状态] [讨论版] [命题人:admin]

题目描述

Ian's going to California, and he has to pack his things, including his collection of cirles. Given a set of cirles, your program must find the smallest rectangular box in which they fit.
All cirles must touch the bottom of the box. The figure below shows an acceptable packing for a set of cirles (although this may not be the optimal packing for these partiular cirles). Note that in an ideal packing, each cirle should touch at least one other cirle (but you probably figured that out).

输入

The first line of input contains a single positive decimal integer n, n < 50. This indicates the number of lines which follow. The subsequent n lines each contain a series of numbers separated by spaces.
The first number on each of these lines is a positive integer m, m < 8, which indicates how many other numbers appear on that line. The next m numbers on the line are the radii of the cirles which must be packed in a single box. These numbers need not be integers.

输出

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 an pack the cirles. Each case should be output on a separate line by itself, with three places after the decimal point. Do not output leading zeroes unless the number is less than 1, e.g. 0.543.

样例输入

3
3 2.0 1.0 2.0
4 2.0 2.0 2.0 2.0
3 2.0 1.0 4.0

样例输出

9.657
16.000
12.657

来源/分类

Waterloo20180616 

给你一堆圆,问排成一排的最小长度是多少,,

显然,如果圆直径相差不大的话,直接排序就好,然而如果相差很大,

那小圆就可以放在大圆的空隙里,计算的话就很不好想

如果相差不大的话,稍微推一下可以得到两圆之间的水平距离是sqrt((r+R)^2-(R-r)^2),然后化简下得到2*sqrt(R*r)

这样,每个圆和都和之前能放的最远的圆相切时恰好使得长度最小,那么当前状态由之前的所有状态转移过来

太菜了,这题队友给讲的

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
double p[50],pos[50];
double Dist(double x,double y){
    return 2*sqrt(x*y);
}
int main(){
    int t,n;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(int i=0;i<n;i++)scanf("%lf",&p[i]);
        double ans = 1e10;
        sort(p,p+n);///sort下,因为next函数
        do{
            double tans = p[0]*2;///初始值为第一个圆的直径
            pos[0] = p[0];///第一个圆的位置
            for(int i=1;i<n;i++){
                double dis = p[i];
                for(int j=i-1;j+1;j--)///找摆放最远的圆
                    dis = max(dis,pos[j]+Dist(p[i],p[j]));
                pos[i] = dis;///更新当前圆位置
                tans = max(tans,dis+p[i]);///本次排列的最小长度是最大的当前圆位置+当前圆的半径
            }
            ans = min(ans,tans);///更新最小的答案
        }while(next_permutation(p,p+n));///枚举每一种排列
        printf("%.3f\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Du_Mingm/article/details/84030978
今日推荐