POJ 3301 Texas Trip

Texas Trip
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3786 Accepted: 1132

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

   本想着这是一个凸包的题目,原来是三分啊,三分正方形旋转的角度,对于某一固定角度,利用几何知识算出最小的正方形面积
推导出以下公式
dis1 = fabs((y2-y1)*sin(d)+(x2-x1)*cos(d));
dis2 = fabs((y2-y1)*cos(d)-(x2-x1)*sin(d));
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#define N 40
#define eqs 1e-15
using namespace std;
int n;
struct num
{
    double x,y;
}a[N];
double dis;
int main()
{
   //freopen("data.in","r",stdin);
    void search();
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0;i<=n-1;i++)
        {
            scanf("%lf %lf",&a[i].x,&a[i].y);
        }
        search();
        printf("%.2lf\n",dis*dis);
    }
    return 0;
}
double get_dis(double d)
{
    double Max = 0;
    for(int i=0;i<=n-1;i++)
    {
        for(int j=i+1;j<=n-1;j++)
        {
            double x1 = a[i].x;
            double y1 = a[i].y;
            double x2 = a[j].x;
            double y2 = a[j].y;
            double dis1 = fabs((y2-y1)*sin(d)+(x2-x1)*cos(d));
            double dis2 = fabs((y2-y1)*cos(d)-(x2-x1)*sin(d));
            Max = max(Max,dis1);
            Max = max(Max,dis2);
        }
    }
    return Max;
}
void search()
{
    double  l = 0,r = asin(1.0);
    double mid1,mid2;
    while(r-l>eqs)
    {
        mid1 = (l+r)/2;
        mid2 = (mid1+r)/2;
        double dis1 = get_dis(mid1);
        double dis2 = get_dis(mid2);
        if(dis1<dis2)
        {
            dis = dis1;
            r = mid2;
        }else
        {
            dis = dis2;
            l = mid1;
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值