【poj2187】 Beauty Contest
http://poj.org/problem?id=2187 (题目链接)
题意
求点集上两点间最长距离
Solution
凸包+旋转卡壳。
旋转卡壳是看起来很难,但是很好意会也很好实现的算法,但是要真正的搞懂搞透还是有点难度,有篇博客写得很好,也就不再赘述了。
代码
// poj2187
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<map>
#define inf 2147483640
#define LL long long
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout);
using namespace std;
inline LL getint() {
LL x=0,f=1;char ch=getchar();
while (ch>'9' || ch<'0') {if (ch=='-') f=-1;ch=getchar();}
while (ch>='0' && ch<='9') {x=x*10+ch-'0';ch=getchar();}
return x*f;
} const int maxn=50010;
struct point {int x,y;}p[maxn];
int n,top,ans,s[maxn]; int dis(point a,point b) {
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int cross(point p0,point p1,point p2) {
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
bool cmp(point a,point b) {
int t=cross(p[1],a,b);
if (t>0) return 1;
if (t<0) return 0;
return dis(p[1],a)<dis(p[1],b);
}
void Graham() {
top=0;
if (n==1) s[++top]=1;
else if (n==2) {s[++top]=1;s[++top]=2;}
else {
s[++top]=1;s[++top]=2;
for (int i=3;i<=n;i++) {
while (top>1 && cross(p[s[top-1]],p[s[top]],p[i])<=0) top--;
s[++top]=i;
}
}
}
void RC() {
int q=2;
ans=dis(p[s[1]],p[s[2]]);
for (int i=1;i<=top;i++) {
while (abs(cross(p[s[i%top+1]],p[s[i]],p[s[q%top+1]]))>abs(cross(p[s[i%top+1]],p[s[i]],p[s[(q-1)%top+1]]))) q=q%top+1;
ans=max(ans,max(dis(p[s[i%top+1]],p[s[q]]),dis(p[s[i]],p[s[q]])));
}
}
int main() {
while (scanf("%d",&n)!=EOF) {
int k=1;
for (int i=1;i<=n;i++) {
scanf("%d%d",&p[i].x,&p[i].y);
if (p[i].x==p[k].x ? p[i].y<p[k].y : p[i].x<p[k].x) k=i;
}
point p0=p[1];p[1]=p[k];p[k]=p0;
sort(p+2,p+1+n,cmp);
Graham();
RC();
printf("%d\n",ans);
}
return 0;
}
【poj2187】 Beauty Contest的更多相关文章
- 【POJ】【2187】Beauty Contest
计算几何/旋转卡壳 学习旋转卡壳请戳这里~感觉讲的最好的就是这个了…… 其实就是找面积最大的三角形?...并且满足单调…… 嗯反正就是这样…… 这是一道模板题 好像必须写成循环访问?我在原数组后面复制 ...
- 【POJ 2187】Beauty Contest 凸包+旋转卡壳
xuán zhuǎn qiǎ ké模板题 是这么读吧(≖ ‿ ≖)✧ 算法挺简单:找对踵点即可,顺便更新答案. #include<cstdio> #include<cstring&g ...
- 【POJ 2187】Beauty Contest(凸包直径、旋转卡壳)
给定点集的最远两点的距离. 先用graham求凸包.旋(xuán)转(zhuàn)卡(qiǎ)壳(ké)求凸包直径. ps:旋转卡壳算法的典型运用 http://blog.csdn.net/hanch ...
- 【洛谷 P1452】 Beauty Contest (二维凸包,旋转卡壳)
题目链接 旋转卡壳模板题把. 有时间再补总结吧. #include <cstdio> #include <cmath> #include <algorithm> u ...
- 【poj2187】最远点对(勉强凑数)
题目简述 输入n个点,及其坐标,n<=50000,所有坐标都是不超过10000的整数组成,没有重点. 问最远点对间的距离的平方是多少 题解 这是一道旋转卡壳的裸题 我们要求这个多边形的直径,这可 ...
- 【ARC066】F - Contest with Drinks Hard
题解 我写的斜率维护,放弃了我最擅长的叉积维护,然后发现叉积维护也不会爆long long哦-- 一写斜率维护我的代码就会莫名变长而且难写--行吧 我们看这题 推了推式子,发现这是个斜率的式子,但是斜 ...
- POJ2187:Beauty Contest——题解
http://poj.org/problem?id=2187 题目大意:给n个点,求点对最大距离的平方. ———————————————————— 很容易证明最大距离的点对在最大凸包上. 那么就是旋转 ...
- 【CF1257E】The Contest【线段树】
题意:给定三个序列abc,问最少操作几次使得满足a<b<c 题解:将三个序列合并起来,设cnt[i][1/2/3]表示前i个数有几个是来自序列1/2/3的. 枚举第一个序列要到i,此时对于 ...
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
随机推荐
- [反编译U3D]Decompile Unity Resources
工具说明 反编译unity project资源文件,包括ios,android,pc等平台资源,仅供学习使用! 以下工具任选其一,未有特殊说明是可以同时提取unity3.x,unity4.x,unit ...
- java 13-2 Arrays工具类
1.Arrays:针对数组进行操作的工具类.比如说排序和查找. 1:public static String toString(int[] a) 把数组转成字符串 2:public static v ...
- Python-json 和 pickle
这是用于序列化的两个模块 json:用于字符串和python数据类型间进行转换 pickle:用于python特有的类型和python的数据类型间进行转换 json模块提供了四个功能:dumps du ...
- SpringMVC Controller介绍(转)
SpringMVC Controller 介绍 一.简介 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理 ...
- MemCached配置与缓存知识概述
先看看百度百科里面对缓存的介绍: 缓存(Cache memory)是硬盘控制器上的一块内存芯片,具有极快的存取速度,它是硬盘内部存储和外界接口之间的缓冲器.由于硬盘的内部数据传输速度和外界介面传输速度 ...
- eclipse的一些初始化设置
eclipse下载地址:http://www.eclipse.org/ 添加java注释模板 codetemplates.xml:Window->Preferences->Java-> ...
- 最长回文子串(Manacher算法)
回文字符串,想必大家不会不熟悉吧? 回文串会求的吧?暴力一遍O(n^2)很简单,但当字符长度很长时便会TLE,简单,hash+二分搞定,其复杂度约为O(nlogn), 而Manacher算法能够在线性 ...
- 使用jQuery加载html页面到指定的div
一.jQuery加载一个html页面到指定的div里 把a.html里面的某一部份的内容加载到b.html的一个div里.比如:加载a.html里面的<div id=“row"> ...
- 对于RegExp反向引用的一点理解
置顶文章:<纯CSS打造银色MacBook Air(完整版)> 上一篇:<关于js的Array.prototype.slice.call> 作者主页:myvin 博主QQ:85 ...
- [C/C++基础] C语言常用函数strlen的使用方法
函数声明:extern unsigned int strlen(char *s); 所属函数库:<string.h> 功能:返回s所指的字符串的长度,其中字符串必须以’\0’结尾 参数:s ...