题面

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)

题解

题目大意:给出若干个农场的坐标,求出相距最远的农场的直线距离的平方。

题解:

首先扫描法求出凸包,旋转卡壳求出最大值即可


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std;
#define MAX 50010
#define INF 1000000000
#define rg register
inline int read()
{
int x=0,t=1;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-'){t=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
return x*t;
}
struct Node
{
int x,y;
}p[MAX],p0,S[MAX];
int n,top,T;
inline bool cmp(Node a,Node b)
{
rg double A=atan2(a.y-p0.y,a.x-p0.x);
rg double B=atan2(b.y-p0.y,b.x-p0.x);
if(A!=B)return A<B;
else return a.x<b.x;
}
inline long long chaji(int x1,int y1,int x2,int y2)//计算叉积
{
return (1LL*x1*y2-1LL*x2*y1);
}
inline long long Compare(Node a,Node b,Node c)//计算向量
{
return chaji((b.x-a.x),(b.y-a.y),(c.x-a.x),(c.y-a.y));
}
inline void Find()//寻找凸包
{
p0=(Node){INF,INF};
rg int k=0;
for(rg int i=0;i<n;++i)//找到最下方的点
if(p0.y>p[i].y||(p0.y==p[i].y&&p0.x>p[i].x))
p0=p[i],k=i;
swap(p[k],p[0]);
sort(&p[1],&p[n],cmp);//关于最下方的点排序
S[0]=p[0];S[1]=p[1];
top=1;//栈顶
for(rg int i=2;i<n;)//求出凸包
{
if(top&&Compare(S[top-1],p[i],S[top])>=0) top--;
else S[++top]=p[i++];
}
}
inline long long Dis(Node a,Node b)//计算两点的距离的平方和
{
return 1LL*(a.x-b.x)*(a.x-b.x)+1LL*(a.y-b.y)*(a.y-b.y);
}
long long GetMax()//求出直径
{
rg long long re=0;
if(top==1)//仅有两个点
return Dis(S[0],S[1]);
S[++top]=S[0];//把第一个点放到最后
int j=2;
for(int i=0;i<top;++i)//枚举边
{
while(Compare(S[i],S[i+1],S[j])<Compare(S[i],S[i+1],S[j+1]))
j=(j+1)%top;
re=max(re,max(Dis(S[i],S[j]),Dis(S[i+1],S[j])));
}
return re; }
int main()
{
n=read();
for(int i=0;i<n;++i)
{
p[i].x=read();p[i].y=read();
}
long long ans=INF,ss;
Find();
ans=GetMax();
cout<<ans<<endl;
return 0;
}

POJ 2187 Beauty Contest(凸包,旋转卡壳)的更多相关文章

  1. POJ 2187 - Beauty Contest - [凸包+旋转卡壳法][凸包的直径]

    题目链接:http://poj.org/problem?id=2187 Time Limit: 3000MS Memory Limit: 65536K Description Bessie, Farm ...

  2. POJ 2187 Beauty Contest [凸包 旋转卡壳]

    Beauty Contest Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 36113   Accepted: 11204 ...

  3. POJ 2187 Beauty Contest【旋转卡壳求凸包直径】

    链接: http://poj.org/problem?id=2187 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

  4. poj 2187 Beauty Contest(凸包求解多节点的之间的最大距离)

    /* poj 2187 Beauty Contest 凸包:寻找每两点之间距离的最大值 这个最大值一定是在凸包的边缘上的! 求凸包的算法: Andrew算法! */ #include<iostr ...

  5. 【POJ】2187 Beauty Contest(旋转卡壳)

    http://poj.org/problem?id=2187 显然直径在凸包上(黑书上有证明).(然后这题让我发现我之前好几次凸包的排序都错了QAQ只排序了x轴.....没有排序y轴.. 然后本题数据 ...

  6. POJ 2187 Beauty Contest 凸包

    Beauty Contest Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 27276   Accepted: 8432 D ...

  7. Beauty Contest 凸包+旋转卡壳法

    Beauty Contest Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 27507   Accepted: 8493 D ...

  8. 【POJ 2187】Beauty Contest 凸包+旋转卡壳

    xuán zhuǎn qiǎ ké模板题 是这么读吧(≖ ‿ ≖)✧ 算法挺简单:找对踵点即可,顺便更新答案. #include<cstdio> #include<cstring&g ...

  9. poj 2187 Beauty Contest 凸包模板+求最远点对

    题目链接 题意:给你n个点的坐标,n<=50000,求最远点对 #include <iostream> #include <cstdio> #include <cs ...

  10. poj 2187 Beauty Contest(二维凸包旋转卡壳)

    D - Beauty Contest Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

随机推荐

  1. javascript Map和Set

    Map和Set JavaScript的默认对象表示方式{}可以视为其他语言中的Map或Dictionary的数据结构,即一组键值对. 但是JavaScript的对象有个小问题,就是键必须是字符串.但实 ...

  2. shell编程之BASH变量(2)

    变量命名规范 在bash中,变量的默认类型都是字符串型,定义 name = 'kk' 变量分类 用户自定义变量.变量自定义的 环境变量:这种变量中主要保存的是和系统操作环境相关的数据.变量可以自定义, ...

  3. 百度编辑器Ueditor增加字体的修改方法

    http://www.jb51.net/article/109896.htm Ueditor本身自带11种字体  使用过程中这11种字体往往不能满足我们的需求,现在我要添加"仿宋" ...

  4. 使用CentOS7配置Squid代理

    其实之前配过一个squid,只是由于太懒,网上随便搜了一个教程,用了默认端口并且没有添加用户认证.某天不幸的被爬虫扫到,被用来发了半个月的垃圾邮件..直到有一天登录邮箱,看到了一大坨警告邮件,才意识到 ...

  5. 织梦去除tag标签url中的问号

    找到文件 include\taglib\tag.lib.php  大概87行 把 $row['link'] = $cfg_cmsurl."/tags.php?/".urlencod ...

  6. jquery validate 动态增加删除验证规则(转载)

    页面加载完成初始化form validate $("#user_regForm").validate({ errorPlacement: function(error, eleme ...

  7. IM开发基础知识补课:正确理解前置HTTP SSO单点登陆接口的原理

    1.前言 一个安全的信息系统,合法身份检查是必须环节.尤其IM这种以“人”为中心的社交体系,身份认证更是必不可少. 一些PC时代小型IM系统中,身份认证可能直接做到长连接中(也就是整个IM系统都是以长 ...

  8. Netbeans简要配置许可证信息

    <#if licenseFirst??>${licenseFirst}</#if>${licensePrefix}Copyright (C) <2017>  < ...

  9. POJ - 1456 贪心+并查集

    做法一:直接贪心,按照利润排序,然后直接尽量给每个活动安排到最晚的时间即可.时间复杂度O(n * d)当d都为10000时,很容易超时.由于这题数据比较水,所有贪心未超时. AC代码 #include ...

  10. Spring Data JPA 入门Demo

    什么是JPA呢? 其实JPA可以说是一种规范,是java5.0之后提出来的用于持久化的一套规范:它不是任何一种ORM框架,在我看来,是现有ORM框架在这个规范下去实现持久层. 它的出现是为了简化现有的 ...