POJ-2187--Beauty Contest---凸包
2017-07-28 10:58
351 查看
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
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)
题意:给你N个点,然后找距离最远的点的距离然后平方。
简单凸包模板题,直接上代码。
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)
题意:给你N个点,然后找距离最远的点的距离然后平方。
简单凸包模板题,直接上代码。
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<iomanip> #define sf scanf #define pf printf using namespace std; const int M=50000+10; const int MAX=0x3f3f3f3f; double xx=MAX,yy=MAX; struct point { double x; double y; } s[M+50],p[M+50]; double cross(double x1,double y1,double x2,double y2) { return x1*y2-x2*y1; } double compare(point a,point b,point c) { return cross(b.x-a.x,b.y-a.y,c.x-a.x,c.y-a.y); } bool cmp(point a,point b) { point c; c.x=0; c.y=0; if(compare(a,b,c)==0) return a.x<b.x; else return compare(a,b,c)>0; } double Caculate(double x1,double y1,double x2,double y2) { return (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1); }//这里的函数用来计算距离 int main() { int n; sf("%d",&n); int i; int t=0; for(i=0; i<=n-1; i++) { sf("%lf%lf",&p[i].x,&p[i].y); if(p[i].y<yy) { xx=p[i].x; yy=p[i].y; t=i; } } p[t]=p[0]; sort(p+1,p+n,cmp); int top=1; s[0].x=xx; s[0].y=yy; s[1]=p[1]; for(i=2; i<=n-1;) { if(top!=0&&compare(s[top-1],s[top],p[i])<0) top--; else s[++top]=p[i++]; } int Max=0; for(i=0; i<=top; i++) { for(int j=i+1; j<=top; j++) { if(Caculate(s[i].x,s[i].y,s[j].x,s[j].y)>Max) Max=Caculate(s[i].x,s[i].y,s[j].x,s[j].y); } }//因为凸包之后的点数量会很少,所以直接遍历寻找最大距离的点 cout<<Max<<endl; }
相关文章推荐
- POJ 2187 Beauty Contest 构造凸包 + 旋转卡壳
- POJ 2187 Beauty Contest 构造凸包 + 旋转卡壳
- POJ 2187:Beauty Contest (凸包+旋转卡壳)
- poj 2187:Beauty Contest(计算几何,求凸包,最远点对)
- POJ 2187 Beauty Contest——凸包
- 【POJ 2187】 Beauty Contest (凸包-Graham扫描算法)
- POJ 2187 Beauty Contest(旋转卡壳求点凸包直径)
- 【凸包】poj 2187 Beauty Contest (旋转卡壳求平面最远点对)
- POJ2187:Beauty Contest(凸包 & 旋转卡壳)
- POJ Beauty Contest 2187 (凸包)
- POJ 2187 Beauty Contest G++
- POJ 2187: Beauty Contest(旋转卡)
- POJ 2187 Beauty Contest ——计算几何
- poj 2187:Beauty Contest(旋转卡壳)
- [poj][2187][Beauty Contest]
- POJ Beauty Contest (凸包)
- POJ 2187:Beauty Contest 求给定一些点集里最远的两个点距离
- Beauty Contest(POJ_2187)
- POJ 2187 Beauty Contest
- poj 2187 Beauty Contest(旋转卡壳求最远点对)