poj 2187 Beauty Contest

最大的距离一定在这些农场形成的凸包上, 求出凸包 找出凸包顶点上的最长距离

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn = 5e4+5;

struct node
{
    double x, y;
    double rad;
}p[maxn], tp[maxn];

bool cmp1(node a, node b)
{
    if(a.y == b.y) return a.x < b.x;
    return a.y < b.y;
}

bool cmp2(node a, node b)
{
    if(a.rad == b.rad) return a.x < b.x;
    return a.rad < b.rad;
}

double rad_point(node a, node b)
{
    return atan2((b.y-a.y), (b.x-a.x));
}

bool tp_point(node a, node b, node c)
{
    int m = (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
    if(m < 0) return true;
    return false;
}

int main()
{
    int n;
    while(scanf("%d", &n) != EOF)
    {
        for(int i = 0; i < n; i++) scanf("%lf%lf", &p[i].x, &p[i].y);
        sort(p, p+n, cmp1);
        for(int i = 0; i < n; i++) p[i].rad = rad_point(p[0], p[i]);
        sort(p+1, p+n, cmp2);

        tp[0] = p[0];
        tp[1] = p[1];
        tp[2] = p[2];
        int top = 2;
        for(int i = 3; i < n; i++)
        {
            while(top >= 1 && tp_point(tp[top-1], tp[top], p[i])) top--;
            tp[++top] = p[i];
        }
        top++;
        int sum = -1;
        for(int i = 0; i < top; i++)
            for(int j = i+1; j < top; j++)
        {
            int m = (tp[i].x-tp[j].x)*(tp[i].x-tp[j].x)+(tp[i].y-tp[j].y)*(tp[i].y-tp[j].y);
            if(sum < m) sum = m;
        }
        printf("%d\n", sum);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值