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 ≤ and that no data set contains more than points. All points in each data set will be no more than units away from (,).
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
- -
- - -
-
- -
Sample Output
4.00
242.00
Source
Waterloo Local Contest, 2007.7.14
这里要知道坐标旋转公式:
如果为旋转前的坐标,为旋转后的坐标,那么有:
然后对给定的所以点进行旋转,范围为[0,PI/2.0],通过三分搜索找到最小的边长,这里用三分搜索是因为旋转函数是一个单峰函数,然后就可以得出答案了。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
int dirx[]={,,-,};
int diry[]={-,,,};
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 36
#define inf 1<<26 int n;
struct Node{
double x,y;
}node[N];
double cal(double a){
double sx=inf;
double sy=inf;
double ex=-inf;
double ey=-inf;
for(int i=;i<n;i++){
double x1=node[i].x*cos(a)-node[i].y*sin(a);
double y1=node[i].y*cos(a)+node[i].x*sin(a);
sx=min(sx,x1);
ex=max(ex,x1);
sy=min(sy,y1);
ey=max(ey,y1);
} return max(ex-sx,ey-sy); }
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%lf%lf",&node[i].x,&node[i].y);
}
double low=;
double high=PI/2.0;
while((high-low)>eps){
double mid1=(*low+high)/;
double mid2=(low+*high)/;
double ans1=cal(mid1);
double ans2=cal(mid2);
if(ans1<ans2){
high=mid2;
}
else{
low=mid1;
}
} printf("%.2lf\n",cal(low)*cal(low));
}
return ;
}