Beauty Contest POJ - 2187 (旋转卡壳求凸包直经)

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) 

思路:先graham扫描法求出凸包(注意取同一直线上的点),接着就是旋转卡壳求直径的模板题了。旋转卡壳第一题。

算法描述 
1. 计算多边形y方向的端点,我们称之为yminymin和ymaxymax 
2. 通过yminymin和ymaxymax 构造两条水平切线。由于他们已经是一对对踵点,计算他们之间的距离并维护一个当前的最大值。 
3. 同时旋转两条线直到其中一条与多边形的一边重合。 
4. 一个新的对踵点对此时产生。计算新的距离,并和当前的最大值比较,大于当前最大值则更新。 
5. 重复步骤3和步骤4的过程直到再次产生对踵点对。 
6. 输出确定最大直径的对踵点对。

代码:

//poj 2187
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>
#include <stack>
using namespace std;
typedef long long ll;
const int maxn=1e5+9;
const int mod=1e9+7;
#define inf 0x3f3f3f3f
const double eps=1e-8;
int n,top;
double ans;
double sqr(double x)
{
 
	return x*x;
}
struct P{
	double x,y;
	P(){}
	P(double _x,double _y):x(_x),y(_y){}
	friend P operator +(P a,P b){
		return P(a.x+b.x,a.y+b.y);
	}
	friend P operator -(P a,P b){
		return P(a.x-b.x,a.y-b.y);
	}
	friend double operator*(P a,P b){
		return a.x*b.y-a.y*b.x;
	}
	friend double operator/(P a,P b){
		return a.x*b.x+a.y*b.y;
	}
	friend bool operator==(P a,P b){
		return fabs(a.x-b.x)<eps&&fabs(a.y-b.y)<eps;
	}
	friend bool operator!=(P a,P b){
		return !(a==b);
	}
	friend bool operator<(P a,P b){
		if(fabs(a.y-b.y)<eps)return a.x<b.x;
		return a.y<b.y;
	}
	friend double dis2(P a){
		return sqr(a.x)+sqr(a.y);
	}
	friend void print(P a){
		printf("%.2lf %.2lf\n",a.x,a.y);
	}
}p[50005],q[50005];
bool cmp(P a,P b)
{
    if(fabs((a-p[1])*(b-p[1]))<eps)
    {
        return dis2(a-p[1])<dis2(b-p[1]);
    }
    return (a-p[1])*(b-p[1])>0;
}
void graham()
{
    for(int i=1;i<=n;i++)
    {
        if(p[i]<p[1]) swap(p[1],p[i]);
    }
    sort(p+2,p+1+n,cmp);
    q[++top]=p[1],q[++top]=p[2];
    for(int i=3;i<=n;i++)
    {
        while(top>1&&((q[top]-q[top-1])*(p[i]-q[top-1])<eps))
        {
            top--;
        }
        q[++top]=p[i];
    }
}
void RC()
{
    q[top+1]=q[1];
    int now=2;
    for(int i=1;i<=top;i++)
    {
        while(((q[i+1]-q[i])*(q[now]-q[i]))<((q[i+1]-q[i])*(q[now+1]-q[i])))
        {
            now++;
            if(now==top+1) now=1;
        }
        ans=max(ans,dis2(q[now]-q[i]));
    }
}
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    scanf("%d",&n);
        top=0;
        ans=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%lf%lf",&p[i].x,&p[i].y);
        }
        graham();
        RC();
        printf("%d\n",int(ans));
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值