您的位置:首页 > 其它

poj 2187 Beauty Contest 题解(凸包模板+旋转卡壳)

2014-08-27 17:06 483 查看
http://poj.org/problem?id=2187

题意:求所给出的点中,距离最远的点对的距离

#include<stdio.h>
#include<algorithm>
#include<math.h>
#define MAX 50009
using namespace std;
struct Node{
int x;
int y;
}p[MAX];
int top,s[MAX],n;
int cross(struct Node a, struct Node b, struct Node c){
return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
int dis(struct Node a, struct Node b){
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
bool cmp(struct Node a, struct Node b){//返回false的时候交换 ,逆时针排序
int ans=cross(p[0],a,b);
if(ans>0||(ans==0&&dis(p[0],a)<dis(p[0],b))){
return true;
}
return false;
}
/**
int cmp1(const void *a,const void *b) //逆时针排序 返回正数要交换,逆时针排序
{
struct Node *c=(struct Node *)a;
struct Node *d=(struct Node *)b;
int k=cross(p[0],*c,*d);
if(k<0) return 1;
else if(k==0 && (dis(p[0],*c)>=dis(p[0],*d)))
return 1;
else return -1;
}**/
void graham(){
s[0]=0;
s[1]=1;
top=1;
int i;
for(i=2;i<n;i++){//求上凸壳
while(top>0&&cross(p[s[top-1]],p[s[top]],p[i])<=0){//这样构造的凸包是逆时针的。
top--;//如果是 cross(p[s[top]],p[s[top-1]],p[i]),则是按照顺时针构造的凸包(提前十逆时针给所有点排序的),
//那么就必须要用这种上凸壳和下凸壳才能构造出完整的凸包,同时最后s[top]==s[0];也就是凸包的起始点出现在了开始和结尾
//这样就为后面的旋转卡壳做了准备,旋转卡壳中就不需要做s[++top]=s[0],这一步了。
}
s[++top]=i;
}
int tmp=top;
s[++top]=n-2;
for(i=n-3;i>=0;i--){//求下凸壳
while(top>tmp&&cross(p[s[top-1]],p[s[top]],p[i])<=0){
top--;
}
s[++top]=i;
}
}
int RC(){//由于是上下凸壳构造的凸包,则不用做s[++top]=s[0];
int q=1;
int ans=0;
for(int i=0;i<top;i++){
while(abs(cross(p[s[i+1]],p[s[i]],p[s[(q+1)%top]]))>abs(cross(p[s[i+1]],p[s[i]],p[s[q]]))){
q=(q+1)%top;
}
ans=max(ans,max(dis(p[s[i]],p[s[q]]),dis(p[s[i+1]],p[s[q+1]])));
}
return ans;
}
int main(){
int zero=0;
while(~scanf("%d",&n)){
for(int i=0;i<n;i++){
scanf("%d %d",&p[i].x,&p[i].y);
}
zero=0;
for(int i=0;i<n;i++){
if(p[zero].y>p[i].y||(p[zero].y==p[i].y&&p[zero].x>p[i].x)) zero=i;
}
struct Node tmp;
tmp=p[0];
p[0]=p[zero];
p[zero]=tmp;
sort(p+1,p+n,cmp);
//qsort(p+1,n-1,sizeof(p[0]),cmp1);
graham();
printf("%d\n",RC());
}
return 0;
}
/**
7
0 0
0 1
1 1
1 0
2 2
0 2
2 0

20
5521 -4185
157 3291
439 9868
4812 -2903
4993 5723
-9349 8008
-9267 -4272
-4861 -9500
6270 -4962
7394 -1099
-4349 8442
-1944 -491
5147 -5526
-2062 4624
-7775 8187
156 7746
-9646 -9686
1038 -2855
-9818 -4150
594 5175

10
0 0
10000 0
1 100
2 199
9999 100
9998 199
100 -900
200 -1799
9800 -1799
9900 -900
**/


#include<stdio.h>
#include<algorithm>
#include<math.h>
#define MAX 50009
using namespace std;
struct Node{
int x;
int y;
}p[MAX];
int top,s[MAX],n;
int cross(struct Node a, struct Node b, struct Node c){
return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
int dis(struct Node a, struct Node b){
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
bool cmp(struct Node a, struct Node b){//返回false的时候交换
int ans=cross(p[0],a,b);
if(ans>0||(ans==0&&dis(p[0],a)<dis(p[0],b))){
return true;
}
return false;
}
void graham(){
s[0]=0;
s[1]=1;
top=1;
int i;
for(i=2;i<n;i++){
while(top>0&&cross(p[s[top-1]],p[s[top]],p[i])<=0){//sort排序也要和这个一样(比如同为顺时针或者同为逆时针),才能构造凸包
top--;
}
s[++top]=i;
}
}
int RC(){
int q=1;
int ans=0;
s[++top]=0;//上面的求凸包的方式,这个地方一定要给栈顶加上起始点
for(int i=0;i<top;i++){
while(abs(cross(p[s[(i+1)%top]],p[s[i]],p[s[(q+1)%top]]))>abs(cross(p[s[(i+1)%top]],p[s[i]],p[s[q]]))){
q=(q+1)%top;
}
ans=max(ans,max(dis(p[s[i]],p[s[q]]),dis(p[s[(i+1)%top]],p[s[(q+1)%top]])));
}
return ans;
}
int main(){
int zero=0;
while(~scanf("%d",&n)){
for(int i=0;i<n;i++){
scanf("%d %d",&p[i].x,&p[i].y);
}
zero=0;
for(int i=0;i<n;i++){
if(p[zero].y>p[i].y||(p[zero].y==p[i].y&&p[zero].x>p[i].x)) zero=i;
}
struct Node tmp;
tmp=p[0];
p[0]=p[zero];
p[zero]=tmp;
sort(p+1,p+n,cmp);
graham();
printf("%d\n",RC());
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: