POJ 3301 Texas Trip

题目链接:http://poj.org/problem?id=3301

Texas Trip

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4935 Accepted: 1543

Description

After a day trip with his friend Dick, Harry noticed a strange pattern of tiny holes in the door of his SUV. The local American Tire store sells fiberglass patching material only in square sheets. What is the smallest patch that
Harry needs to fix his door?

Assume that the holes are points on the integer lattice in the plane. Your job is to find the area of the smallest square that will cover all the holes.

Input

The first line of input contains a single integer
T expressed in decimal with no leading zeroes, denoting the number of test cases to follow. The subsequent lines of input describe the test cases.

Each test case begins with a single line, containing a single integer
n expressed in decimal with no leading zeroes, the number of points to follow; each of the following
n lines contains two integers x and y, both expressed in decimal with no leading zeroes, giving the coordinates of one of your points.

You are guaranteed that T ≤ 30 and that no data set contains more than 30 points. All points in each data set will be no more than 500 units away from (0,0).

Output

Print, on a single line with two decimal places of precision, the area of the smallest square containing all of your points.

Sample Input

2
4
-1 -1
1 -1
1 1
-1 1
4
10 1
10 -1
-10 1
-10 -1

Sample Output

4.00
242.00

Source

Waterloo Local Contest, 2007.7.14

思路:单峰函数求极值问题。三分角度[0,90],求出最优解。

直接附上AC代码:

#include <cstdio>
#include <cmath>
#include <algorithm>
//#pragma comment(linker, "/STACK:102400000, 102400000")
using namespace std;
const int maxn = 35;
const double eps = 1e-8;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
struct node{
	double x, y;
} p[maxn];
int n;

double filter(double x){
	double maxx=-inf, minx=inf, maxy=-inf, miny=inf;
	for (int i=0; i<n; ++i){
		maxx = max(maxx, p[i].x*cos(x)-p[i].y*sin(x));
		maxy = max(maxy, p[i].x*sin(x)+p[i].y*cos(x));
		miny = min(miny, p[i].x*sin(x)+p[i].y*cos(x));
		minx = min(minx, p[i].x*cos(x)-p[i].y*sin(x));
	}
	return max(maxx-minx, maxy-miny);
}

int main(){
	#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	int T;
	scanf("%d", &T);
	while (T--){
		scanf("%d", &n);
		for (int i=0; i<n; ++i)
			scanf("%lf%lf", &p[i].x, &p[i].y);
		double l=0.0, r=pi/2.0, mid, tri;
		while (r-l > eps){
			mid = (r+l)/2.0;
			tri = (r+mid)/2.0;
			if (filter(mid) > filter(tri))
				l = mid;
			else
				r = tri;
		}
		double ans = filter(l);
		printf("%.2f\n", ans*ans);
	}
	return 0;
}
时间: 2024-10-13 12:18:02

POJ 3301 Texas Trip的相关文章

POJ 3301 Texas Trip (三分)

题目链接 题意 : 给你若干个点,让你找最小的正方形覆盖这所有的点.输出面积. 思路 : 三分枚举正方形两对边的距离,然后求出最大,本题用的是旋转正方形,也可以用旋转点,即点的相对位置不变. 正方形从0度到180度变化的过程中,把所有点覆盖,面积肯定是有一个最小峰值,是一个凸函数.因此可以用三分法解决.这里有一个难点就是已知两个定点的x,y坐标,过这两个点做两条平行线,平行线并与x轴成d度的角,怎么算出两条平行线的距离. d1 = fabs(cos(d)*(yi-yj)-sin(d)*(xi-x

三分 --- POJ 3301 Texas Trip

Texas Trip Problem's Link:   http://poj.org/problem?id=3301 Mean: 给定n(n <= 30)个点,求出包含这些点的面积最小的正方形的面积. analyse: 首先要确定的是旋转的角度在0到180度之间即可,超过180度是和前面的相同的. 坐标轴旋转后,坐标变换为: X’ = x * cosa - y * sina; y’ = y * cosa + x * sina; Time complexity: O(n) Source code

poj 3301 Texas Trip(几何+三分)

Description After a day trip with his friend Dick, Harry noticed a strange pattern of tiny holes in the door of his SUV. The local American Tire store sells fiberglass patching material only in square sheets. What is the smallest patch that Harry nee

POJ 3301:Texas Trip(计算几何+三分)

http://poj.org/problem?id=3301 题意:在二维平面上有n个点,每个点有一个坐标,问需要的正方形最小面积是多少可以覆盖所有的点. 思路:从第二个样例可以看出,将正方形旋转45°的时候,面积是最小的. 因此考虑旋转正方形,就可以当作旋转本来的点,对于旋转后的点,求最大的x和最小的x,最大的y和最小的y,就可以求得覆盖旋转后的点的正方形面积了. 然后对于每一个角度,都要进行判断,这个时候就觉得要用到X分了. 因为不满足单调性,所以用了三分.(其实也不太清楚为什么能三分).

POJ 1734 Sightseeing trip (Floyd 最小环+记录路径)

Sightseeing trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5040   Accepted: 1932   Special Judge Description There is a travel agency in Adelton town on Zanzibar island. It has decided to offer its clients, besides many other attra

poj3301 Texas Trip【三分算法】

题目地址:http://poj.org/problem?id=3301 简述:T组测试数据,每组线输入n,代表有n个点,接下来输入这n个点的坐标,坐标都是整数. 要求用一个最小的正方形覆盖所有的点,输出它的面积,精确到小数点后两位. 算法思路:枚举角度,计算面积, 三分枚举 (可参考:程序设计 解题策略  吴永辉...著 394页) 代码: #include <stdio.h> #include <string.h> #include <stdlib.h> #inclu

poj 1734 Sightseeing trip判断最短长度的环

Sightseeing trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5590   Accepted: 2151   Special Judge Description There is a travel agency in Adelton town on Zanzibar island. It has decided to offer its clients, besides many other attra

POJ 1734.Sightseeing trip 解题报告

Floyd 最小环模板题 code /* floyd最小环,记录路径,时间复杂度O(n^3) 不能处理负环 */ #include <iostream> #include <cstring> using namespace std; const int INF = 109, maxn = 252645135; int g[INF][INF], dis[INF][INF], pre[INF][INF]; int ans[INF]; //pr[i][j]记录i到j最短路径的第一个点 i

POJ 1734 Sightseeing trip

题目大意: 求一个最小环. 用Floyd 求最小环算法. #include <iostream> #include <cstdlib> #include <cstdio> #include <algorithm> #include <vector> #include <queue> #include <cmath> #include <cstring> using namespace std; #define