poj 3301 Texas Trip (三分)

本文介绍了一个编程问题,即如何寻找能够覆盖一组特定坐标点的最小面积正方形,并提供了一种解决方案——通过三分法来确定最优的旋转角度,使得计算出的正方形面积达到最小。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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));
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值