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 ...
随机推荐
- selenium启动PhantomJS错误
from selenium import webdriverbrowser = webdriver.PhantomJS(executable_path="D:\PhantomJS\phant ...
- 给文件加ip访问限制
获取IP: function getIP(){ return isset($_SERVER['HTTP_X_FORWARDED_FOR'])? $_SERVER['HTTP_X_FORWARDED_F ...
- jstl简介
JavaServer Page Standard Tag Library是一个有用的JSP标签的集合,它封装了许多JSP应用程序通用的核心功能. JSTL支持常见的,结构性任务,如迭代和条件,标签为操 ...
- 【JQGRID DOCUMENTATION】.学习笔记.4.Navigator
Navigator是一个将查找或编辑记录的动作,变得非常容易达到用户交互特性.开发者可以创建自定义动作,也可以使用六个预定义的动作.jqGrid为预定义的动作提供icon button图形. 一组完整 ...
- 模拟namenode崩溃,使用secondarynamenode恢复
方法一.使用namespaceID 1.在namenode节点上,将dfs.name.dir指定的目录中(这里是name目录)的内容情况,以此来模拟故障发生. [hadoop@node1 name]$ ...
- 关于PHP的正则表达式
1.入门简介 简单的说,正则表达式是一种可以用于模式匹配和替换的强有力的工具.我们可以在几乎所有的基于UNIX系统的工具中找到正则表达式的身影,例如,vi编辑器,Perl或PHP脚本语言,以及awk或 ...
- 微信利用PHP创建自定义菜单的方法
在使用通用接口前,你需要做以下两步工作:1.拥有一个微信公众账号,并获取到appid和appsecret(在公众平台申请内测资格,审核通过后可获得)2.通过获取凭证接口获取到access_token注 ...
- CI框架分页类
分页类1.分页类参数说明 'base_url' => 指向你的分页所在的控制器类/方法的完整的 URL, 'total_rows' => 数据的总行数, 'per_page' => ...
- java产生随机数并求和
设计思路: 先随机生成10个数,组成一个数组,然后用消息框显示数组内容,然后用循环计算数组元素的和,将结果也显示在消息框中. 程序流程图: 源程序代码: import javax.swing.*; p ...
- .Net neatupload上传控件实现文件上传的进度条
1. 引入bin文件 (可以到neatupload官网下载,也可以到教育厅申报系统中找) 2. 将控件加入到工具栏,在工具栏中点鼠标右键,如图: 3. 加入neatuplaod这个文件夹(可以到nea ...