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
题目大意:
有
T
组数据,每组数据有
解题思路:
(1)首先考虑当正方形为平行于坐标轴的情况:
面积为
max(横坐标相差最大值,纵坐标相差最大值)∗max(横坐标相差最大值,纵坐标相差最大值)
(2)当然这不是最优的,那么如果我们将正方形进行旋转之后会发现一定会有一个最优值,那么旋转正方形操作有点困难,那么我们可以考虑旋转坐标系,得到最新的坐标,然后在根据 (1) 进行计算,因为这是一个先递减后递增的函数,所以一定有极小值,那么我们可以三分得到其最小值。
旋转之后的坐标如下:
代码:
#include <iostream>
#include <string.h>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
typedef long long LL;
const int MAXN = 35;
const double PI = acos(-1);
const double eps = 1e-8;
const LL MOD = 1e9+7;
struct Point{
double x, y;
}a[MAXN];
int n;
double f(double x){
double x1=MOD, x2=-MOD, y1=MOD, y2=-MOD;
for(int i=0; i<n; i++){
double dis = sqrt(a[i].x*a[i].x + a[i].y*a[i].y);
double theta = atan2(1.0*a[i].y,(1.0*a[i].x+eps))-x;
double yy = dis*sin(theta);
double xx = dis*cos(theta);
x1 = min(x1, xx); x2 = max(x2, xx);
y1 = min(y1, yy); y2 = max(y2, yy);
}
return max(x2-x1,y2-y1)*max(x2-x1,y2-y1);
}
double sanfen(double left, double right){
double midl, midr;
while (right-left > eps){
midl = left + (right - left)/3;
midr = right - (right - left)/3;
if(f(midl) <= f(midr)) right = midr;
else left = midl;
}
return f(left);
}
int main(){
//freopen("C:/Users/yaonie/Desktop/in.txt", "r", stdin);
//freopen("C:/Users/yaonie/Desktop/out.txt", "w", stdout);
int T; scanf("%d", &T);
while(T--){
scanf("%d", &n);
for(int i=0; i<n; i++) scanf("%lf%lf",&a[i].x,&a[i].y);
double ans = sanfen(0, 0.5*PI);
printf("%.2f\n",ans);
}
return 0;
}