[原创]POJ 2187 Beauty Contest [旋转卡壳]【计算几何】

2017-03-29 19:57:49 Tabris_ 阅读数:425


博客爬取于2020-06-14 22:41:03
以下为正文

版权声明:本文为Tabris原创文章,未经博主允许不得私自转载。
https://blog.csdn.net/qq_33184171/article/details/68070650


题目链接: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)

————————————————————————————————————————————

没啥说的就是一个旋转卡壳,算是留个模板

————————————————————————————————————————————

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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[N]=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;
}