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 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 followingn 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
题意如下:

给你n个点的坐标,然后让你选择一个最小的正方形将它们全部覆盖,求这个正方形的最小面积

思路如下:

首先考虑让正方形平行于X轴放置的面积,那么最小面积必然是这个正方形进行一定的旋转之后得到的,但是问题在于这个正方形并不一定是要以其对角线的交点旋转的,因此难以处理,但是可以换一种思路:考虑将所有的点进行旋转,这时只需找到演X轴或者Y轴距离最远的一对点的距离即时旋转这个角度的答案了。

旋转后新点的坐标计算公式为:

x1 = x*cos(a) - y*cos(a)

y1 = y*cos(a) + x*cos(a)  (a为旋转弧度,x,y为原点坐标)


稍微证明一下为什么旋转过程是一个单峰函数(如有错误,还望指正):

我们现在假设我们取得是一个最大的正方形,现在我们要通过一定的旋转让其面积缩小(不考虑只有一个合法方案的情况)。

此时必然有一些点是在正方形的边上面的,那么我们便可以通过旋转让其进入正方形内部,此时便可以缩短边长让其再回到边上(如果旋转让一部分出了正方形覆盖的话那么说明现在的状态已经是答案了~)


代码如下:

#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
using namespace std;
const int maxn = 35;
const double pi = acos(-1);
const double eps = 1e-10;
const double inf = 1e9;
struct node{
    double x,y;
};
int n;
node point[maxn];

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

int main(){
    int test;
    scanf("%d",&test);
    while(test--){
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%lf%lf",&point[i].x,&point[i].y);
        }
        double l=0,r=pi/2;
        while(r-l>eps){
            double ll=(l*2+r)/3;
            double rr=(l+r*2)/3;
            double a1=solve(ll),a2=solve(rr);
            if(a1>a2)l=ll;
            else r=rr;
        }
        printf("%.2f\n",solve(l)*solve(l));
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值