poj 3301 Texas Trip (三分)

易镜
2023-12-01

Texas Trip
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5487 Accepted: 1702

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

[Submit]   [Go Back]   [Status]   [Discuss]


题目大意:用一个面积最小的正方形覆盖所有点。

题解:三分

正方形相对于(0,0)的倾斜度满足面积单峰,正方形旋转不好计算,所以我们考虑将所有的点旋转然后进行计算。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define eps 1e-12
#define N 103
#define inf 1000000000
using namespace std;
int n,m;
struct data{
	double x,y;
	data (double X=0,double Y=0){
		x=X,y=Y;
	}
}a[N],b[N];
data rotate(data a,double rad)
{
	return data (a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));
}
double pow1(double x)
{
	return x*x;
}
double calc(double rad)
{
	for (int i=1;i<=n;i++) b[i]=rotate(a[i],rad);
    double mxx=-inf,mxy=-inf;
    double mnx=inf,mny=inf;
    for (int i=1;i<=n;i++){
    	mxx=max(mxx,b[i].x);
    	mnx=min(mnx,b[i].x);
    	mxy=max(mxy,b[i].y);
    	mny=min(mny,b[i].y);
	}
	return pow1(max(mxx-mnx,mxy-mny));
}
int main()
{
	freopen("a.in","r",stdin);
	int T;
	scanf("%d",&T);
	for (int t=1;t<=T;t++){
		scanf("%d",&n);
		for (int i=1;i<=n;i++) scanf("%lf%lf",&a[i].x,&a[i].y);
		double l=0; double r=acos(-1.0);
		while (r-l>eps) {
			double mid=(l+r)/2;
			double midmid=(l+mid)/2;
			if (calc(mid)>calc(midmid))
			 r=mid-eps;
			 else l=midmid+eps;
		}
		printf("%.2lf\n",calc(l));
	}
}


 类似资料: