Language:
Texas Trip
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 |
题意:求出能覆盖平面所有点的最小正方形
解题思路:旋转坐标依次求出能覆盖的正方形三分角度旋转求出最小正方形
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#define inf 0x3f3f3f3f
#define eps 1e-8
#define PI acos(-1.0)
using namespace std;
struct point{
double x,y;
}A[35];
double MAX(double a,double b){
return a>b?a:b;
}
double MIN(double a,double b){
return a<b?a:b;
}
int sgn(double n){
if(fabs(n)<eps)return 0;
if(n<0)return -1;
return 1;
}
int N;
double getL(double angle){//求正方形边长
int i,j,k;
double L,maxx=-1010.0,maxy=-1010.0,minx=1010.0,miny=1010.0;
for(i=0;i<N;++i){
double x=A[i].x*cos(angle)-A[i].y*sin(angle);//坐标旋转
double y=A[i].x*sin(angle)+A[i].y*cos(angle);
minx=MIN(minx,x);
miny=MIN(miny,y);
maxx=MAX(maxx,x);
maxy=MAX(maxy,y);
}
return MAX(maxx-minx,maxy-miny);
}
int main()
{
int t,i,j;
scanf("%d",&t);
while(t--){
scanf("%d",&N);
for(i=0;i<N;++i){
scanf("%lf%lf",&A[i].x,&A[i].y);
}
int size=100;
double left=0,right=PI,mid,mmid,ans;
while(size--){//三分角度
mid=(left+right)/2.0;
mmid=(mid+right)/2.0;
double l1=getL(mid);
double l2=getL(mmid);
if(sgn(l1-l2)<=0){
right=mmid;
ans=l1;
}
else
left=mid;
}
printf("%.2lf\n",ans*ans);
}
return 0;
}