uva 10012 How Big Is It?(枚举)

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 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 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

题目大意:给出一些圆的 半径,求出可以装下这下这些圆的矩形箱子的最小尺寸(宽度)。圆都要与矩形底边相切。

解题思路:枚举所有情况,主要是将圆的半径转化为实际x坐标。

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
using namespace std;
double p[10], r[10], ans, temp;
int n;
void getPos(int id)
{
	double x;
	for (int i = 0; i < id; ++i)
	{
		x = sqrt((r[i] + r[id]) * (r[i] + r[id]) - (r[i] - r[id]) * (r[i] - r[id])) + p[i];
		p[id] = max(p[id], x);
	}
}
int main()
{
	int T;
	scanf("%d", &T);
	while (T--)
	{
		scanf("%d", &n);
		for (int i = 0; i < n; i++) scanf("%lf", &r[i]);
		sort(r, r + n);
		ans = 0xFFFFFFFF;
		do
		{
			memcpy(p, r, sizeof(p));
			for (int i = 1; i < n; i++) {
				getPos(i); //计算该圆圆心的x轴坐标
			}
			temp = 0.0;
			for (int i = 0; i < n; i++) {
				temp = max(temp, r[i] + p[i]);
			}
			ans = min(ans, temp);
		}
		while (next_permutation(r, r + n)); //枚举不同排列顺序
		printf("%.3f\n", ans);
	}
	return 0;
}
时间: 2024-10-14 20:14:05

uva 10012 How Big Is It?(枚举)的相关文章

【UVA】11464-Even Parity(二进制枚举子集)

枚举第一行的所有可能情况,之后根据上面行计算下面行(判断是否冲突),获得最终结果. 14058243 11464 Even Parity Accepted C++ 0.275 2014-08-18 05:14:15 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> #include<stack> #inc

UVA - 11374 Airport Express (Dijkstra模板+枚举)

Description Problem D: Airport Express In a small city called Iokh, a train service, Airport-Express, takes residents to the airport more quickly than other transports. There are two types of trains in Airport-Express, the Economy-Xpress and the Comm

uva 140 Bandwidth (全排列+暴力枚举)

uva 140 Bandwidth Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and an ordering on the elements in V, then the bandwidth of a node v is defined as the maximum distance in the ordering between v and any node to which it

uva 10167 Birthday Cake(暴力/枚举)

uva 10167 Birthday Cake Background Lucy and Lily are twins. Today is their birthday. Mother buys a birthday cake for them.Now we put the cake onto a Descartes coordinate. Its center is at (0,0), and the cake's length of radius is 100. There are 2N (N

UVa 1354 Mobile Computing[暴力枚举]

**1354 Mobile Computing** There is a mysterious planet called Yaen, whose space is 2-dimensional. There are many beautiful stones on the planet, and the Yaen people love to collect them. They bring the stones back home and make nice mobile arts of th

UVa 10012 有多大 没AC,待修改

题意:给出一些圆的半径,把所有圆放到一个矩形里,要求所有圆都必须与矩形的最下边相切,求矩形的最小长度. 本来写得很快,以为是一道水题,结果有太多情况没考虑..我是按照最左圆的半径加上每两相切圆的圆心间水平距离再加上最右圆的半径写的,有太多情况没考虑.一会补上一个,缝缝补补的,现在都有些晕了,现在还遗漏的情况是,我只考虑了第二个圆比第一个圆能到更左,以及倒数第二个圆比倒数第一个圆能到更右,但是第三个圆或第四个圆也可能比它左边的圆更能到达左边的,右边类似..这就像,只补上了i-2号圆与i号圆相切,而

UVa 11025 The broken pedometer【枚举子集】

题意:给出一个矩阵,这个矩阵由n个数的二进制表示,p表示用p位二进制来表示的一个数 问最少用多少列就能将这n个数区分开 枚举子集,然后统计每一种子集用了多少列,维护一个最小值 b[i]==1代表的是选择了这一列 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #include<vector&g

UVA - 10167 - Birthday Cake (简单枚举)

思路:简单枚举 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> using namespace std; int x[105], y[105]; int main() { int A, B, N; while(scanf("%d", &N), N) { for(int

UVA - 1619 Feel Good 标记+枚举

题目大意:给出n个正整数的序列,要求你找出符合sum(a1 + a2 + a3 + - + an) * min(a1,a2,-,an)的最大值,如果有多个符合条件的,输出最短序列 解题思路:从输出中观察得到,输出的答案要有三个,最大值,左端点,右端点. 那就设两个数组,纪录已当前数位最小值所能覆盖的最大区间,然后枚举每个数,求出区间和再乘上当前数求得值再进行比较 设置左右区间时,可以递归比较,假设l[i]纪录的时ai所能覆盖的最左端点,如果aj <= ai (j > i),那么l[j] = l