【三分 + 几何】Texas Trip POJ - 3301 (将所有点框在一个矩形中,使面积最小)

三分始终舍弃离极值点远的那一边
midl = (l + r) / 2或者 midl = (r - l) / 3 + l
midr = (midl + r) / 2或者 midr = r - (r - l) / 3

【二分 + 几何】Texas Trip POJ - 3301 (将所有点框在一个矩形中,使面积最小)

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

题意:
给定一堆点,求包含所有点的最小正方形

思路:
如图,假设出这样一个最小矩形,包含了所有点。
这里写图片描述

如果这个正方形的边平行于X轴就,那么边长就是点的横坐标最大差或者纵坐标最大差,即max(dx, dy)。
那么现在这是一个斜着的正方形,怎么办呢?我们可以把所有点旋转一个角度(极坐标),然后使得可以用一个平行于X轴的正方形框住这些点。然后再用max(dx, dy)求边长即可。

显然在旋转过程中,只有一个角度求得的面积是最小的,角度过大过小都会使面积变大。因此这是一个凹函数,用三分

角度需要枚举【0,pi / 2】

TIPS
极坐标旋转公式:
x'=x*cos(angle)-y*sin(angle)
y'=y*cos(angle)+x*sin(angle)

AC代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#define maxn 1005
#define INF 0x3f3f3f3f
#define pi 3.1415926535
using namespace std;

int t, n;

struct point
{
    int x, y;
} p[maxn];

double getl(double ang)
{
    double minx = INF, maxx = - INF, miny = INF, maxy = -INF;
    double dx, dy;
    for(int i = 0; i < n; i++)
    {
        dx = p[i].x * cos(ang) - p[i].y * sin(ang);
        dy = p[i].y * cos(ang) + p[i].x * sin(ang);
        minx = min(minx, dx);
        maxx = max(maxx, dx);
        miny = min(miny, dy);
        maxy = max(maxy, dy);
    }
    return max(maxx - minx, maxy - miny);
}

void sol()
{
    double l = 0, r = pi;
    for(int i = 0; i < 100; i++)
    {
        double m1 = (r - l) / 3  + l, m2 = r - (r - l) / 3;
        double l1 = getl(m1), l2 = getl(m2);
        if(l1 < l2)
            r = m2;
        else
            l = m1;
    }
    printf("%.2f\n", getl(l) * getl(l));
}

int main()
{
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        for(int i = 0; i < n; i++)
            scanf("%d%d", &p[i].x, &p[i].y);
        sol();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/floraqiu/article/details/81210848