算法复习——凸包加旋转卡壳(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 ...
随机推荐
- 学习用5W1H来管理自己的项目/工作
学习用5W1H来管理自己的项目/工作 最近开始需要系统化的思维模型,这只是一个开始,一下用脑图的形式来简介5W1H的具体内容: 先写xmind思维树的文本导出,后面附上图片.^ _ ^ 5W1H ...
- PHP 根据两点的经纬度计算距离
/** * @name 根据经纬度确定两点的距离[地理位置] * @author tbj * @param float $lat 纬度值 * @param float $lng 经度值 * @date ...
- MySQL备份还原介绍
window系统下 1.导出整个数据库mysqldump -u 用户名 -p 数据库名 > 导出的文件名mysqldump -u dbuser -p dbname > dbname.sql ...
- Redis监控之redis-live.conf配置
{ "RedisServers": [ { "server": "192.168.1.201", "port": 637 ...
- redis 一些使用过的命令
因为我是JAVA的,所以也是用java的api 主要是文档看起来太麻烦,自己英文也不好,每次用之前都要看一遍,自己把常用的一点点的放进来,方便使用 分布式连接池对象配置 JedisPoolConfig ...
- maven打包错误:No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
[INFO] Scanning for projects...[INFO] ...
- Vue 前端面试题[转]
https://mp.weixin.qq.com/s/Uxhx2dJ1Xbm6N3Gl7wNZNw Vue 前端面试题 游荡de蝌蚪 前端开发 1周前 作者:游荡de蝌蚪 https://segmen ...
- 数据库_5_MySQL数据库介绍
一.MySQL数据库 MySQL:MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品.MySQL 是最流行的关系型数据库管理系统之一,在 WEB ...
- Perl 使用哈希的引用
$ref = \%hash_clomnname_linevalue; $hash_of_whole_table{$table_name} = {%$ref};
- Kernel Stack Overflow(转)
0x00 漏洞代码 stack_smashing.c #include <linux/init.h> #include <linux/module.h> #include &l ...