算法复习——凸包加旋转卡壳(poj2187)
题目:
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
题解:
凸包加旋转卡壳模板题,注意三个细节:凸包判断时注意变量i和tot,卡壳时注意一个next(j)!=i的条件,然后和只有两个点时的特判;
代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cctype>
#include<cstring>
#include<string>
#include<ctime>
#include<algorithm>
using namespace std;
struct point
{
int x;
int y;
}p[],q[];
inline int operator*(point a,point b)
{
return a.x*b.y-a.y*b.x;
}
inline point operator-(point a,point b)
{
point t;
t.x=a.x-b.x;
t.y=a.y-b.y;
return t;
}
inline int norm(point a)
{
return a.x*a.x+a.y*a.y;
}
bool comp(int u,int v)
{
int det=(p[u]-p[])*(p[v]-p[]);
if(det!=) return det>;
return norm(p[u]-p[])<norm(p[v]-p[]);
}
int n,m;
int next(int i)
{
if(i!=m) return ++i;
else return ;
}
void tubao()
{
int id=;
for(int i=;i<=n;i++)
if(p[id].x>p[i].x||(p[id].x==p[i].x&&p[id].y>p[i].y))
id=i;
if(id!=) swap(p[id],p[]);
int per[];
for(int i=;i<=n;i++) per[i]=i;
sort(per+,per+n+,comp);
q[++m]=p[];
for(int i=;i<=n;i++)
{
int j=per[i];
while(m>=&&(q[m]-q[m-])*(p[j]-q[m-])<=) m--;
q[++m]=p[j];
}
q[++m]=p[];
}
int area(point u,point v,point s)
{
return (u-s)*(v-s);
}
int solve()
{
if(m==) return (norm(q[]-q[]));
q[m+]=q[];
int res=;
for(int i=,j=;i<=m;i++)
{
while(next(j)!=i&&area(q[i],q[i+],q[j+])>=area(q[i],q[i+],q[j]))
j=next(j);
res=max(res,norm(q[i+]-q[j]));
res=max(res,norm(q[i]-q[j]));
}
return res;
}
int main()
{
//freopen("a.in","r",stdin);
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d%d",&p[i].x,&p[i].y);
tubao();
int ans=solve();
cout<<ans<<endl;
}
算法复习——凸包加旋转卡壳(poj2187)的更多相关文章
- poj 2187 凸包加旋转卡壳算法
题目链接:http://poj.org/problem?id=2187 旋转卡壳算法:http://www.cppblog.com/staryjy/archive/2009/11/19/101412. ...
- POJ 2187 Beauty Contest(凸包,旋转卡壳)
题面 Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, earning the ...
- 【BZOJ1185】[HNOI2007]最小矩形覆盖(凸包,旋转卡壳)
[BZOJ1185][HNOI2007]最小矩形覆盖(凸包,旋转卡壳) 题面 BZOJ 洛谷 题解 最小的矩形一定存在一条边在凸包上,那么枚举这条边,我们还差三个点,即距离当前边的最远点,以及做这条边 ...
- BZOJ1069 SCOI2007 最大土地面积 凸包、旋转卡壳
传送门 在这里假设可以选择两个相同的点吧-- 那么选出来的四个点一定会在凸包上 建立凸包,然后枚举这个四边形的对角线.策略是先枚举对角线上的一个点,然后沿着凸包枚举另一个点.在枚举另一个点的过程中可以 ...
- BZOJ1185 HNOI2007 最小矩形覆盖 凸包、旋转卡壳
传送门 首先,肯定只有凸包上的点会限制这个矩形,所以建立凸包. 然后可以知道,矩形上一定有一条边与凸包上的边重合,否则可以转一下使得它重合,答案会更小. 于是沿着凸包枚举这一条边,通过旋转卡壳找到离这 ...
- 【洛谷 P4166】 [SCOI2007]最大土地面积(凸包,旋转卡壳)
题目链接 又调了我两个多小时巨亏 直接\(O(n^4)\)枚举4个点显然不行. 数据范围提示我们需要一个\(O(n^2)\)的算法. 于是\(O(n^2)\)枚举对角线,然后在这两个点两边各找一个点使 ...
- 【洛谷 P3187】 [HNOI2007]最小矩形覆盖 (二维凸包,旋转卡壳)
题目链接 嗯,毒瘤题. 首先有一个结论,就是最小矩形一定有条边和凸包重合.脑补一下就好了. 然后枚举凸包的边,用旋转卡壳维护上顶点.左端点.右端点就好了. 上顶点用叉积,叉积越大三角形面积越大,对应的 ...
- 【洛谷 P1452】 Beauty Contest (二维凸包,旋转卡壳)
题目链接 旋转卡壳模板题把. 有时间再补总结吧. #include <cstdio> #include <cmath> #include <algorithm> u ...
- POJ 2187 /// 凸包入门 旋转卡壳
题目大意: 求最远点对距离 求凸包上的最远点对 挑战263页 #include <cstdio> #include <string.h> #include <algori ...
随机推荐
- JS 操作内容 操作元素
操作内容:普通元素.innerHTML = "值": 会把标记执行渲染普通元素.innerText = "值": 将值原封不动的展示出来,即使里面有标记 var ...
- ubuntu 14.04 配置java 1.8环境变量
从官网上下载jdk 源文件,并解压 root@hett-PowerEdge-T30:/usr/local/src# tar -xzvf jdk-8u151-linux-x64.tar.gz 解压完成之 ...
- vue cli的安装与使用
一.简介 vue作为前端开发的热门工具,受到前端开发人员的喜爱.为了简化项目的构建,采用vue cli来简化开发. vue cli是一个全局安装的npm包,提供了终端使用的命令.vue create可 ...
- jsc 解码窥探
先使用 JS_DecodeScript反编译jsc 得到AST树 AST树词法解析 http://esprima.org/ AST还原成源码: npm install escodegen AST树遍 ...
- 如何解决webpack中css背景图片的绝对地址
在项目开发中,一般写相对路径是没有问题的,但是在项目比较大的情况下,我的scss文件可能为了方便管理,会放在不同的文件夹下,有的可能又不需要放在文件夹下,比如我的scss文件结构如下: module ...
- 浮动清楚浮动及position的用法
float 在 CSS 中,任何元素都可以浮动. 浮动元素会生成一个块级框,而不论它本身是何种元素. 关于浮动的两个特点: 浮动的框可以向左或向右移动,直到它的外边缘碰到包含框或另一个浮动框的边框为止 ...
- Bootstrap历练实例:基本按钮组
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- Bzoj 1088: [SCOI2005]扫雷Mine (DP)
Bzoj 1088: [SCOI2005]扫雷Mine 怒写一发,算不上DP的游戏题 知道了前\(i-1\)项,第\(i\)项会被第二列的第\(i-1\)得知 设\(f[i]\)为第一列的第\(i\) ...
- 学习笔记(_huaji_)
假如我没有见过太阳,我也许会忍受黑暗. 如果我知道自己会在哪里死去,我就永远都不去那儿.失败的经历,其实也有它的价值. 人的过失会带来错误,但要制造真正的灾难还得用计算机. 嘴角微微上扬已不复当年轻狂 ...
- 5.电影搜索之 自动填充,也叫autocomplete、搜索建议!
什么叫自动填充,用过百度的应该都知道!当你输入关键词之后,会有一个下拉的候选列表,都是与你输入的内容相关的,这个就是自动填充的搜索建议.一般的搜索引擎或者站内搜索都会有这个功能. 今天分享下这个功能的 ...