您的位置:首页 > 其它

2017-04-04 在校训练T3 选美比赛 contest

2017-04-05 15:53 162 查看
选美比赛 contest

Description

约翰农夫的母牛贝西,刚刚在牛选美比赛赢得第一名,赢得了“世界牛小姐”称号。为了传播善意,贝西将参观世界各地的农场N(2 < = N < = 50000)。为简单起见,世界将被表示成一个二维平面,其中每个农场位于一对整数坐标(x,y),其值在-2000000~2000000范围内。没有两个农场共享相同的坐标。

尽管贝西旅行中走农场之间的直线,但一些农场之间的距离可能很大,所以她想带着一个手提箱,足够她装食物,即使最远的距离。她想确定旅行中最大可能的距离,这样她才能决定箱子的大小。帮助贝西计算所有成对的农场间最大距离。(最远点对)

Input

第一行一个整数N

以下N行,每行两个整数,表示一个农场的坐标。

Output

一行,一个整数表示最远一对农场距离的平方。

Sample Input

4

0 0

0 1

1 1

1 0

Sample Output

2

本题是先叉乘,然后旋(xuán)转(zhuǎn)卡(qiǎ)壳(ké)。

没了。

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std ;
const int maxn=50010;

int n,top;
long long ans;
typedef struct NODE{
long long x,y;
NODE (){}
NODE (long long a,long long b){
x=a,y=b;
}
}N;
N p[maxn],stack[maxn];
N operator -(N a,N b){
return N (a.x-b.x,a.y-b.y);
}
long long operator * (N a,N b){
return a.x*b.y-a.y*b.x;
}
long long sqr (long long x) {return x*x;}
long long dist (N x,N y){
return sqr (x.x-y.x)+sqr(x.y-y.y);
}
bool operator <(N a,N b){
long long cnt=((a-p[1])*(b-p[1]));
return cnt>0 || (cnt==0&&dist(p[1],a)<dist (p[1],b));
}
void init (){
scanf ("%d",&n);
int i,k=1;
for (i=1;i<=n;i++){
scanf ("%lld %lld",&p[i].x,&p[i].y);
if (p[i].x < p[k].x || (p[i].x==p[k].x&&p[i].y<p[k].y)) k=i;
}
swap (p[1],p[k]);
sort (p+2,p+n+1);
}
void graham(){//构建凸包
stack[++top]=p[1];
stack[++top]=p[2];
int i;
for(i=3;i<=n;i++){
while(top>1&&(stack[top]-stack[top-1])*(p[i]-stack[top-1])<=0) top--;
stack[++top]=p[i];
}
}
void xzkk(){
stack[top+1]=stack[1];
int now=2,i;
for(i=1;i<=top;i++){
while((stack[i+1]-stack[i])*(stack[now]-stack[i])<(stack[i+1]-stack[i])*(stack[now+1]-stack[i])){
now++;
if(now==top+1) now=1;
}
ans=max(ans,dist(stack[now],stack[i]));
}
}
int main (){
init ();
graham ();
xzkk ();
cout <<ans;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: