Texas Trip POJ - 3301 三分

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

平面上有n个点,求最小的能够覆盖所有点的正方形的面积。
虽然求的是面积,但是应该三分找的是角度,将直角坐标系旋转,判断点之间两两连接成的直线在各个角度下最长x,y轴跨度。

#include <stdio.h>
#include <math.h>

const double PI=acos(-1.0);
double a[31],b[31];
int T,n,i,j;

double check(double r){
    double re,lt;
    re=0;
    for(i=0;i<n;i++){
        for(j=i+1;j<n;j++){
            lt=fabs(cos(r)*(b[i]-b[j])-sin(r)*(a[i]-a[j]));
            if(lt>re)
                re=lt;
            lt=fabs(sin(r)*(b[i]-b[j])+cos(r)*(a[i]-a[j]));
            if(lt>re)
                re=lt;
        }
    }
    return re;
}

int main()
{
    double low,high,mid,mmid,lm,lmm,s;
    scanf("%d",&T);
        while(T--){
        scanf("%d",&n);
        for(i=0;i<n;i++){
            scanf("%lf %lf",&a[i],&b[i]);
        }
        low=0;high=PI/2;
        while(high-low>1e-8){
            mid=(high+low)/2;
            mmid=(high+mid)/2;
            lm=check(mid);
            lmm=check(mmid);
            if(lm>lmm){
                low=mid;
            }else{
                high=mmid;
            }

        }
        if(lm<lmm)
            s=lm;
        else
            s=lmm;
        s*=s;
        printf("%.2lf\n",s);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值