算法复习——凸包加旋转卡壳(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 ...
随机推荐
- 解决常见SVN冲突问题(转)
转自:http://www.w3cfuns.com/blog-5443287-5403523.html 一个大项目在开发中可能会拆分成几个小项目,分别分去,同时共通的部分再由人做,做完后再统一合并.同 ...
- GP SQL 优化
1.收集统计信息vacuum full analyze ZCXT.ZCOT_PS_PROJECT; 2.检查表的数据量分布select gp_segment_id,count(*) from fact ...
- tomcat+nginx 横向扩展
1.分别在电脑上部署两个tomcat tomcat1 tomcat2 2.不是nginx 并启动 输入 localhost 并进入nginx欢迎界面,证明部署成功 3.修改nginx 配置 ngin ...
- Android(java)学习笔记146:网页源码查看器(Handler消息机制)
1.项目框架图: 2.首先是布局文件activity_main.xml: <LinearLayout xmlns:android="http://schemas.android.com ...
- WPF中单选框RadioButton
单选框RadioButton的基本使用: <StackPanel Margin="10"> <Label FontWeight="Bold"& ...
- gEdit - GTK+ 基础文本编辑器
语法 gedit [--help] [--version] [文件名] [文件名] [文件名] 等等... 描述 gEdit 是一个 X窗口系统下的基础文本编辑器由 GTK+ 写成.它现在支持建立,打 ...
- 在idea下创建maven
之前一直用eclipse,现在要用idea写一个安装过程玩玩 一:New Project 二:选择maven,在project SDK上选择你安装的jdk,默认安装在c:/Program Files ...
- python基础一 day10(2)
复习: # 三元运算符# 接收结果的变量 = 条件为真的结果 if 条件 else 条件为假的结果# 接收结果的变量 = “真结果” if 条件 else “假结果”## 命名空间 和 作用域# 三种 ...
- 美可能排除中国大陆制造/生产的所有5G产品
https://www.wsj.com/articles/u-s-considers-requiring-5g-equipment-for-domestic-use-be-made-outside-c ...
- ios之UIScrollView
UIScrollView 类负责所有基于 UIKit 的滚动操作. 一.创建 [java] view plaincopy CGRect bounds = [ [ UIScreen mainSc ...