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
来源/分类
给你一堆圆,问排成一排的最小长度是多少,,
显然,如果圆直径相差不大的话,直接排序就好,然而如果相差很大,
那小圆就可以放在大圆的空隙里,计算的话就很不好想
如果相差不大的话,稍微推一下可以得到两圆之间的水平距离是sqrt((r+R)^2-(R-r)^2),然后化简下得到2*sqrt(R*r)
这样,每个圆都暴力之前的所有圆求当前位置最远的时候恰好使得长度最小,
那么当前状态由之前的所有状态转移过来
我觉得有必要讲一下这个是为什么考虑放的最远
首先假设之前放了i-1个圆了,那么考虑第i个圆的时候,
这个圆肯定是和前面某个圆相切的,否则要是都不相切,那说明有缝隙,肯定不是最优的
那么和哪一个相切呢,肯定是能将当前圆放的位置最远的那个,肯定的
和那一个相切能放的最远,你要和别的相切,就会交叉了
太菜了,这题队友给讲的
#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;
}