POJ 3301 Texas Trip(最小正方形覆盖+三分)

最小覆盖正方形
本文介绍了一种算法,用于解决如何找到包含一组整数点的最小面积正方形的问题。该算法通过旋转坐标系来寻找最优解,并使用三分法确定最佳旋转角度。

传送门

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

题目大意:

T 组数据,每组数据有 n 个整数点,现在要求用一个正方形使得所有点都在正方形内(包括边界),求这样的正方形最小的面积。

解题思路:

(1)首先考虑当正方形为平行于坐标轴的情况:
面积为 max(,)max(,)
(2)当然这不是最优的,那么如果我们将正方形进行旋转之后会发现一定会有一个最优值,那么旋转正方形操作有点困难,那么我们可以考虑旋转坐标系,得到最新的坐标,然后在根据 (1) 进行计算,因为这是一个先递减后递增的函数,所以一定有极小值,那么我们可以三分得到其最小值。
旋转之后的坐标如下:
这里写图片描述

代码:

#include <iostream>
#include <string.h>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
typedef long long LL;
const int MAXN = 35;
const double PI = acos(-1);
const double eps = 1e-8;
const LL MOD = 1e9+7;
struct Point{
    double x, y;
}a[MAXN];
int n;
double f(double x){
    double x1=MOD, x2=-MOD, y1=MOD, y2=-MOD;
    for(int i=0; i<n; i++){
        double dis = sqrt(a[i].x*a[i].x + a[i].y*a[i].y);
        double theta = atan2(1.0*a[i].y,(1.0*a[i].x+eps))-x;
        double yy = dis*sin(theta);
        double xx = dis*cos(theta);
        x1 = min(x1, xx); x2 = max(x2, xx);
        y1 = min(y1, yy); y2 = max(y2, yy);
    }
    return max(x2-x1,y2-y1)*max(x2-x1,y2-y1);
}
double sanfen(double left, double right){
    double midl, midr;
    while (right-left > eps){
        midl = left + (right - left)/3;
        midr = right - (right - left)/3;
        if(f(midl) <= f(midr)) right = midr;
        else left = midl;
    }
    return f(left);
}

int main(){
    //freopen("C:/Users/yaonie/Desktop/in.txt", "r", stdin);
    //freopen("C:/Users/yaonie/Desktop/out.txt", "w", stdout);
    int T; scanf("%d", &T);
    while(T--){
        scanf("%d", &n);
        for(int i=0; i<n; i++) scanf("%lf%lf",&a[i].x,&a[i].y);
        double ans = sanfen(0, 0.5*PI);
        printf("%.2f\n",ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值