首页 技术 正文
技术 2022年11月15日
0 收藏 760 点赞 3,569 浏览 2730 个字

题面

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;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,964
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,486
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,331
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,114
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,747
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,781