POJ 2187 Beauty Contest (旋转卡壳法)

来源:互联网 发布:淘宝买new3ds 编辑:程序博客网 时间:2024/06/05 07:45
Beauty Contest
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 33457 Accepted: 10367

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) 


凸包:

题意:

平面上有N个牧场。i号牧场的位置在格点(xi, yi),所有牧场的位置互不相同。请计算距离最远的两个牧场间的距离,输出最远距离的平方。

分析:

由于在限制时间内无法完全枚举所有点对并取距离的最大值,需要避免计算一些不必要的点对。如果某个点在另外三个点组成的三角形的内部,那么他就不可能属于最远的点对,因而可以删去。这样,最后需要考虑的点,就只剩下不在任意三个点组成的三角形内部的,所给点集中最外围的点了。这些最外围的点的集合,就是包围原点集的最小凸多边形的顶点组成的集合,成为原点集的凸包。因为顶点的坐标限定为整数,坐标值的范围不超过M的凸多边形的顶点数只有O(根号M)个,所以只要枚举凸包上的所有点对并计算距离就可以求得最远点对了。

 

求凸包的算法有很多,要求n个点集对应的凸包,只要O(nlogn)的时间。这里给大家介绍一种比较容易实现的基于平面扫描法的Graham扫描算法。

首先,把点集按x坐标到y坐标的字典序升序排序。那么排序后的第一个和最后一个点必然是凸包上的顶点,他们之间的部分可以分成上下两条链分别求解。求下侧的链时只要从小到大处理排序后的点列,逐步构造凸包。在构造过程中的凸包末尾加上新的顶点后,可能会破坏凸性,此时只要将凹的部分的点从末尾出去就好了。求上侧的链也是一样地从大到小处理即可。排序的复杂度为O(nlogn),剩余部分的处理的复杂度为O(n)。


事实上,即使坐标方位变大这道题也能求解。为此,我们需要再次用到凸包的性质。

假设最远点对是p和q,那么p就是点集中(p-q)方向最远的点,而q是点集中(q-p)方向最远的点。因此,可以按照逆时针逐渐改变方向,同时枚举出所有对于某个方向上最远的点对,那么最远点对一定也包含在其中。在逐渐改变方向的过程中,对踵点对只有在方向等于凸包某条边的法线方向时发生变化,此时点将向凸包上对应的相邻点移动。令方向逆时针旋转一圈,那么对踵点对也在凸包上转了一周,这样就可以在凸包顶点数的线代时间内求得最远点对。像这样,在凸包上旋转扫描的方向又叫做旋转卡壳法。


#include <cstdio>#include <vector>#include <algorithm>#include <cstring>using namespace std;const int maxn = 50000 + 10;typedef int type_xy;struct P{type_xy x, y;P() {}P(type_xy x, type_xy y) : x(x), y(y) {}P operator + (P p){ return P(x + p.x, y + p.y); }P operator - (P p){ return P(x - p.x, y - p.y); }P operator * (type_xy d){ return P(x*d, y*d); }bool operator < (const P& a) const{if (x != a.x) return x < a.x;else return y < a.y;}type_xy dot(P p) { return x*p.x + y*p.y; }type_xy det(P p) { return x*p.y - y*p.x; }};int N;P ps[maxn];//字典序比较bool cmp_x(const P& p, const P& q){    if (p.x != q.x)        return p.x < q.x;    return p.y < q.y;}//求凸包vector<P> convex_hull(P* ps, int n){    sort(ps, ps + n, cmp_x);    int k = 0;          //凸包的顶点数    vector<P> qs(n * 2);        //构造中的凸包    //构造凸包的下侧    for (int i = 0; i < n; i++){        while (k > 1 && (qs[k - 1] - qs[k - 2]).det(ps[i] - qs[k - 1]) <= 0)            k--;        qs[k++] = ps[i];    }    //构造凸包的上侧    for (int i = n - 2, t = k; i >= 0; i--){        while (k > t && (qs[k - 1] - qs[k - 2]).det(ps[i] - qs[k - 1]) <= 0)            k--;        qs[k++] = ps[i];    }    qs.resize(k - 1);    return qs;}//距离的平方double dist(P p, P q){    return (p - q).dot(p - q);}void solve(){    vector<P> qs = convex_hull(ps, N);    int n = qs.size();    if (n == 2){         //特别处理凸包退化的情况        printf("%.0f\n", dist(qs[0], qs[1]));        return;    }    int i = 0, j = 0;           //某个方向上的对踵点对    //求出x轴方向上的对踵点对    for (int k = 0; k < n; k++){        if (!cmp_x(qs[i], qs[k]))            i = k;        if (cmp_x(qs[j], qs[k]))            j = k;    }    double res = 0;    int si = i, sj = j;    while (i != sj || j != si){     //将方向逐步旋转180度        res = max(res, dist(qs[i], qs[j]));        //判断先转到边i-(i+1)的法线方向还是边j-(j+1)的法线方向        if ((qs[(i + 1) % n] - qs[i]).det(qs[(j + 1) % n] - qs[j]) < 0)            i = (i + 1) % n;        //先转到边i-(i+1)的法线方向        else            j = (j + 1) % n;        //先转到边j-(j+1)的法线方向    }    printf("%.0f\n", res);}int main(){    while (scanf("%d", &N) != EOF){        for (int i = 0; i < N; i++){            scanf("%d%d", &ps[i].x, &ps[i].y);        }        solve();    }    return 0;}


0 0