poj 2187 Beauty Contest (凸包暴力求最远点对+旋转卡壳)
链接:http://poj.org/problem?id=2187
Description
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
* Lines 2..N+1: Two space-separated integers x and y specifying coordinate of each farm
Output
Sample Input
4
0 0
0 1
1 1
1 0
Sample Output
2
Hint
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <math.h> using namespace std; const int MAX=;
const double eps=1e-;
typedef struct point
{
double x,y;
}point; point c[MAX]; bool dy(double x,double y)
{
return x>y+eps;
}
bool xy(double x,double y)
{
return x<y-eps;
}
bool xyd(double x,double y)
{
return x<y+eps;
}
bool dyd(double x,double y)
{
return x>y-eps;
}
bool dd(double x,double y)
{
return fabs(x-y)<eps;
} point stk[MAX];
int top; double crossProduct(point a,point b,point c)
{
return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);
}
double dist(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
} double dist1(point a,point b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
} bool cmp(point a,point b)
{
if(dd(a.y,b.y))
{
return xy(a.x,b.x);
}
return xy(a.y,b.y);
}
bool cmp1(point a,point b)
{
double len=crossProduct(c[],a,b);
if(dd(len,0.0))
{
return xy(dist(c[],a),dist(c[],b));
}
return xy(len,0.0);
} double solve()
{
double maxx=0.0;
for(int i=;i<=top;i++)
{
for(int j=i+;j<=top;j++)
{
if(dy(dist1(stk[i],stk[j]),maxx))
{
maxx=dist1(stk[i],stk[j]);
}
}
}
return maxx;
} double Graham(int n)
{
sort(c,c+n,cmp);
sort(c+,c+n,cmp1);
top=;
stk[top++]=c[];
stk[top++]=c[];
stk[top++]=c[];
top--;
for(int i=;i<n;i++)
{
while()
{
point a,b;
a=stk[top];
b=stk[top-];
if(xyd(crossProduct(a,b,c[i]),0.0))
{
top--;
}
else
break;
}
stk[++top]=c[i];
}
return solve();
} int main()
{
int n,i,j;
while(scanf("%d",&n)!=EOF)
{
for(i=;i<n;i++)
{
scanf("%lf%lf",&c[i].x,&c[i].y);
} if(n==)
{
printf("%.0lf\n",dist1(c[],c[]));
}
else
{
printf("%.0lf\n",Graham(n));
}
}
return ;
}
2014/7/27更新
旋转卡壳,卡了我两天啊,快卡死的节奏,照别人代码敲的,也不知道有没有漏掉什么,感觉凸包写的都有问题,仅供借鉴
再粘贴几个学习网址,感觉没几个人是真正理解的,包括我
http://blog.csdn.net/freezhanacmore/article/details/9527663
http://www.cnblogs.com/Booble/archive/2011/03/10/1980089.html
http://www.cnblogs.com/DreamUp/archive/2010/09/16/1828131.html
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#define eps 1e-6
#define MAX 50010
using namespace std;
typedef struct point
{
double x,y;
}point; point c[MAX];
int stk[MAX];
int top; bool xy(double x,double y){ return x<y-eps; }
bool dy(double x,double y){ return x>y+eps; }
bool xyd(double x,double y){ return x<y+eps; }
bool dyd(double x,double y){ return x>y-eps; }
bool dd(double x,double y){ return fabs(x-y)<eps; } double crossProduct(point a,point b,point c)
{
return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);
}
double dist(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double dist_1(point a,point b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
} bool cmp(point a,point b)
{
double len=crossProduct(c[],a,b);
if(dd(len,0.0))
{
return xy(dist(c[],a),dist(c[],b));
}
return xy(len ,0.0);
} double max(double x,double y)
{
return xy(x,y)?y:x;//
}
double rotaing(int n)
{
int q=;
double ans=0.0;
stk[n]=stk[];
for(int i=;i<n;i++)
{
while( xy(fabs(crossProduct(c[stk[i]],c[stk[i+]],c[stk[q]])),
fabs(crossProduct(c[stk[i]],c[stk[i+]],c[stk[q+]]))))
q=(q+)%n;
ans=max(ans,dist_1(c[stk[i]],c[stk[q]]));
}
return ans;
} double Graham(int n)
{
int tmp=;
for(int i=;i<n;i++)
{
if(xy(c[i].x,c[tmp].x) || dd(c[i].x,c[tmp].x) && xy(c[i].y,c[tmp].y))
tmp=i;
}
swap(c[],c[tmp]);
sort(c+,c+n,cmp);
stk[]=;
stk[]=;
top=;
for(int i=;i<n;i++)
{
while( xyd(crossProduct(c[stk[top]],c[stk[top-]],c[i]),0.0)&&top>=)
{
top--;
}
stk[++top]=i;
}
return rotaing(top+);
} int main()
{
int n,i,j,k,t;
while(scanf("%d",&n)!=EOF&&n)
{
for(i=;i<n;i++)
{
scanf("%lf%lf",&c[i].x,&c[i].y);
}
int ans=(int )Graham(n);
printf("%d\n",ans);
}
return ;
}
poj 2187 Beauty Contest (凸包暴力求最远点对+旋转卡壳)的更多相关文章
- poj 2187 Beauty Contest 凸包模板+求最远点对
题目链接 题意:给你n个点的坐标,n<=50000,求最远点对 #include <iostream> #include <cstdio> #include <cs ...
- poj 2187 Beauty Contest(凸包求解多节点的之间的最大距离)
/* poj 2187 Beauty Contest 凸包:寻找每两点之间距离的最大值 这个最大值一定是在凸包的边缘上的! 求凸包的算法: Andrew算法! */ #include<iostr ...
- POJ 2187 - Beauty Contest - [凸包+旋转卡壳法][凸包的直径]
题目链接:http://poj.org/problem?id=2187 Time Limit: 3000MS Memory Limit: 65536K Description Bessie, Farm ...
- POJ 2187 Beauty Contest 凸包
Beauty Contest Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 27276 Accepted: 8432 D ...
- POJ 2187 Beauty Contest [凸包 旋转卡壳]
Beauty Contest Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 36113 Accepted: 11204 ...
- POJ 2187 Beauty Contest(凸包+旋转卡壳)
Description Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, ea ...
- poj 2187 Beauty Contest——旋转卡壳
题目:http://poj.org/problem?id=2187 学习材料:https://blog.csdn.net/wang_heng199/article/details/74477738 h ...
- POJ 2187 Beauty Contest【旋转卡壳求凸包直径】
链接: http://poj.org/problem?id=2187 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...
- poj 2187:Beauty Contest(计算几何,求凸包,最远点对)
Beauty Contest Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 26180 Accepted: 8081 D ...
随机推荐
- 读 《JavaScript: The Good Parts》 有感
提炼出一门语言或技术的 Good Parts, 使用该子集去构造健壮稳固的应用. 我们总是倾向于去学习和使用所有的语言特性,好像凡是新的,凡是提供了的, 就有必要去使用: 这本书告诉我们, 要有选择性 ...
- xx
[git]merge和rebase的区别 - 削微寒 - 博客园 http://www.cnblogs.com/vamei/p/3480994.html 协议森林16 小美的桌号(DHCP协议) - ...
- 161108、Java IO流读写文件的几个注意点
平时写IO相关代码机会挺少的,但却都知道使用BufferedXXXX来读写效率高,没想到里面还有这么多陷阱,这两天突然被其中一个陷阱折腾一下:读一个文件,然后写到另外一个文件,前后两个文件居然不一样? ...
- 【转载】Perl异常处理方法总结
程序脚本在运行过程中,总会碰到这样那样的问题,我们会预知一些问题并为其准备好处理代码,而有一些不能预知.好的程序要能尽可能多的处理可能出现的异常问题,本文就总结了一些方法来解决这些异常,当然perl在 ...
- NIOS II CPU复位异常的原因及解决方案
NIOS II CPU复位异常的原因及解决方案 近期在用nios ii做项目时,发现一个奇怪的现象,在NIOS II EDS软件中编写好的代码,烧写到芯片中,第一次能够正常运行,但是当我按下板卡上 ...
- Android系统版本与API Level对照表
Platform Version API Level VERSION_CODE Notes Android 4.2 17 JELLY_BEAN_MR1 Android 4.1, 4.1.1 16 ...
- JavaEE基础(七)
1.面向对象(构造方法Constructor概述和格式) A:构造方法概述和作用 给对象的数据(属性)进行初始化 B:构造方法格式特点 a:方法名与类名相同(大小也要与类名一致) b:没有返回值类型, ...
- RAC例子
我个人非常推崇ReactiveCocoa,它就像中国的太极,太极生两仪,两仪生四象,四象生八卦,八卦生万物.ReactiveCocoa是一个高度抽象的编程框架,它真的很抽象,初看你不知道它是要干嘛的, ...
- Mongodb 和Redis 的相同点和不同点
MongoDB和Redis都是NoSQL,采用结构型数据存储.二者在使用场景中,存在一定的区别,这也主要由于二者在内存映射的处理过程,持久化的处理方法不同.MongoDB建议集群部署,更多的考虑到集群 ...
- 将ASCII码位于32~126的95个字符输出到屏幕上,为了美观
//将ASCII码位于32~126的95个字符输出到屏幕上,为了美观.要求小于100的码,前面加一个0,每八个转行class shijixing{ public static void main(St ...