码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3301 Texas Trip

时间:2016-05-11 07:18:48      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

题目链接: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


思路:单峰函数求极值问题。三分角度[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;
}


POJ 3301 Texas Trip

标签:

原文地址:http://blog.csdn.net/silenceneo/article/details/51369793

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!