poj 3301 Texas Trip 三分搜索

来源:互联网 发布:潜水教练 知乎 编辑:程序博客网 时间:2024/06/05 15:43

Texas Trip
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 4459 Accepted: 1375

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

24-1 -11 -11 1-1 1410 110 -1-10 1-10 -1

Sample Output

4.00242.00
题意:平面上给出一些点,求出最小包含所有点的正方形的面积。

思路:要求最小的正方形包含所有的点,一个简单的思路就是先求出x,y的最大、最小值做一个初始正方形然后开始旋转,旋转过程中便会出现一个最小的面积包含所有的点。但是旋转正方形并改变边长也并不容易,所以就让正方形不动,旋转点,对于每个旋转角度,求出x,y最大最小值,这就转化为一个三分搜索求极值的问题(对于旋转过程中正方形面积变化为什么是旋转角度凸函数我就不知道了。。。)。所以最终三分旋转角度,求出面积极值。。

代码:

#include <cstdio>#include <cmath>#include <algorithm>using namespace std;#define maxn 50#define inf 0x3fffffff#define pi acos(-1.0)struct Point{double x,y;}p[maxn];int n;double f(double a){double minx=inf,maxx=-inf,miny=inf,maxy=-inf;double tx,ty;for(int i=0;i<n;i++){tx = p[i].x*cos(a) - p[i].y*sin(a);ty = p[i].y*cos(a) + p[i].x*sin(a);if(tx>maxx) maxx = tx;if(tx<minx) minx = tx;if(ty>maxy) maxy = ty;if(ty<miny) miny = ty;}return max(maxx-minx,maxy-miny);}int main(){int T;scanf("%d",&T);while(T--){scanf("%d",&n);for(int i=0;i<n;i++){scanf("%lf %lf",&p[i].x,&p[i].y);}double l=0.0, r = pi,ll,rr;for(int i=0;i<100;i++){ll = l+(r-l)/3;rr = r-(r-l)/3;if(f(ll)<f(rr)) r = rr;else l = ll;}printf("%.2lf\n",f(ll)*f(ll));}return 0;}



0 0