poj2187 最远点对(旋转卡壳)

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.

题意:

寻找平面最近点对

tip:

旋转卡壳,在凸包上顺时针转边,找最远点,叉积相当于面积,因为底相同,比较叉积就相当于比较了高(距离)了,于是就可以O(n) 因为同一方向转的时候,当前点离边比下一个点近,必然离刚才边绕着同一方向的下一跳边更近

凸包(0—top) 处理共线:<= 把中间共线部分去掉

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
int n,stack[50010],top;
const int maxn = 1e6+10;
const double eps = 1e-8;
struct Tpoint{
    double x;
    double y;
}list[50010];

int dblcmp(double p){
    if(fabs(p)<eps) return 0;
    return p>0?1:-1;
}

double Cross(Tpoint p0, Tpoint p1, Tpoint p2) {
    return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}
bool cmp(Tpoint p1, Tpoint p2){
    if(p1.y == p2.y)    return p1.x < p2.x;
    return p1.y < p2.y;
}
void Graham(){
    top = 1;
    for(int i = 0 ; i <= 2; i++)    stack[i] = i;
    for(int i=2;i<n;i++){
        while(top && dblcmp(Cross(list[stack[top-1]], list[stack[top]],list[i])<= 0)){
            top--;
        }
        stack[++top] = i;
    }
    int len = top;
    stack[++top] = n-2;
    for(int i = n-3; i >= 0 ; i--){
        while(top!=len && dblcmp(Cross(list[stack[top-1]], list[stack[top]],list[i])<= 0))    top--;
        stack[++top] = i;
    }
//    for(int i = 0; i <= top ; i++){
//        printf("x = %f  y = %f\n",list[stack[i]].x,list[stack[i]].y);
//    }
}

void init(){

    for(int i=0 ; i < n ; i++ )
        scanf("%lf%lf",&list[i].x,&list[i].y);
    //if(n == 2)
    memset(stack,0,sizeof(stack));
    sort(list, list+n, cmp);
}

double dis(int a,int b){
    return (list[stack[a]].x-list[stack[b]].x)*(list[stack[a]].x-list[stack[b]].x) + (list[stack[a]].y-list[stack[b]].y)*(list[stack[a]].y-list[stack[b]].y);
}

void sov(){
    int q = 1;
    double ans = 0;
    for(int i = 0  ; i <= top ; i++){
        while(Cross(list[stack[(q+1)%top]],list[stack[i]],list[stack[i+1]]) > Cross(list[stack[q]],list[stack[i]],list[stack[i+1]])){
            //ans = max(ans,max(dis(i,q),dis(i+1,q)));
            q = (q+1)%top ;
        }
        ans = max(ans,max(dis(i,q),dis(i+1,q)));
    }
    printf("%d\n",(int)ans);
}

int main(){
    while(~scanf("%d",&n)){
        init();
        Graham();
        sov();
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值