POJ 2187 Beauty Contest(旋转卡壳)

Beauty Contest
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 28053 Accepted: 8685

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


题意很简单,给一堆点,求这些点中距离最长的点的距离。范围比较大,肯定不能枚举。可以想到,距离最长的点肯定在最外围,也就是在凸包上。求最远点对有一个叫做旋转卡壳的算法,可以专门用来解决这种问题。

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
const double PI = acos(-1.0);
struct Point{
    double x,y;
    Point(double x=0,double y=0):x(x),y(y){}
};
typedef Point Vec;
//向量+向量 = 向量,点+向量 = 点
Vec operator +(Vec A,Vec B){return Vec(A.x+B.x,A.y+B.y);}
//点-点 = 向量
Vec operator -(Point A,Point B){return Vec(A.x-B.x,A.y-B.y);}
//向量*数 = 向量
Vec operator *(Vec A,double p){return Vec(A.x*p,A.y*p);}
//向量/数 = 向量
Vec operator /(Vec A,double p){return Vec(A.x/p,A.y/p);}
bool operator <(const Point& a,const Point& b){
    return a.x<b.x || (a.x == b.x && a.y<b.y);
}
const double EPS = 1e-10;
int dcmp(double x){
    if(fabs(x)<EPS) return 0;else return x<0? -1: 1;
}
bool operator == (const Point& a,const Point &b){
    return dcmp(a.x-b.x)==0 &&dcmp(a.y-b.y) == 0;
}
/*==========以上为基本定义============*/
double Dot(Vec A,Vec B){return A.x*B.x+A.y*B.y;}
double Length(Vec A){ return sqrt(Dot(A,A));}
double Angle(Vec A,Vec B){return acos(Dot(A,B)/Length(A)/Length(B));}
/*==========用点积算向量长度和两个向量夹角============*/
double Cross(Vec A,Vec B){return A.x*B.y - A.y*B.x;}
//ABC的三角形有向面积的两倍
double Area2(Point A,Point B,Point C){return Cross(B-A,C-A);}
//rad是弧度 逆时针旋转
Vec Rotate(Vec A,double rad){
    return Vec(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
}
//逆时针旋转90°的单位法向量
Vec Normal(Vec A){
    double L = Length(A);
    return Vec(-A.y/L,A.x/L);
}
/*=====以上为叉积的基本运算=====*/
//P+tv和Q+tw两条直线的交点,确保有唯一交点
Point GetlineIntersection(Point P,Vec v,Point Q,Vec w){
    Vec u = P-Q;
    double t = Cross(w,u)/Cross(v,w);
    return P+v*t;
}
//点到直线的距离
double DistanceToLine(Point P,Point A,Point B){
    Vec v1 = B-A,v2 = P-A;
    return fabs(Cross(v1,v2)/Length(v1));//不取绝对值那么得到的是有向距离
}
//点到线段的距离
double DistanceToSegment(Point P,Point A,Point B){
    if(A == B)return Length(P-A);
    Vec v1 = B - A, v2 = P - A, v3 = P - B;
    if(dcmp(Dot(v1,v2))<0)return Length(v2);
    else if(dcmp(Dot(v1,v3)>0)) return Length(v3);
    else return fabs(Cross(v1,v2))/Length(v1);
}
//点在直线上的投影
Point GetLineProjection(Point P,Point A,Point B){
    Vec v = B - A;
    return A + v*(Dot(v,P-A) / Dot(v,v));
}
//判断两条线段是否相交 此处必须为规范相交
bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2){
    double c1 = Cross(a2-a1,b1-a1),c2 = Cross(a2-a1,b2-a1);
    double c3 = Cross(b2-b1,a1-b1),c4 = Cross(b2-b1,a2-b1);
    return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0;
}
//如果允许端点相交,则用以下代码,判断一个点是否在一条线段上
bool OnSegment(Point p,Point a1,Point a2){
    return dcmp(Cross(a1-p,a2-p)) == 0 && dcmp(Dot(a1-p,a2-p))<0;
}
/*=========以上为点和直线,直线和直线关系的内容========*/
//多边形有向面积
double PolygonArea(Point* p,int n){
    double area = 0;
    for(int i=1;i<n-1;i++)
        area += Cross(p[i] - p[0],p[i+1]-p[0]);
    return area/2;
}
//判断一个浮点数是否为整数
double isint(double x){
    return fabs(x - (int)(x+0.5))<EPS;
}
//凸包
vector<Point> ConvexHull(vector<Point> p){
    sort(p.begin(),p.end());
    //删除重复点
    p.erase(unique(p.begin(),p.end()),p.end());
    int n = p.size();
    vector<Point> ch(n+1);
    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--;
    ch.resize(m);
    return ch;
}
int dist2(Point a,Point b){
    return (int)(a.x-b.x)*(a.x-b.x)+(int)(a.y-b.y)*(a.y-b.y);
}
//旋转卡壳
int diameter2(vector<Point>points){
    vector<Point> p = ConvexHull(points);
    int n = p.size();
    if(n == 1)return 0;
    if(n == 2)return dist2(p[0],p[1]);
    p.push_back(p[0]);
    int ans = 0;
    for(int u=0,v = 1;u<n;u++){
        // 当Area(p[u], p[u+1], p[v+1]) <= Area(p[u], p[u+1], p[v])时停止旋转
        // 即Cross(p[u+1]-p[u], p[v+1]-p[u]) - Cross(p[u+1]-p[u], p[v]-p[u]) <= 0
        // 根据Cross(A,B) - Cross(A,C) = Cross(A,B-C)
        // 化简得Cross(p[u+1]-p[u], p[v+1]-p[v]) <= 0
        while(1){
            int diff = Cross(p[u+1]-p[u],p[v+1]-p[v]);
            if(diff<=0){
                ans = max(ans,dist2(p[u],p[v]));
                if(diff == 0) ans = max(ans,dist2(p[u],p[v+1]));
                break;
            }
            v = (v+1)%n;
        }
    }
    return ans;
}
int main()
{
    int n;
    while(scanf("%d",&n)==1){
        double x,y;
        vector<Point> p;
        for(int i=1;i<=n;i++){
            scanf("%lf%lf",&x,&y);
            p.push_back(Point(x,y));
        }
        int ans = diameter2(p);
        printf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值