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 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


#include<cstdio>
#include<cmath>
#include<cstring>
#define EPS 1e-12
#define chu 10000000
double x[100],y[100];
int n;
double du(double a)
{
    double minx=chu,miny=chu,maxx=-chu,maxy=-chu;
    int i;
    for(i=0;i<n;i++)
    {
        double x1=x[i]*cos(a)-y[i]*sin(a);
        double y1=x[i]*sin(a)+y[i]*cos(a);
        if(x1>maxx) maxx=x1;
        if(x1<minx) minx=x1;
        if(y1>maxy) maxy=y1;
        if(y1<miny) miny=y1;
    }
    if(maxx-minx>maxy-miny) return maxx-minx;
    else return maxy-miny;
}
int main()
{
    int k;
    scanf("%d",&k);
    while(k--)
    {
        int i;
        memset(x,0,sizeof(x));
        memset(y,0,sizeof(y));
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%lf %lf",&x[i],&y[i]);
            x[i]=x[i]+555;
            y[i]=y[i]+555;
        }
        double L=0.0,R=acos(-1.0),mid1,mid2;
       // int cnt=0;
        while(R-L>EPS)
        {
          //  cnt++;
            mid1=L+(R-L)/3;
            mid2=R-(R-L)/3;
            if(du(mid1)>du(mid2)) L=mid1;
            else R=mid2;
        }
       // printf("%d\n",cnt);
       // printf("%.2f\n",du(L));
        printf("%.2f\n",du(L)*du(L));
    }
    return 0;
}

/*
 如果正方形两相邻边是与坐标轴平行的,那么只需求出所有点的横坐标差的最大值,纵坐标差的最大值,然后再求两者的最大值,
 同样的道理当正方形相邻两边与坐标轴不平行时,旋转坐标轴,使新的坐标轴平行正方形的两相邻边,
 然后把旧坐标轴上的点的坐标全部转化到新的坐标轴上,再求出横纵坐标差的最大值,取两者的最大值即可。
 分析知二维空间上,对于任意正方形的位置,总能顺时针旋转坐标轴0~90‘ 达到满足要求。

 当坐标旋转a角度时可以求出 x1=x*cos(a)-y*sin(a);   y1=x*sin(a)+y]*cos(a);   
 可以证明它们都是凸性函数,故他们差的最大值也是凸性函数,故可以用三分法,对旋转角度进行三分.
*/




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值