神奇的3分法,求单峰函数极值的利器!!!
公式可以画图推导如下:
y1=cos(a)*y-sin(a)*x;
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3429 | Accepted: 1018 |
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
24-1 -11 -11 1-1 1410 110 -1-10 1-10 -1
Sample Output
4.00242.00
Source
1 #include <cstdio> 2 #include <cmath> 3 4 using namespace std; 5 6 #define INF (1<<25) 7 #define eps (1e-12) 8 #define PI acos(-1.0); 9 10 double x[33]; 11 double y[33]; 12 13 int n; 14 15 double mindis(double a) 16 { 17 double yMin=INF*1.0,xMin=INF*1.0,xMax=-INF*1.,yMax=-INF*1.0; 18 double xx[33]; 19 double yy[33]; 20 21 for(int i=0;i<n;i++) 22 { 23 xx[i]=x[i]; 24 yy[i]=y[i]; 25 } 26 27 for(int i=0;i<n;i++) 28 { 29 double xk=xx[i],yk=yy[i]; 30 xx[i]=cos(a)*xk+sin(a)*yk; 31 yy[i]=cos(a)*yk-sin(a)*xk; 32 } 33 34 for(int i=0;i<n;i++) 35 { 36 if(xx[i]<xMin) 37 { 38 xMin=xx[i]; 39 } 40 if(yy[i]<yMin) 41 { 42 yMin=yy[i]; 43 } 44 if(xx[i]>xMax) 45 { 46 xMax=xx[i]; 47 } 48 if(yy[i]>yMax) 49 { 50 yMax=yy[i]; 51 } 52 } 53 54 double ansx=xMax-xMin; 55 double ansy=yMax-yMin; 56 57 if(ansx-ansy>eps) 58 { 59 return ansx; 60 } 61 else 62 return ansy; 63 64 } 65 66 67 int main() 68 { 69 int m; 70 scanf("%d",&m); 71 while(m--) 72 { 73 scanf("%d",&n); 74 for(int i=0;i<n;i++) 75 { 76 scanf("%lf%lf",&x[i],&y[i]); 77 } 78 79 double st=0,ed=PI; 80 81 while(ed-st>=eps) 82 { 83 double m1=(st*2+ed)/3.; 84 double m2=(st+2*ed)/3.; 85 86 double dism1=mindis(m1); 87 double dism2=mindis(m2); 88 89 if(dism1-dism2<eps) 90 { 91 ed=m2; 92 } 93 else 94 { 95 st=m1; 96 } 97 98 } 99 100 printf("%.2lf\n",mindis(st)*mindis(st)); 101 } 102 103 return 0; 104 }