poj 2187 求凸包最远两点距离的平方值

来源:互联网 发布:巴西 知乎 编辑:程序博客网 时间:2024/05/01 11:47

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
40 00 11 11 0
Sample Output
2
Hint
Farm 1 (0, 0) and farm 3 (1, 1) have the longest distance (square root of 2) 
给定平面上的一些散点集,求最远两点距离的平方值。

#include <iostream>#include <cstring>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cmath>using namespace std;typedef long long LL;const double INF = 1e18+5;const int MAXN = 1e6 + 5;const double eps = 1e-10;const double PI = acos(-1.0);int double_cmp(double x){    if(fabs(x) < eps)        return 0;    if(x > 0)        return 1;    return -1;}struct Point{    double x, y;    int id;    Point() {}    Point (double _x, double _y, int i):x(_x),y(_y),id(i) {}    bool operator <(const struct Point &tmp)const    {        if(double_cmp(x-tmp.x) == 0)            return double_cmp(y-tmp.y) < 0;        return double_cmp(x-tmp.x) < 0;    }    bool operator == (const struct Point &tmp)const    {        return double_cmp(x-tmp.x)==0&&double_cmp(y-tmp.y)==0;    }} p[MAXN],st[MAXN];double XMulti(Point a, Point b, Point c)///ac X ab{    return (c.x-a.x)*(b.y-a.y) - (b.x-a.x)*(c.y-a.y);}double dis(Point a, Point b){    return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));}double dis2(Point a, Point b){    return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y);}double dot(Point a, Point b, Point c)///点积 ab . ac{    double s1 = b.x-a.x;    double t1 = b.y-a.y;    double s2 = c.x-a.x;    double t2 = c.y-a.y;    return s1*s2 + t1*t2;}int ConvexHull(Point *p, int n, Point *st)///凸包{    sort(p, p+n);    n = unique(p, p+n)-p;///去重    int m = 0;    for(int i=0; i<n; i++)    {        while(m>1 && XMulti(st[m-2],p[i],st[m-1])<=0)            m--;        st[m++] = p[i];    }    int k = m;    for(int i=n-2; i>=0; i--)    {        while(m>k && XMulti(st[m-2],p[i],st[m-1])<=0)            m--;        st[m++] = p[i];    }    if(n > 1)        m--;    return m;}double rotating_calipers(Point *p, int n)///旋转卡壳求凸包的直径,平面最远的点对{    int k=1;    double ans = 0;    p[n]=p[0];    for(int i=0; i<n; i++)    {        while(fabs(XMulti(p[i+1],p[k],p[i]))<fabs(XMulti(p[i+1],p[k+1],p[i])))            k=(k+1)%n;        ans = max(ans,max(dis2(p[i],p[k]),dis2(p[i+1],p[k])));///注意是用的函数dis2    }    return ans;}int main(){    int n;    while(cin>>n)    {        for(int i=0; i<n; i++)        {            cin>>p[i].x>>p[i].y;        }        int m = ConvexHull(p, n, st);        double ans = rotating_calipers(st, m);        printf("%0.0lf\n",ans);    }    return 0;}




0 0