题目:

Description

Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, earning the title 'Miss Cow World'. As a result, Bessie will make a tour of N (2 <= N <= 50,000) farms around the world in order to spread goodwill between farmers and their cows. For simplicity, the world will be represented as a two-dimensional plane, where each farm is located at a pair of integer coordinates (x,y), each having a value in the range -10,000 ... 10,000. No two farms share the same pair of coordinates.

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

* Line 1: A single integer, N

* Lines 2..N+1: Two space-separated integers x and y specifying coordinate of each farm

Output

* Line 1: A single integer that is the squared distance between the pair of farms that are farthest apart from each other. 

Sample Input

4
0 0
0 1
1 1
1 0

Sample Output

2

Hint

Farm 1 (0, 0) and farm 3 (1, 1) have the longest distance (square root of 2) 

题解:

凸包加旋转卡壳模板题,注意三个细节:凸包判断时注意变量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[50005],q[50005];
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[1])*(p[v]-p[1]);if(det!=0)  return det>0;return norm(p[u]-p[1])<norm(p[v]-p[1]);
}
int n,m;
int next(int i)
{if(i!=m)  return ++i;else return 1;
}
void tubao()
{int id=1;for(int i=2;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!=1)  swap(p[id],p[1]); int per[50005];for(int i=1;i<=n;i++)  per[i]=i;sort(per+2,per+n+1,comp);q[++m]=p[1];for(int i=2;i<=n;i++){int j=per[i];while(m>=2&&(q[m]-q[m-1])*(p[j]-q[m-1])<=0)  m--;q[++m]=p[j];}q[++m]=p[1];
}
int area(point u,point v,point s)
{return (u-s)*(v-s);
}
int solve()
{if(m==2)  return (norm(q[1]-q[2]));q[m+1]=q[1];int res=0;for(int i=1,j=3;i<=m;i++) {while(next(j)!=i&&area(q[i],q[i+1],q[j+1])>=area(q[i],q[i+1],q[j]))j=next(j);res=max(res,norm(q[i+1]-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=1;i<=n;i++)scanf("%d%d",&p[i].x,&p[i].y);tubao();int ans=solve();cout<<ans<<endl;
}

转载于:https://www.cnblogs.com/AseanA/p/6662407.html

算法复习——凸包加旋转卡壳(poj2187)相关推荐

  1. 凸包问题--旋转卡壳

    前情提要: 1978年,M.I.Shamos在论文<Computational Ceometry>中介绍了一种寻找凸多边形直径的线性算法. Shamos的算法就像绕着多边形旋转一对卡壳,因 ...

  2. 【LA 4728】Square, Seoul 2009 (凸包,旋转卡壳)

    Description 给定很多个正方形的定点,求这些点中两两之间距离的最大值. Solution 直接求出凸包后旋转卡壳即可.. Source /************************** ...

  3. 算法学习:计算几何旋转卡壳

    [定义] [对踵点]多边形上存在平行切线的两点 [多边形半径]多边形上任意两点的最大长度 [旋转卡壳] 选取y轴上,最高和最低的两个点,令两条平行于x轴的线切过这两点 然后我们开始让这两条线旋转 当一 ...

  4. 旋转卡壳凸包(不用一下子就学完所有)

    目录 前言 参考博客 前置知识 1.极角排序 2.凸包(默认逆时针) 3.对踵点 旋转卡壳能解决的各类问题 1.计算距离 1.1凸多边形直径 1.2凸多边形宽 1.3凸多边形间最大距离 1.4凸多边形 ...

  5. POJ - 2187 Beauty Contest (求距离最远点对-凸包+旋转卡壳/枚举 (旋转卡壳学习))

    链接:https://vjudge.net/problem/POJ-2187 题意:求求距离最远点对. 思路:肯定为凸包上的点,可枚举,也可根据凸包性质旋转卡壳求对踵点. 参考博客: https:// ...

  6. 2015百度之星初赛(1)1006 旋转卡壳

    矩形面积 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  7. 最远对踵点 旋转卡壳

    original link - http://poj.org/problem?id=2187 题意: 求最远点对 推荐 https://blog.csdn.net/wang_heng199/artic ...

  8. 旋转卡壳简介(POJ2187)(洛谷P1452)

    读音 word上是这么读的: 前置技能 凸包 算法用途 旋转卡壳可以在O(n)O(n)O(n)的时间内确定一对对踵点对,它的用途包括但不限于:计算距离(凸多边形直径).计算外接矩形(最小面积/周长). ...

  9. POj2187 【模板】旋转卡壳 / 选美大赛

    POj2187 [模板]旋转卡壳 / 选美大赛 题目描述 农夫约翰奖的牛贝西(Bessie)刚刚在牛选美比赛中获得第一名,并获得了"牛世界小姐"的头衔.结果,贝茜将参观世界各地的N ...

最新文章

  1. oracle存储过程备份,利用ORACLE存储过程与JOB结合实现对数据表自动备份
  2. mongodb集群 java_MongoDB集群JavaAPI插入数据
  3. 以太坊DAPP[2]-×××-react框架与web3实例
  4. Java与ElasticSerach的整合
  5. 牛客网_Go语言相关练习_选择题(2)
  6. bitcount java_Java源码解释之Integer.bitCount
  7. html table 充满div,HTML,使用div+css实现自适应table布局
  8. 代码行数越少就越“简单”吗?
  9. ProGuard的各种参数说明
  10. LNMP的安装(命令)
  11. eNSP常用命令 华为模拟器eNSP常用命令
  12. Kindle Fire 刷机至Android 4.2.2
  13. 用python画生日蛋糕-祝你生日快乐
  14. 背篼酥课堂-GPS定位(一) nodemcu 解析gps
  15. 从零基础到斩获BAT算法岗offer,围观复旦大佬的秋招之路
  16. 搜狗校招编程题-建房子
  17. ffmpeg 多视频 画中画
  18. 在sh_goods表中查询评分小于4的商品的不同分类id。
  19. layer 父获取子页面元素
  20. 基于NEO4J图模型的关系计算

热门文章

  1. /etc/rc.d/rc.sysinit 分析
  2. 如何修改路由器和交换机的密码
  3. Maven手动将jar包放入本地仓库
  4. 人工智能:决胜未来之道培训课程大纲
  5. linux 网卡配置详情
  6. 我自己写的3D图形数学库。。。有点乱!
  7. const变量的使用方法。。
  8. Android stdio build.gradle buildscript 里面的repositories 和allprojects里面 repositories 的区别
  9. gradle多工程打包冲突问题
  10. 高大上的Android沉浸式状态栏?