#POJ3301#Texas Trip(三分查找)

来源:互联网 发布:决战武林进阶数据灵宠 编辑:程序博客网 时间:2024/04/29 21:57

Texas Trip
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 5778 Accepted: 1786

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


这个题是真的厉害了,当时把我看懵了,因为感觉不知从何开始,

然后我莫名其妙想到了斜率,但是经过推算并不可以,

因为二分的话,它又不是一个单调的,我怎么缩小区间呢,

然后上网学习了一个新的分法:三分查找,这个其实很好懂,

推荐一篇文,写得很详细了:http://blog.csdn.net/pi9nc/article/details/9666627


这题还有个点就是要去求旋转后每个点的情况,

用公式去推就比较好:

X‘=X*cos(a)-Y*sin(a);

Y'=Y*cos(a)+X*sin(a);


Code:

StatusAcceptedTime32msMemory192kBLength1080LangC++Submitted2017-07-15 09:35:37Shared

#include<iostream>#include<cstdio>#include<cstdlib>#include<cmath>using namespace std;const int Max = 35;const double eps = 1e-9;struct node{    int x, y;}P[Max];int N;double Find(double k){    double Dis = 0.0;    for(int i = 1; i < N; ++ i)        for(int j = i + 1; j <= N; ++ j){            double Dis1 = fabs(cos(k) * (P[i].y - P[j].y) - sin(k) * (P[i].x - P[j].x));            double Dis2 = fabs(sin(k) * (P[i].y - P[j].y) + cos(k) * (P[i].x - P[j].x));            Dis = max(Dis, max(Dis1, Dis2));        }    return Dis * Dis;}int main(){    int T;    scanf("%d", &T);    while(T --){        scanf("%d", &N);        for(int i = 1; i <= N; ++ i)            scanf("%d%d", &P[i].x, &P[i].y);        double dn = 0.0, up = acos(- 1.0), mid, mmid, f1, f2;        while(up - dn >= eps){            mid = (dn + up) / 2;            mmid = (mid + up) / 2;            f1 = Find(mid);            f2 = Find(mmid);            if(f1 < f2) up = mmid;            else dn = mid;        }        printf("%0.2lf\n", min(f1, f2));    }    return 0;}





原创粉丝点击