UVA - 10012

  How Big Is It? 

Ian's going to California, and he has to pack his things, including his collection of circles. Given a set of circles, your program must find the smallest rectangular box in which they fit. All circles must touch the bottom of the box. The figure below shows an acceptable packing for a set of circles (although this may not be the optimal packing for these particular circles). Note that in an ideal 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 nn<=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 circles which must be packed in a single box. These numbers need not be integers.

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 can pack the circles. 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.

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

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <cmath>
 6 using namespace std;
 7 
 8 double a[10];
 9 int N;
10 
11 class Node {
12 public:
13     double left,mid,right;
14     bool operator < (const Node& rhs) const {
15         return left < rhs.left;
16     }
17 }visit[10];
18 
19 double work() {
20     double left = 10000000000;
21     double right = -1000000000;
22     for (int i = 0;i < N;i++) {
23         double x = 0;
24         for (int j = 0;j < i;j++) {
25             x = max(x,visit[j].mid + 2 * sqrt(a[i] * a[j]) + a[i]);
26         }
27         visit[i].right = x;
28         visit[i].mid = x - a[i];
29         visit[i].left = x - 2 * a[i];
30         if (visit[i].left < left) left = visit[i].left;
31         if (visit[i].right > right) right = visit[i].right; 
32     }
33     return right - left;
34 }
35 
36 int main () {
37     int T;
38     freopen("1.in","r",stdin);
39     cin >> T;
40     while (T--) {
41         cin >> N;
42         for (int i = 0;i < N;i++) {
43             cin >> a[i];
44         }
45         double mini = 1000000000.0;
46         sort(a,a + N);
47         do {   
48             double ans = work();
49             if (ans < mini) mini = ans;
50         }while(next_permutation(a,a + N));
51         printf("%.3lf\n",mini);
52     }
53 }
View Code

 

posted @ 2014-11-29 10:13  闪光阳  阅读(151)  评论(0编辑  收藏  举报