Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3786 | Accepted: 1132 |
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
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#define N 40
#define eqs 1e-15
using namespace std;
int n;
struct num
{
double x,y;
}a[N];
double dis;
int main()
{
//freopen("data.in","r",stdin);
void search();
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=0;i<=n-1;i++)
{
scanf("%lf %lf",&a[i].x,&a[i].y);
}
search();
printf("%.2lf\n",dis*dis);
}
return 0;
}
double get_dis(double d)
{
double Max = 0;
for(int i=0;i<=n-1;i++)
{
for(int j=i+1;j<=n-1;j++)
{
double x1 = a[i].x;
double y1 = a[i].y;
double x2 = a[j].x;
double y2 = a[j].y;
double dis1 = fabs((y2-y1)*sin(d)+(x2-x1)*cos(d));
double dis2 = fabs((y2-y1)*cos(d)-(x2-x1)*sin(d));
Max = max(Max,dis1);
Max = max(Max,dis2);
}
}
return Max;
}
void search()
{
double l = 0,r = asin(1.0);
double mid1,mid2;
while(r-l>eqs)
{
mid1 = (l+r)/2;
mid2 = (mid1+r)/2;
double dis1 = get_dis(mid1);
double dis2 = get_dis(mid2);
if(dis1<dis2)
{
dis = dis1;
r = mid2;
}else
{
dis = dis2;
l = mid1;
}
}
}