poj 3301 Texas Trip (三分求极值)

Texas Trip
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3233   Accepted: 948

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

Waterloo Local Contest, 2007.7.14
首先旋转的角度只要在0到180度即可,超过180度跟前面的相同的。坐标轴旋转后,图形的坐标变换为:
X’ = x * cosa - y * sina;

y’ = y * cosa + x * sina;

坐标轴绕原点逆时针旋转角度,相当于图形绕原点顺时针旋转角度,角度取负,坐标的转换公式很容易求,画出坐标研究下就出来了。

问题是要求最小的正方形,假设这个正方形的边都是分别与坐标轴平行,也就是说正方形没有旋转一定的角度,那么我们只要考虑最上,最下,最左,最右 的点即可,当正方形旋转过一定的角度d是我们也只要考虑最边上的点的距离差即可(故这题也可用枚举旋转角度的方法来求解,但要注意步长的选取以保证精度)。

yi-yj相当于在旋转后的坐标的y方向的最大长度,xi-xj相当于在旋转后的坐标的x方向的最大长度,取俩者中最大的,枚举边长,找出面积最小的正方形。

假设旋转角度为d,那么枚举每两个点关于旋转角度为d的直线距离取最大值,即可保证覆盖所有的点,在垂直和平行坐标轴两个方向上这样的距离分别为:

   dis1 = fabs(cos(d) * (y[i] - y[j]) - sin(d) * (x[i] - x[j]))

   dis2 = fabs(sin(d) * (y[i] - y[j]) + cos(d) * (x[i] - x[j]))

当然dis1和dis2大小不一定一致,要保证图形是覆盖所有点的正方形,所以我们选取较大者为边长。

#include<iostream>
#include<cstdio>
#include<cmath>
#define eps 1e-12
using namespace std;
struct point
{
    double x,y;
};
point data[35];
int n;
int sgn(double a)
{
    return (a>eps)-(a<-eps);
}
double dist1(double g)
{
    int i,j;double m=0,k;
    for(i=0;i<n-1;i++)
    for(j=i+1;j<n;j++)
    {
      k=fabs((data[i].y-data[j].y)*cos(g)-(data[i].x-data[j].x)*sin(g));
      if(k>m)
      m=k;
    }
    return m;
}
double dist2(double g)
{
    int i,j;double m=0,k;
    for(i=0;i<n-1;i++)
    for(j=i+1;j<n;j++)
    {
      k=fabs((data[i].y-data[j].y)*sin(g)+(data[i].x-data[j].x)*cos(g));
      if(k>m)
      m=k;
    }
    return m;
}
double ping(double a,double b)
{
    return a>b?a*a:b*b;
}
void work()
{
    double t1,t2,l,r,mid1,mid2;
    l=0;r=acos(-1.0);
    do
    {
       mid1=(l+r)*0.5;
       mid2=(mid1+r)*0.5;
       t1=ping(dist1(mid1),dist2(mid1));
       t2=ping(dist1(mid2),dist2(mid2));
       if(t1<t2)
       r=mid2;
       else
       l=mid1;
    }
    while(sgn(r-l)>0);
    printf("%.2lf\n",t1);
}
int main()
{
    int t,i;
    double ff;
    scanf("%d",&t);
     while(t--)
    {
        scanf("%d",&n);
        for(i=0;i<n;i++)
        scanf("%lf%lf",&data[i].x,&data[i].y);
        work();
    }

    return 0;
}



你可能感兴趣的:(Integer,input,each,图形,patch,output)