二分三分 POJ3301 三分

1 篇文章 0 订阅

Texas Trip

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6388 Accepted: 1986

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

 

求最小能覆盖过所给点的正方形面积,我们可以想到,如果正方形区域是正的吗,那么y的最大最小值之差与x的最大最小值之差中大的那个就是最小正方形面积。

然而,正方形可以旋转。

但我们发现,旋转正方形就很难判定问题得到结果,那我们就转换一下,根据相对性,可以想成旋转坐标;旋转坐标不好计算旋转后的坐标,那我们再转换一下,可以想成旋转坐标系。我们可以得到坐标轴旋转t角度的横纵坐标公式:

x' = x*cost + y*sint

y' = y*cost - x*sint

现在问题又来了,我们怎样得到正方形区域面积最小的角度?

正方形面积随旋转角的变化并不是一个单调的关系,我们想到中间会有最小值的存在,所以三分角度。

 

代码如下:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#define PI acos(-1.0)
using namespace std;

const int maxn=30;
const int maxp=500,minp=-500;
const double eps=1e-8;

int n;
struct point{
	int x,y;
}p[maxn+10];

double cal(double t)
{
	double x,y,xm,xs,ym,ys;
	xm=ym=minp;
	xs=ys=maxp;
	for(int i=0;i<n;++i){
		x=p[i].x*cos(t)+p[i].y*sin(t);
		y=p[i].y*cos(t)-p[i].x*sin(t);
		if(x>xm) xm=x;
		if(x<xs) xs=x;
		if(y>ym) ym=y;
		if(y<ys) ys=y;
	}
	double sa=max((xm-xs),(ym-ys));
	return sa*sa;
}

int main()
{
	int t;
	int xm,xs,ym,ys;

	scanf("%d",&t);
	while(t--){
		xm=ym=minp;
		xs=ys=maxp;
		memset(p,0,sizeof(p));
		scanf("%d",&n);
		for(int i=0;i<n;++i){
			scanf("%d%d",&p[i].x,&p[i].y);
//			if(p[i].x>xm) xm=p[i].x;
//			if(p[i].x<xs) xs=p[i].x;
//			if(p[i].y>ym) ym=p[i].y;
//			if(p[i].x<ys) ys=p[i].y;
		}
		double left=0,right=PI/2,midl,midr;
		double cmidl,cmidr;
		while(right-left>eps){
			midl=(left+right)/2;
			midr=(midl+right)/2;
            cmidl=cal(midl);
            cmidr=cal(midr);//printf("c:%.2f  %.2f\n",cmidl,cmidr);
			if(cmidl<cmidr)
				right=midr;
			else
				left=midl;
		}
		printf("%.2f\n",cmidl);
	}

	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值