hdu 2187(凸包直径 1.枚举 2.旋转卡壳)

Beauty Contest
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 33115   Accepted: 10278

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

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)

Source

题意:求最远的两个点。

分析:相隔最远的点肯定在凸包上,于是我们用凸包然后枚举,由于凸包上的点并不多,所以这是O(n*n)的枚举不会爆(141MS,我自己都没想到这么快),但是有旋转卡壳的方法,学习了之后再来补解法。

用处很广的旋转卡壳:http://blog.csdn.net/ACMaker

方法一:枚举(141MS)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

const int N = 50005;
struct Point{
    int x,y;
}p[N],Stack[N];
int n;

int mult(Point a,Point b,Point c){
    return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
int dis(Point a,Point b){
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int cmp(Point a,Point b){
    if(mult(a,b,p[0])>0) return 1;
    if(mult(a,b,p[0])==0&&dis(b,p[0])>dis(a,p[0])) return 1;
    return 0;
}
int Graham(){
    sort(p+1,p+n,cmp);
    int top = 2;
    Stack[0]=p[0];
    Stack[1]=p[1];
    Stack[2]=p[2];
    for(int i=3;i<n;i++){
        while(top>=1&&mult(p[i],Stack[top],Stack[top-1])>=0){
            top--;
        }
        Stack[++top]=p[i];
    }
    return top;
}
int main()
{
    while(scanf("%d",&n)!=EOF){
        for(int i=0;i<n;i++){
            scanf("%d%d",&p[i].x,&p[i].y);
        }
        int k = 0;
        for(int i=1;i<n;i++){
            if(p[k].y>p[i].y||(p[k].y==p[i].y)&&(p[k].x>p[i].x)){
                k=i;
            }
        }
        swap(p[0],p[k]);
        //printf("%d %d\n",p[0].x,p[0].y);
        int top = Graham();
        int ans =0;
        for(int i=0;i<=top;i++){
            for(int j=i+1;j<=top;j++){
                if(ans<dis(Stack[i],Stack[j])){
                    ans = dis(Stack[i],Stack[j]);
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

 方法二:旋转卡壳 (94MS)

图引自:http://www.cnblogs.com/Booble/archive/2011/04/03/2004865.html

(图一)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

const int N = 50005;
struct Point{
    int x,y;
}p[N],Stack[N];
int n;

int mult(Point a,Point b,Point c){
    return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
int dis(Point a,Point b){
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int cmp(Point a,Point b){
    if(mult(a,b,p[0])>0) return 1;
    if(mult(a,b,p[0])==0&&dis(b,p[0])>dis(a,p[0])) return 1;
    return 0;
}
int Graham(){
    sort(p+1,p+n,cmp);
    int top = 2;
    Stack[0]=p[0];
    Stack[1]=p[1];
    Stack[2]=p[2];
    for(int i=3;i<n;i++){
        while(top>=1&&mult(p[i],Stack[top],Stack[top-1])>=0){
            top--;
        }
        Stack[++top]=p[i];
    }
    return top;
}
int rotating_calipers(int top)//旋转卡壳
{
    int q=1,ans=0,p;
    Stack[++top]=Stack[0]; ///将最后一个点和第一点的边也要算进去,于是多加一个点表示第一个点
    for(p=0; p<top; p++)
    {
     ///找到以Stack[p] 和 Stack[p+1]这两个点为底边的最大三角形 (叉积之积为三角形有向面积)(图1)
while(mult(Stack[p],Stack[p+1],Stack[q+1])>mult(Stack[p],Stack[p+1],Stack[q])) q=(q+1)%top;
     ///在底边的两个点中选择与顶点距离大的 ans
=max(ans,max(dis(Stack[p],Stack[q]),dis(Stack[p+1],Stack[q+1]))); } return ans; } int main() { while(scanf("%d",&n)!=EOF){ for(int i=0;i<n;i++){ scanf("%d%d",&p[i].x,&p[i].y); } int k = 0; for(int i=1;i<n;i++){ if(p[k].y>p[i].y||(p[k].y==p[i].y)&&(p[k].x>p[i].x)){ k=i; } } swap(p[0],p[k]); //printf("%d %d\n",p[0].x,p[0].y); int top = Graham(); int ans =rotating_calipers(top); printf("%d\n",ans); } return 0; }

 

posted @ 2016-04-25 18:07  樱花庄的龙之介大人  阅读(569)  评论(0编辑  收藏  举报