Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4034 | Accepted: 1211 |
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 followingn 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轴最大坐标差,y轴最大坐标差,选出两者最大值。据说这个最大值与角度的函数关系是凹函数,所以可以枚举0到180度。这个只是据说,没有证明。另外注意精度要取到1e-12才能过。
code:#include<iostream> #include<cstdio> #include<cmath> using namespace std; struct node{ double x,y; } Point[50]; int N; double cal(double mid){ double minx=0x3f3f3f3f,miny=0x3f3f3f3f,maxx=-0x3f3f3f3f,maxy=-0x3f3f3f3f,x,y; for(int i=0;i<N;i++){ x=Point[i].x*cos(mid)-Point[i].y*sin(mid); y=Point[i].y*cos(mid)+Point[i].x*sin(mid); minx=min(x,minx); miny=min(y,miny); maxx=max(x,maxx); maxy=max(y,maxy); } return max(maxx-minx,maxy-miny); } int main() { int t; scanf("%d",&t); while(t--){ scanf("%d",&N); for(int i=0;i<N;i++){ scanf("%lf%lf",&Point[i].x,&Point[i].y); } double left=0,right=acos(-1.0),mid,midmid,midv,midmidv; while(fabs(right-left)>0.0000000000001){ mid=(left+right)/2.0; midmid=(mid+right)/2.0; midv=cal(mid); midmidv=cal(midmid); if(midv<=midmidv){ right=midmid; } else left=mid; } double l=cal(mid); printf("%.2lf\n",l*l); } return 0; }