Texas Trip POJ - 3301 三分 几何

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

Hint

题意

求覆盖所有点的最小正方形

题解:

看了ACdreamer 的 解析
利用坐标的旋转找出旋转后的点之间最大距离 (找边)
然后利用三分找出这个距离的最小值
坐标旋转公式 tx = x*cos(t)-y*sin(t)
ty = y*cos(t)+x*sin(t)

AC代码

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long LL;
const double eps = 1e-8;
const int N = 1e5;
int n;
double x[N],y[N];
double check(double k){
    double xx = -1e9,nx = 1e9;
    double xy = -1e9,ny = 1e9;
    for (int i = 0; i < n; ++i){
        double tx = x[i]*cos(k)-y[i]*sin(k),ty = y[i]*cos(k)+x[i]*sin(k);
        xx = max(tx,xx);
        xy = max(ty,xy);
        nx = min(tx,nx);
        ny = min(ty,ny);
    }
    return max(xy-ny,xx-nx);
}
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 l = 0,r = acos(-1.0)/2;
        while (r-l>eps){
            double ll = (2*l+r)/3;
            double rr = (2*r+l)/3;
            double ans1 = check(ll);
            double ans2 = check(rr);
            if (ans1>ans2) l = ll;
            else r = rr;
        }
        printf("%.2lf\n",check(l)*check(l));
    }

    return 0;
}

你可能感兴趣的:(二分法/三分法,数学(几何))