POJ - 2187:Beauty Contest (最简单的旋转卡壳,求最远距离)

Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, earning the title 'Miss Cow World'. As a result, Bessie will make a tour of N (2 <= N <= 50,000) farms around the world in order to spread goodwill between farmers and their cows. For simplicity, the world will be represented as a two-dimensional plane, where each farm is located at a pair of integer coordinates (x,y), each having a value in the range -10,000 ... 10,000. No two farms share the same pair of coordinates. 

Even though Bessie travels directly in a straight line between pairs of farms, the distance between some farms can be quite large, so she wants to bring a suitcase full of hay with her so she has enough food to eat on each leg of her journey. Since Bessie refills her suitcase at every farm she visits, she wants to determine the maximum possible distance she might need to travel so she knows the size of suitcase she must bring.Help Bessie by computing the maximum distance among all pairs of farms. 

Input

* Line 1: A single integer, N 

* Lines 2..N+1: Two space-separated integers x and y specifying coordinate of each farm 

Output

* Line 1: A single integer that is the squared distance between the pair of farms that are farthest apart from each other. 

Sample Input

4
0 0
0 1
1 1
1 0

Sample Output

2

Hint

Farm 1 (0, 0) and farm 3 (1, 1) have the longest distance (square root of 2) 

题意:给定二维平面上N个点,输出最远距离的平方。

思路:先求出凸包,只考虑凸包上的点。利用旋转卡壳求最远距离。模板达成。

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ll long long
#define RC rotating_calipers
using namespace std;
const int maxn=100010;
struct point{
    double x,y;
    point(double x=0,double y=0):x(x),y(y){}
    bool operator < (const point &c) const { return x<c.x||(x==c.x&&y<c.y);}
    point operator - (const point &c) const { return point(x-c.x,y-c.y);}
    double operator * (const point &c) const { return x*c.y-y*c.x; }
    double operator | (const point &c) const { return (x-c.x)*(x-c.x)+(y-c.y)*(y-c.y); }
};
double det(point A,point B){ return A.x*B.y-A.y*B.x;}
double det(point O,point A,point B){ return det(A-O,B-O);}
point a[maxn],ch[maxn];
void convexhull(int n,int &top)
{
    sort(a+1,a+n+1); top=0;
    for(int i=1;i<=n;i++){
        while(top>1&&det(ch[top-1],ch[top],a[i])<=0) top--;
        ch[++top]=a[i];
    }
    int ttop=top;
    for(int i=n-1;i>=1;i--){
        while(top>ttop&&det(ch[top-1],ch[top],a[i])<=0) top--;
        ch[++top]=a[i];
    }
}
double rotating_calipers(point p[],int top)
{
    double ans=0; int now=2;
    rep(i,1,top-1){
        while(det(p[i],p[i+1],p[now])<det(p[i],p[i+1],p[now+1])){
            now++; //最远距离对应了最大面积。
            if(now==top) now=1;
        }
        ans=max(ans,(p[now]|p[i]));
    }
    return ans;
}
int main()
{
    int N; scanf("%d",&N);
    for(int i=1;i<=N;i++)
      scanf("%lf%lf",&a[i].x,&a[i].y);
    int top; convexhull(N,top);
    printf("%lld",(ll)RC(ch,top));
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/hua-dong/p/9619998.html