POJ 2187 Beauty Contest

Description

Bessie, Farmer John’s prize cow, has just won first place in a bovinebeauty 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 farmsshare the same pair of coordinates.

Even though Bessie travels directly in a straight line between pairsof 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 foodto eat on each leg of her journey. Since Bessie refills her suitcase at every farm she visits, she wants to determine the maximum possibledistance 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.


【题目分析】
题目的大意就是求平面最远点对。暴力T,然后我采用RP法,每次随机800w组数据,结果换了好几个随机种子都是WA。(RP太差)然后写出来凸包,然后再枚举,正确性显然。这样就可以过了。


【代码】

#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <stack>
#include <cmath>
using namespace std;
struct P{
    int x,y;
}p[50001];
P s[50001];
int top=0;
int ans=0;
inline long long dis(P a,P b)
{return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);}
inline P operator-(const P &a,const P &b)
{return (P){a.x-b.x,a.y-b.y};}
inline long long operator*(const P &a,const P &b)
{return a.x*b.y-a.y*b.x;}
inline bool operator<(const P &a,const P &b)
{
    long long x=(a-p[1])*(b-p[1]);
    if (x==0) return dis(p[1],a)<dis(p[1],b);
    else return x<0;
}
int main()
{
    int n;
    scanf("%d",&n);
    for (int i=1;i<=n;++i) scanf("%d%d",&p[i].x,&p[i].y);
    int t=1;
    for (int i=1;i<=n;++i) if (p[i].y<p[t].y||(p[i].y==p[t].y&&p[i].x<p[t].x)) t=i;//扫描一遍,找到起始点 
    swap(p[1],p[t]);
    sort(p+2,p+n+1);
    s[++top]=p[1];s[++top]=p[2];
    for (int i=3;i<=n;++i)
    {
        while (top>=2&&(s[top]-s[top-1])*(p[i]-s[top-1])>=0) top--;
        s[++top]=p[i];
    }
    for (int i=1;i<=top;++i)
      for (int j=1;j<=top;++j)
        if (i!=j)
        {
            int now=dis(s[i],s[j]);
            ans=max(now,ans);
        }
    printf("%d\n",ans);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值