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
Waterloo Local Contest, 2007.7.14
这里要知道坐标旋转公式:
如果为旋转前的坐标,为旋转后的坐标,那么有:
然后对给定的所以点进行旋转,范围为[0,PI/2.0],通过三分搜索找到最小的边长,这里用三分搜索是因为旋转函数是一个单峰函数,然后就可以得出答案了。
1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include<iostream> 3 #include<cstdio> 4 #include<cstring> 5 #include<cmath> 6 #include<math.h> 7 #include<algorithm> 8 #include<queue> 9 #include<set> 10 #include<bitset> 11 #include<map> 12 #include<vector> 13 #include<stdlib.h> 14 #include <stack> 15 using namespace std; 16 int dirx[]={0,0,-1,1}; 17 int diry[]={-1,1,0,0}; 18 #define PI acos(-1.0) 19 #define max(a,b) (a) > (b) ? (a) : (b) 20 #define min(a,b) (a) < (b) ? (a) : (b) 21 #define ll long long 22 #define eps 1e-10 23 #define MOD 1000000007 24 #define N 36 25 #define inf 1<<26 26 27 int n; 28 struct Node{ 29 double x,y; 30 }node[N]; 31 double cal(double a){ 32 double sx=inf; 33 double sy=inf; 34 double ex=-inf; 35 double ey=-inf; 36 for(int i=0;i<n;i++){ 37 double x1=node[i].x*cos(a)-node[i].y*sin(a); 38 double y1=node[i].y*cos(a)+node[i].x*sin(a); 39 sx=min(sx,x1); 40 ex=max(ex,x1); 41 sy=min(sy,y1); 42 ey=max(ey,y1); 43 } 44 45 return max(ex-sx,ey-sy); 46 47 } 48 int main() 49 { 50 int t; 51 scanf("%d",&t); 52 while(t--){ 53 scanf("%d",&n); 54 for(int i=0;i<n;i++){ 55 scanf("%lf%lf",&node[i].x,&node[i].y); 56 } 57 double low=0; 58 double high=PI/2.0; 59 while((high-low)>eps){ 60 double mid1=(2*low+high)/3; 61 double mid2=(low+2*high)/3; 62 double ans1=cal(mid1); 63 double ans2=cal(mid2); 64 if(ans1<ans2){ 65 high=mid2; 66 } 67 else{ 68 low=mid1; 69 } 70 } 71 72 73 printf("%.2lf ",cal(low)*cal(low)); 74 } 75 return 0; 76 }