POJ 2187 Beauty Contest [旋转卡壳]【计算几何】
2017-03-29 19:57
435 查看
题目链接:http://poj.org/problem?id=2187
————————————————————————————————————————————
Beauty Contest
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 36877 Accepted: 11417
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)
————————————————————————————————————————————
没啥说的就是一个旋转卡壳,算是留个模板
————————————————————————————————————————————
————————————————————————————————————————————
Beauty Contest
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 36877 Accepted: 11417
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)
————————————————————————————————————————————
没啥说的就是一个旋转卡壳,算是留个模板
————————————————————————————————————————————
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int maxn=100000+10; int N,top; struct node{ int x,y; }p[maxn],point,stack[maxn]; int get_cross(node a,node b,node c){ return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x); } int get_dis(node a,node b){ return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y); } void find_point(){ int col=0; point=p[0]; for(int i=1;i<N;i++){ if(p[i].y<point.y || (p[i].y==point.y && p[i].x<point.x)){ col=i; point=p[i]; } } swap(p[0],p[col]); } bool cmp(node a,node b){//we sort these points as the clockwise. int t=get_cross(point,a,b); if(t<0)return true; if(t>0)return false; int xx=get_dis(point,a),yy=get_dis(point,b); return xx<yy; } void Graham(){ top=1; p =p[0]; stack[0]=p[0]; stack[1]=p[1]; for(int i=2;i<N;i++){//it rotates as the clockwise. while(top>=1 && get_cross(stack[top-1],stack[top],p[i])>=0)top--; stack[++top]=p[i]; } } void rotating_colipers(){ int ans=0,x=2; stack[top+1]=stack[0]; for(int i=1;i<=top;i++){ while(get_cross(stack[x+1],stack[i+1],stack[i])>get_cross(stack[x],stack[i+1],stack[i])){ x=(x+1)%(top+1); } ans=max(ans,get_dis(stack[x],stack[i])); ans=max(ans,get_dis(stack[x],stack[i+1])); ans=max(ans,get_dis(stack[x+1],stack[i+1])); ans=max(ans,get_dis(stack[x+1],stack[i])); } printf("%d",ans); } int main(){ scanf("%d", &N); for(int i=0;i<N;i++){ scanf("%d%d",&p[i].x, &p[i].y); } if(N==2)printf("%d",get_dis(p[0],p[1])); else{ find_point(); sort(p+1,p+N,cmp); Graham(); rotating_colipers(); } puts(""); return 0; }
相关文章推荐
- 【计算几何】 Andrew凸包算法 + 旋转卡壳(以求点对最长距离为例) -- 以 POJ 2187 Beauty Contest 为例
- POJ 2187(计算几何+旋转卡壳法或暴力)
- [Poj 2187]计算几何之凸包(三) {旋转卡壳初步}
- POJ 2187 计算几何之旋转卡壳
- POJ 2187 Beauty Contest(凸包 + 旋转卡壳)
- POJ 2187 Beauty Contest(凸包+旋转卡壳)
- poj 2187 Beauty Contest (旋转卡壳之平面最远点对)
- POJ 2187 Beauty Contest 旋转卡壳入门题
- poj 2187 Beauty Contest(凸包+旋转卡壳)
- poj 2187 Beauty Contest 题解(凸包模板+旋转卡壳)
- POJ 2187 Beauty Contest [凸包,旋转卡壳]
- POJ 2079(计算几何初步——凸包加旋转卡壳)
- POJ 2187 Beauty Contest 旋转卡壳
- Poj 2187 Beauty Contest_旋转凸包卡壳
- POJ 2187 Beauty Contest (求最远点对,凸包+旋转卡壳)
- POJ 2187 Beauty Contest【旋转卡壳求凸包直径】
- POJ 2187 Beauty Contest(计算几何-- 凸包)
- poj 2187 Beauty Contest , 旋转卡壳求凸包的直径的平方
- poj2187 Beauty Contest 最远点对模板(旋转卡壳)
- poj 2187 Beauty Contest 经典题目:凸包+旋转卡壳