poj 2187【凸包求最远距离】

http://poj.org/problem?id=2187

这里很明显不能直接暴力(50000*50000)=。=

我们要知道最远的两点一定是凸包上的顶点,所以首先构造凸包,然后枚举找出最远距离,注意这里是平方!还要注意n==2的情况,直接求距离

#include<iostream>
#include<vector>
#include<map>
#include<stack>
#include<algorithm>
#include<queue>
#include<list>
#include<set>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<stdio.h>
#include<ctype.h>
#include<iomanip>

using namespace std;

#define LL long long
#define pi acos(-1)
#define N 50010
#define INF 9999999999
#define eps 1e-8

int n;
int top;

struct point
{
    int x,y;
}p[N],stck[N],p0;

int cross(point a,point b,point c)
{
    return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}

int dis(point a,point b)
{
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}

bool cmp(point a,point b)
{
     int t=cross(p0,a,b);
     return t>0 || (t==0 && dis(p0,a)<dis(p0,b));
}


void tubao()
{
    int i,j,k;
    k=0;
    for(i=1;i<n;i++)
        if(p[i].y<p[k].y || (p[i].y==p[k].y&&p[i].x<p[k].x))
            k=i;
    p0=p[k];
    p[k]=p[0];
    p[0]=p0;
    sort(p+1,p+n,cmp);
    top=1;
    stck[0]=p[0];
    stck[1]=p[1];
    for(i=2;i<n;i++)
    {
        while(top>0&&cross(stck[top-1],stck[top],p[i])<0)
        top--;
        stck[++top]=p[i];
    }
    top+=1;
    return ;
}

void solve()
{
    int max=0;
    int s;
    int i,j;
    for(i=0;i<top;i++)
    for(j=i+1;j<top;j++)
    {
        s=dis(stck[i],stck[j]);
        if(s>max)
            max=s;
    }
    printf("%d\n",max);
}

int main()
{
   // freopen("a.txt","r",stdin);
    while(scanf("%d",&n)!=EOF)
    {
        int i,j,k;
        for(i=0;i<n;i++)
            scanf("%d %d",&p[i].x,&p[i].y);
        if(n<3)
        {
            printf("%d\n",dis(p[0],p[1]));
            continue;
        }
        tubao();
        solve();
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值