poj 2187 Beauty Contest
题意:给你一个数据范围在2~5e4范围内的横纵坐标在-1e4~1e4的点,问你任意两点之间的距离的最大值的平方等于多少?
一道卡壳凸包的模板题,也是第一次写计算几何的题,就看了些模板,关于预备知识;我是直接找到左下角的点,排好序之后,就直接形成凸包,之后调用rotating_calipers()求解;里面注意在凸包构造好之后,因为是++top的,所以在后面卡壳里面%top会出现问题,所以循环之后再一次++top;开始看graham()里面的while循环看了很久,其实就是维护一个逆时针旋转单调栈,还有一点就是在卡壳里面的while循环里面,为什么直接对q递增,之后%top;能过证明当三角形面积(叉乘)在增大时,边长也在增大;
本文参考 孟起
// 110ms
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<time.h>
using namespace std;
const int MAXN = 5e4+;
struct point{
int x,y;
point(){}
point(int _x,int _y){
x = _x; y = _y;
}
int operator *(const point &b)const{
return (x*b.y - y*b.x);
}
point operator -(const point &b)const{
return point(x - b.x,y - b.y);
} void input(){
scanf("%d%d",&x,&y);
}
}p[MAXN];
int dist2(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) // 正表示逆时针,返回true表示不要交换;
{
int cp = (a-p[])*(b-p[]);
if(cp < ) return false;
if(cp == && (dist2(a,p[]) >= dist2(b,p[])) ) return false;
return true;
} int stk[MAXN],top;
void graham(int n) // 形成凸包;
{
int i;
stk[] = ;stk[] = ;
top = ;
for(i = ;i < n;i++){ // 构造一个逆时针旋转的单调栈;
while(top > && (p[stk[top]] - p[stk[top-]])*(p[i] - p[stk[top-]]) <= )
top--;
stk[++top] = i;
}
stk[++top] = ;//为了下面%top,很容易知道n-1号元素一定在凸包里面;
/*for(i=0;i<n;i++)
printf("**%d %d\n",p[i].x,p[i].y);
printf("\n%d\n",top); // 0 ~ top - 1;
for(i=0;i<top;i++)
printf("**%d %d\n",p[stk[i]].x,p[stk[i]].y);*/
} int rotating_calipers() //卡壳
{
int q = ,ans = ;
stk[top] = ;
for(int i = ;i < top;i++){
point tmp = p[stk[i+]] - p[stk[i]];
while((tmp * (p[stk[q+]] - p[stk[i]])) > (tmp * (p[stk[q]] - p[stk[i]]))) // 叉积与面积和边长的关系;
q = (q+)%top;
ans = max(ans,max(dist2(p[stk[i+]],p[stk[q+]]),dist2(p[stk[i]],p[stk[q]])));//最好的是i&&q,但是有平行的情况,so:max
}
return ans;
}
int main()
{
int i,n;
while(scanf("%d",&n) == ){
for(i = ;i < n;i++)
p[i].input();
int st = ;
for(i = ;i < n;i++) //选出起始点;
if(p[st].y > p[i].y||(p[st].y == p[i].y && p[st].x > p[i].x))
st = i;
swap(p[],p[st]);
sort(p+,p+n,cmp);//以p[0]为参考点逆时针极角由近到远排序;
graham(n);
printf("%d\n",rotating_calipers());
}
return ;
}
poj 2187 Beauty Contest的更多相关文章
- poj 2187 Beauty Contest(凸包求解多节点的之间的最大距离)
/* poj 2187 Beauty Contest 凸包:寻找每两点之间距离的最大值 这个最大值一定是在凸包的边缘上的! 求凸包的算法: Andrew算法! */ #include<iostr ...
- poj 2187 Beauty Contest (凸包暴力求最远点对+旋转卡壳)
链接:http://poj.org/problem?id=2187 Description Bessie, Farmer John's prize cow, has just won first pl ...
- POJ 2187 - Beauty Contest - [凸包+旋转卡壳法][凸包的直径]
题目链接:http://poj.org/problem?id=2187 Time Limit: 3000MS Memory Limit: 65536K Description Bessie, Farm ...
- 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: 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(凸包,旋转卡壳)
题面 Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, earning the ...
- poj 2187:Beauty Contest(旋转卡壳)
Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 32708 Accepted: 10156 Description Bes ...
- POJ 2187 Beauty Contest(凸包+旋转卡壳)
Description Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, ea ...
随机推荐
- JS精确到小数点两位
1.会四色五入 var num =2.446242342; num = num.toFixed(2); // 输出结果为 2.452.正则Number(15.7784514000.toString() ...
- IE兼容低版本
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><m ...
- Redis集群
一.Redis集群原理 集群技术是构建高性能网站架构的重要手段,试想在网站承受高并发访问压力的同时,还需要从海量数据中查询出满足条件的数据,并快速响应,我们必然想到的是将数据进行切片,把数据根据某种规 ...
- 配置Linux数据转发(给其他接口转发一个接口的internet网络)
配置Linux数据转发 [主机]第一步开启转发net.ipv4.ip_forward = 1echo "1" > /proc/sys/net/ipv4/ip_forward第 ...
- JavaScript设计模式(一)
使用JavaScript框架和库过程中, 我遇到过很多感觉上'奇形怪状'的代码. 大多数情况下, 按照惯例编写代码也能够写出很多出色的功能. 但是如果不从根本上理解它们实现的方法, 就没办法完全充分发 ...
- 递归法绑定文件夹到导航树&在指定文件夹下新建文件夹
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (Request.QueryString[&q ...
- 错误源:WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery'. Please add a ScriptResourceMapping named jquery(case-sensitive).
Server Error in '/' Application. WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping ...
- 树莓派配置AP模式
所需硬件:树莓派.无线网卡 1.查看无线网卡是否被识别 pi@raspberrypi ~ $ sudo lsusb Bus Device : ID : Standard Microsystems Co ...
- asp:第三平台登陆
第三平台登陆接口申请网址: http://open.51094.com/ 文档: 第三方合作登录平台使用说明 为方便更多的开发朋友,本人特将当前市面上所有支持第三方联合登录的接口集为一体,以前需要多次 ...
- aspx文件移动到新建的文件夹中设置路径的问题
项目中仅仅把aspx移动到想要的文件夹内是会出错的,不用想也知道是路径问题.这里我就说这个路径该如何去修改. 两个地方需要修改:1.母版路径修改方法: <link href="Styl ...