算法复习——凸包加旋转卡壳(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 ...
 
随机推荐
- Git常用命令的使用方法
			
推荐一个比较好的GIT的教学地址,廖雪峰老师的git教程! 这里简述Git常用命令的使用方法: 一.初始化git 右键进入 Git Bash 1.建立身份信息 git config --global ...
 - Android上线check_list
			
Android 上线 check_list 类型 序号 检查项 结果(pass/no) 安装 卸载 1 非Root环境下的安装.卸载 2 Root环境下的安装.卸载 3 安装文件检查,无泄漏用户信息的 ...
 - 洛谷 P1629 邮递员送信
			
题目描述 有一个邮递员要送东西,邮局在节点1.他总共要送N-1样东西,其目的地分别是2~N.由于这个城市的交通比较繁忙,因此所有的道路都是单行的,共有M条道路,通过每条道路需要一定的时间.这个邮递员每 ...
 - ubuntu 16.04 国内源安装docker
			
1. 通过curl命令安装 检查是否安装curl root@ros-OptiPlex-3050:~# which curlroot@ros-OptiPlex-3050:~# 更新安装 root@ros ...
 - Unity之脚本编译顺序
			
根据官方的解释,它们的编译顺序如下: (1)所有在Standard Assets.Pro Standard Assets或者Plugins文件夹中的脚本会产生一个Assembly-CSharp-fil ...
 - UVA 1220 Party at Hali-Bula (树形DP)
			
求一棵数的最大独立集结点个数并判断方案是否唯一. dp[i][j]表示以i为根的子树的最大独立集,j的取值为选和不选. 决策: 当选择i时,就不能选择它的子结点. 当不选i时,它的子结点可选可不选. ...
 - nyoj-248-buying feed
			
http://acm.nyist.net/JudgeOnline/problem.php?pid=248 BUYING FEED 时间限制:3000 ms | 内存限制:65535 KB 难度:4 ...
 - idea Please specify commit message
			
在idea中使用github来进行版本控制的时候, 当点击提交的时候遇到了这个问题 错误: Please specify commit message 解决方法: 在commit message中填写 ...
 - 洛谷 P2846 光开关
			
https://www.luogu.org/problemnew/show/P2846 好多题解用线段树来写,然而分块不是更简单好些吗? 一个数组use记录这一块进行了多少次开关操作,两边单独计算,注 ...
 - 洛谷P1001 A+B Problem
			
这道题…………还是很简单!!! code: #include <iostream> #include <cstdio> using namespace std; int mai ...