【POJ】2187 Beauty Contest(旋转卡壳)
http://poj.org/problem?id=2187
显然直径在凸包上(黑书上有证明)。(然后这题让我发现我之前好几次凸包的排序都错了QAQ只排序了x轴。。。。。没有排序y轴。。
然后本题数据水,暴力也能过。。。
(之前一直以为距离是单增的,其实并不是,应该是三角形面积单增...)
考虑旋转卡壳
一篇好的文章:http://www.cnblogs.com/Booble/archive/2011/04/03/2004865.html
首先对踵点就是两条平行线夹紧凸包的两个点(或者3个点或4个点,平行线过两个点情况,凸包去掉三点共线)时的点对,可以证明对踵点对最多只有3N/2个
首先卡住一点-两点(即边)可以等效于卡主一点-一点(前边的点),所以我们只需要找边的前一个点的对踵点即可。
性质1:对踵点对之间的距离最大
证明:黑书上有...
因此我们只需要枚举每条边,找出对应的对踵点(用叉积求面积来找,这条边与其它点的面积是单峰的,然后往后递推决策是单调不降的)
性质2:当枚举边按序枚举时,对踵点的位置单增
证明:不会QAQ似乎黑书上也有?
这就提供了一个很好的性质,即我们枚举边时维护一下对踵点的位置即可,那么查找对踵点的复杂度均摊$O(n)$
性质3:每个点的对踵点不一定只有1个
证明:显然吧。。。
由于性质3的存在,我们考虑是否我们枚举边找到第一个对踵点就更新的算法会出现问题?
答案是不会。
因为我们枚举边时已经遍历了所有点,因此遗漏的对踵点的距离总是会计算到
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#include <map>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define mkpii make_pair<int, int>
#define pdi pair<double, int>
#define mkpdi make_pair<double, int>
#define pli pair<ll, int>
#define mkpli make_pair<ll, int>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }
#define printarr1(a, b) for1(_, 1, b) cout << a[_] << '\t'; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=50005;
struct dat { int x, y; }a[N], b[N]; int cha(const dat &a, const dat &b, const dat &c) {
static int x1, x2, y1, y2;
x1=a.x-c.x, x2=b.x-c.x, y1=a.y-c.y, y2=b.y-c.y;
return x1*y2-x2*y1;
}
int n;
bool cmp(const dat &a, const dat &b) { return a.x==b.x?a.y<b.y:a.x<b.x; }
void tu() {
sort(a+1, a+1+n, cmp);
int top=0;
for1(i, 1, n) {
while(top>1 && cha(a[i], b[top], b[top-1])>=0) --top;
b[++top]=a[i];
}
int k=top;
for3(i, n-1, 1) {
while(top>k && cha(a[i], b[top], b[top-1])>=0) --top;
b[++top]=a[i];
}
if(n>1) --top;
n=top;
}
int sqr(const int x) { return x*x; }
int dis(const dat &a, const dat &b) { return sqr(a.x-b.x)+sqr(a.y-b.y); } int main() {
read(n);
for1(i, 1, n) read(a[i].x), read(a[i].y);
tu();
int ans=0;
b[n+1]=b[1];
int j=2;
for1(i, 1, n) {
while(cha(b[i+1], b[j+1], b[i])>cha(b[i+1], b[j], b[i])) j=j%n+1;
ans=max(ans, dis(b[i], b[j]));
}
printf("%d\n", ans);
return 0;
}
|
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 Sample Output 2 Hint Farm 1 (0, 0) and farm 3 (1, 1) have the longest distance (square root of 2)
Source |
【POJ】2187 Beauty Contest(旋转卡壳)的更多相关文章
- poj 2187:Beauty Contest(旋转卡壳)
Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 32708 Accepted: 10156 Description Bes ...
- poj 2187 Beauty Contest——旋转卡壳
题目:http://poj.org/problem?id=2187 学习材料:https://blog.csdn.net/wang_heng199/article/details/74477738 h ...
- poj 2187 Beauty Contest , 旋转卡壳求凸包的直径的平方
旋转卡壳求凸包的直径的平方 板子题 #include<cstdio> #include<vector> #include<cmath> #include<al ...
- poj 2187 Beauty Contest —— 旋转卡壳
题目:http://poj.org/problem?id=2187 学习资料:https://blog.csdn.net/wang_heng199/article/details/74477738 h ...
- poj 2187 Beauty Contest(凸包求解多节点的之间的最大距离)
/* poj 2187 Beauty Contest 凸包:寻找每两点之间距离的最大值 这个最大值一定是在凸包的边缘上的! 求凸包的算法: Andrew算法! */ #include<iostr ...
- poj 2187 Beauty Contest (凸包暴力求最远点对+旋转卡壳)
链接:http://poj.org/problem?id=2187 Description Bessie, Farmer John's prize cow, has just won first pl ...
- POJ 2187 - Beauty Contest - [凸包+旋转卡壳法][凸包的直径]
题目链接:http://poj.org/problem?id=2187 Time Limit: 3000MS Memory Limit: 65536K Description Bessie, Farm ...
- POJ 2187 Beauty Contest【旋转卡壳求凸包直径】
链接: http://poj.org/problem?id=2187 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...
- POJ 2187 Beauty Contest(凸包,旋转卡壳)
题面 Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, earning the ...
- POJ 2187 Beauty Contest(凸包+旋转卡壳)
Description Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, ea ...
随机推荐
- Linux rsync 命令详解
服务器之间常常要保持些文件或目录的一致,比如一些大的软件下载网站,它们通常使用多台服务器来提供下载服务.当一台服务器上的文件更新后,其它的服务器 也需要更新,而且 在更新的时候应该是只对新增或是修改过 ...
- Linux 命令行生成随机密码的十种方法
Linux操作系统的一大优点是对于同样一件事情,你可以使用高达数百种方法来实现它.例如,你可以通过数十种方法来生成随机密码.本文将介绍生成随机密码的十种方法.这些方法均收集于Command-Line ...
- 最长公共子串 NYOJ 36
http://acm.nyist.net/JudgeOnline/problem.php?pid=36 最长公共子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 ...
- 《ASP.NET1200例》解决母版页报错“内容控件必须是内容页中的顶级控件,或是引用母版页的嵌套母版页。”
VS2005下,添加了母版页这个控件,我们可以讲N个页面中共同的部分放在母版页来实现,并让WEB窗体集成自我们的母版页,就可以让我们的站点具有统一的风格了.在VS2005SP1之前的版本中,我们只能创 ...
- css 属性
部分属性在firefox 中添加-moz- safari 以及chrome 加上-webkit- opera 加上-o- ie9里可能需要-ms- jquery 中的css 操作 和一般的cs ...
- Http 协议Header
Responses 部分 Header 解释 示例 Accept-Ranges 表明服务器是否支持指定范围请求及哪种类型的分段请求 Accept-Ranges: bytes Age 从原始服务器到代理 ...
- 毫秒数转换为指定格式日期的js代码
var format = function(time, format){ var t = new Date(time); var tf = function(i){return (i < 10 ...
- MFC 相关文件夹、文件操作
//关于文件(夹)操作,可以参考下SHFileOperation这个外壳函数,貌似可以显示进度条.以下没有使用SHFileOperation//删除一个文件夹下的所有内容void myDeleteDi ...
- Adaboost算法结合Haar-like特征
Adaboost算法结合Haar-like特征 一.Haar-like特征 目前通常使用的Haar-like特征主要包括Paul Viola和Michal Jones在人脸检测中使用的由Papageo ...
- IOS项目自动构建
# Sets the target folders and the final framework product. # 如果工程名称和Framework的Target名称不一样的话,要自定义FMKN ...