Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4648 | Accepted: 1448 |
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
给你一些点,求覆盖这些点的最小正方形面积
但是最小正方形不一定是标准正放,穷举每个点对角度坐标表换的值(三分旋转的角度),然后求标准正放的最小正方形即可newx = x*cos(a) - y*sin(a)
newy = y*cos(a) + x*sin(a);
#include <iostream> #include <cstdio> #include <cstring> #include <stdlib.h> #include <algorithm> #include <queue> #include <map> #include <cmath> #define inf 0x3f3f3f3f #define N 501 #define eps 1e-10 #define PI 3.1415926 using namespace std; int n; double x[N], y[N]; double Length(double a) { double max_x = -inf, min_x = inf; double max_y = -inf, min_y = inf; for(int i = 0; i < n; i++) { double xx= x[i]*cos(a) - y[i]*sin(a); double yy= x[i]*sin(a) + y[i]*cos(a); max_x = max(max_x, xx); min_x = min(min_x, xx); max_y = max(max_y, yy); min_y = min(min_y, yy); } double b1 = max_x - min_x; double b2 = max_y - min_y; if(b1 > b2) return b1*b1; return b2*b2; } int main() { int T; scanf("%d", &T); while(T--) { scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%lf%lf", &x[i], &y[i]); } double low = 0; double high = PI; double mid1, mid2; while(high - low > eps) { mid1 = low + (high - low) / 3; mid2 = high - (high - low) / 3; double b1 = Length(mid1); double b2 = Length(mid2); if(b1 < b2) high = mid2; else low = mid1; } printf("%.2lf\n",Length(low)); } return 0; }