求凸包上最大距离 poj2187

来源:互联网 发布:高仿5s土豪金淘宝 编辑:程序博客网 时间:2024/04/20 10:55
Beauty Contest
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 38389 Accepted: 11866

Description

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

40 00 11 11 0

Sample Output

2

Hint

Farm 1 (0, 0) and farm 3 (1, 1) have the longest distance (square root of 2) 

Source

USACO 2003 Fall

    就是求最大距离。。先求出凸包,这里的排序方法和我写的上一篇不太一样,这里是根据最左下角已确定的点与之叉乘是否大于零若共线则优先排距离小的,留个坑,我的再想想为啥

   之后求每一对对踵点间最大距离,(其实是每个点都找一遍,利用三角形面积,等底,求最大高)稍优化的暴力。。

  

#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>#include<vector>#include<iostream>#define N 50005using namespace std;struct node{int x,y;}a[N],point;int cheng(node a,node b,node c){return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);}int dis(node a,node b){return (b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y);}int cmp(node b,node c){int t=cheng(a[1],b,c);if(t==0)   return dis(a[1],b)<dis(a[1],c);return t>0;}int n,vis[N],head=-1,zhan[N],ans;int main(){scanf("%d",&n);point.x=10005,point.y=10005;int j;for(int i=1;i<=n;i++){scanf("%d%d",&a[i].x,&a[i].y);    if(a[i].y<point.y)         point=a[i],j=i;    else       if(a[i].y==point.y&&a[i].x<point.x)            point=a[i],j=i;}if(n==2){printf("%d\n",dis(a[1],a[2]));return 0;}a[j]=a[1];a[1]=point;sort(a+2,a+n+1,cmp);zhan[++head]=1;zhan[++head]=2;for(int i=3;i<=n;i++){    while(head>0&&cheng(a[zhan[head-1]],a[zhan[head]],a[i])<=0)head--;    zhan[++head]=i;}j=1;zhan[++head]=1;for(int i=0;i<head;i++){while(cheng(a[zhan[i]],a[zhan[i+1]],a[zhan[j+1]])>cheng(a[zhan[i]],a[zhan[i+1]],a[zhan[j]]))   j=(j+1)%head;ans=max(ans,dis(a[zhan[i]],a[zhan[j]]));}printf("%d\n",ans);}

原创粉丝点击