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的更多相关文章

  1. poj 2187 Beauty Contest(凸包求解多节点的之间的最大距离)

    /* poj 2187 Beauty Contest 凸包:寻找每两点之间距离的最大值 这个最大值一定是在凸包的边缘上的! 求凸包的算法: Andrew算法! */ #include<iostr ...

  2. poj 2187 Beauty Contest (凸包暴力求最远点对+旋转卡壳)

    链接:http://poj.org/problem?id=2187 Description Bessie, Farmer John's prize cow, has just won first pl ...

  3. POJ 2187 - Beauty Contest - [凸包+旋转卡壳法][凸包的直径]

    题目链接:http://poj.org/problem?id=2187 Time Limit: 3000MS Memory Limit: 65536K Description Bessie, Farm ...

  4. POJ 2187 Beauty Contest【旋转卡壳求凸包直径】

    链接: http://poj.org/problem?id=2187 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

  5. POJ 2187 Beauty Contest 凸包

    Beauty Contest Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 27276   Accepted: 8432 D ...

  6. POJ 2187 Beauty Contest [凸包 旋转卡壳]

    Beauty Contest Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 36113   Accepted: 11204 ...

  7. POJ 2187 Beauty Contest(凸包,旋转卡壳)

    题面 Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, earning the ...

  8. poj 2187:Beauty Contest(旋转卡壳)

    Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 32708   Accepted: 10156 Description Bes ...

  9. POJ 2187 Beauty Contest(凸包+旋转卡壳)

    Description Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, ea ...

随机推荐

  1. 可扩展的listview--Expandablelistview

    layout.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" x ...

  2. web项目设计与开发——DBHelper3

    本次学习的内容为根据DBHelper对数据库里的数据进行增删改查 具体内容为: 一.编写程序 1.创建工程——Mangage    2.在src目录下创建五个包,分别为DAO,DBHelper,Ent ...

  3. Python基础:1.数据类型(列表)

    提示:python版本为2.7,windows系统 1.列表(List) List,是一个有序的集合,可以添加.删除其中的元素. >>> colors = ['red', 'oran ...

  4. D3中path各指令的含义

    svg.append('path').attr({ id: 'mypath', d: 'M50 100Q350 50 350 250Q250 50 50 250' }) path 的指令有: 指令 参 ...

  5. CCLablettf读取显示xml文件内容显示中文

    CCDictionary *strings = CCDictionary::createWithContentsOfFile("tips2.xml"); const char *h ...

  6. golang 依赖控制反转(IoC)

    主流开发语言,为了达到项目间的低耦合,都会借助IoC框架来实现.即抽象和实现分离,使用抽象层,不用关心这些抽象层的具体实现:抽象层的实现,可以独立实现.现在比较流行的领域驱动设计(ddd),为了达到将 ...

  7. c# 远程监控(1) 大纲

    此篇文章主要讲了如何使用C# Winform程序模拟一个远程监控系统,并使用RTP实时传输协议传输数据. 应用场景:远程监控.局域网视频会议.客户端流式缓冲播放 这方面的资料还是有一些,但是都需要整合 ...

  8. smarty实现缓存

    首先需要在mySmarty中添加配置信息,开启缓存,设置缓存文件存放目录,设置缓存时间缓存可以实现减少访问数据库,减轻数据库压力,访问一次数据库,形成静态页面,下次直接调用这个页面,也可以用nocac ...

  9. 将Cent0S 7的网卡名称eno16777736改为eth0

    新建的虚拟机redhat linux7默认的网卡名称是eno16777736,找不到eth0如图所示: 修改网卡名称: 输入如下命令,进入对应目录,编辑文件: vim /etc/sysconfig/g ...

  10. jQuery 源码分析4: jQuery.extend

    jQuery.extend是jQuery最重要的方法之一,下面看看jQuery是怎样实现扩展操作的 // 如果传入一个对象,这个对象的属性会被添加到jQuery对象中 // 如果传入两个或多个对象,所 ...