Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5942 | Accepted: 1838 |
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
题意:在坐标轴上有n个点,找出一个最小的正方形来把所有点都包含在内,输出它的面积。
思路:由于这个正方形可以有倾斜角度,所以找到正方形面积关于倾斜角度的函数,这是一个凹函数,对此三分查找。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
using namespace std;
#define eps 10e-9
int x[35],y[35];
int t,n;
double l,r;
double f(double d) //凹函数,包含全部点的最小正方形面积关于边倾斜角度的函数
{
double dis1,dis2;
double dis=0;
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
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]) );
if(dis1>dis)dis=dis1;
if(dis2>dis)dis=dis2;
}
}
return dis*dis;
}
double three_devide(double low,double up) //关于角度为自变量三分
{
double m1,m2;
while(up-low>=eps) //设一个最大误差值
{
m1=low+(up-low)/3; //第一个三分点
m2=up-(up-low)/3; //第二个三分点
if(f(m1)>=f(m2))
low=m1;
else
up=m2;
}
return f((m1+m2)/2);
}
int main()
{
int x1,y1;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=0;i<n;i++)scanf("%d%d",&x[i],&y[i]);
l=0.0;
r=acos(-1.0);
printf("%.2lf\n",three_devide(l,r));
}
}