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 ;
}
最新文章
- JS 原型链
- SP2-0618: 无法找到会话标识符。启用检查 PLUSTRACE 角色 SP2-0611: 启用 STATISTICS 报告时出错
- vijos[1355]车队过桥问题
- 常用的Java代码汇总
- laravel 中 与前端的一些事4 之合并压缩静态文件
- 解决 Eclipse 项目有红感叹号的方法
- Equipment Box[HDU1110]
- 2016年12月15日 星期四 --出埃及记 Exodus 21:10
- tp集成支付宝担保支付
- python学习之路二(字符串,字典,序列和元组)
- Docker版本升级至17.03
- linux_Nginx优化
- canvas 绘制图形
- SAP OLE中常用的一些方法和属性
- JavaScript(第十八天)【DOM基础】
- my.conf配置信息
- zabbix 自定义监控项简单案例
- GZip对字符串压缩和解压
- Python中创建守护进程
- azkaban编译安装配置文档