POJ 2187 Beauty Contest(凸包 + 旋转卡壳)

来源:互联网 发布:sql server error 26 编辑:程序博客网 时间:2024/05/02 01:13
Beauty Contest
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 31180 Accepted: 9670

Description

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) 

解题思路:
旋转卡壳求凸多边形的直径。
  1. 计算多边形 y 方向上的端点。 我们称之为 ymin 和 ymax 。
  2. 通过 ymin 和 ymax 构造两条水平切线。 由于他们已经是一对对踵点, 计算他们之间的距离并维护为一个当前最大值。
  3. 同时旋转两条线直到其中一条与多边形的一条边重合。
  4. 一个新的对踵点对此时产生。 计算新的距离, 并和当前最大值比较, 大于当前最大值则更新。
  5. 重复步骤3和步骤4的过程直到再次产生对踵点对 (ymin,ymax) 。
  6. 输出确定最大直径的对踵点对。
#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <cmath>#include <vector>#include <queue>#include <set>#include <stack>#include <algorithm>#define LL long longusing namespace std;const int MAXN = 50000 + 10;struct Point{    int x, y;    Point(int x = 0, int y = 0) : x(x), y(y) {}} p[MAXN];int n;typedef Point Vector;Vector operator + (Vector A, Vector B){    return Vector(A.x + B.x, A.y + B.y);}Vector operator - (Vector A, Vector B){    return Vector(A.x - B.x, A.y - B.y);}double operator * (Vector A, Vector B){    return A.x * B.y - B.x * A.y;}bool operator < (const Point& a, const Point& b){    return a.x < b.x || (a.x == b.x && a.y < b.y);}int Cross(Vector A, Vector B){    return A.x * B.y - A.y * B.x;}Point ch[MAXN];int ConvexHull(){    sort(p, p + n);    int m = 0;    for(int i=0; i<n; i++)    {        while(m > 1 && Cross(ch[m-1] - ch[m-2], p[i] - ch[m-2]) <= 0) m--;        ch[m++] = p[i];    }    int k = m;    for(int i=n-2; i>=0; i--)    {        while(m > k && Cross(ch[m-1] - ch[m-2], p[i] - ch[m-2]) <= 0) m--;        ch[m++] = p[i];    }    if(n > 1) m--;    return m;}int dis(Point a, Point b){    return (a.x - b.x)*(a.x - b.x) + (a.y - b.y) * (a.y - b.y);}bool cmp(Point a, Point b){    if(a.y == b.y) return a.x < b.x;    return a.y < b.y;}bool cmp2(Point a, Point b){    int x1 = a.x - ch[0].x;    int x2 = b.x - ch[0].x;    int y1 = a.y - ch[0].y;    int y2 = b.y - ch[0].y;    if(x1 * y2 == x2 * y1) return dis(a, ch[0]) < dis(b, ch[0]);    return x1 * y2 > x2 * y1;}int vis[MAXN];int main(){    while(scanf("%d", &n)!=EOF)    {        for(int i=0; i<n; i++)        {            scanf("%d%d", &p[i].x, &p[i].y);        }        int m = ConvexHull();        sort(ch, ch + m, cmp);        sort(ch + 1, ch + m, cmp2);      //  for(int i=0; i<m; i++) cout << ch[i].x << ' ' << ch[i].y << endl;        int Min = 0, Max = 0;        for(int i=1; i<m; i++)        {            if(ch[i].y > ch[Max].y) Max = i;            if(ch[i].y < ch[Min].y) Min = i;        }        int len = dis(ch[Min], ch[Max]);        int s1 = Min, s2 = Max;        memset(vis, 0, sizeof(vis));        while(vis[s1] == 0 || vis[s2] == 0)        {            vis[s1] = vis[s2] = 1;            int x1 = ch[s2].x - ch[(s2 +1) % m].x;            int x2 = ch[(s1 + 1) % m].x - ch[s1].x;            int y1 = ch[s2].y - ch[(s2 + 1) % m].y;            int y2 = ch[(s1 + 1) % m].y - ch[s1].y;            if(x1 *y2 > x2 * y1) s2 = (s2 + 1) % m;            else s1 = (s1 + 1) % m;            len = max(len, dis(ch[s1], ch[s2]));        }        printf("%d\n", len);    }    return 0;}


0 0
原创粉丝点击