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
用一个正方形覆盖所有的点,在正方形旋转的过程中有最小值,在中间一个角度取得,因此需要三分。
这里有一个难点就是已知两个定点的x,y坐标,过这两个点做两条平行线,平行线并与x轴成d度的角,怎么算出两条平行线的距离。

我查了很久的资料,看到别人得出的公式是

d1 = fabs(cos(d)*(yi-yj)-sin(d)*(xi-xj));

d2 = fabs*(sin(d)*(yi-yj)+cos(d)*(xi-xj));

为什么会有两个距离呢?

因为两个点可以做出两对平行线,一条是与x轴成d度角,另一个是与x成(180-d)度角

代码
#include <iostream>
#include <math.h>
#include <stdio.h>
using namespace std;
int n,x[100],y[100];
double hehe(double d)
{
   double dj1,dj2,dj=0,dj0;
  for(int i=1;i<n;i++)
  {
   for(int j=i+1;j<=n;j++)
   {
   dj1=fabs(cos(d)*(y[i]-y[j])-sin(d)*(x[i]-x[j]));
   dj2=fabs(sin(d)*(y[i]-y[j])+cos(d)*(x[i]-x[j]));
   dj0=max(dj1,dj2);
    if(dj0>dj)
     dj=dj0;
   }
  }
  return dj*dj;
}
int main()
{
    int t;
    double mid,midmid,l,r,d1,d2;
    cin>>t;
    while(t--)
    {
     cin>>n;
     for(int i=1;i<=n;i++)
     {
      cin>>x[i]>>y[i];
     }
     l=0;
     r=acos(-1.0);
     do
     {
      mid=(l+r)/2;
      midmid=(mid+r)/2;
      d1=hehe(mid);
      d2=hehe(midmid);
      if(d1<=d2)
      r=midmid;
      else
      l=mid;
     }while(r-l>=1e-9);
    printf("%.2f\n",d1);
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值