POJ 2187 Beauty Contest (凸包最远点距旋转卡壳)

2024-03-05 19:32

本文主要是介绍POJ 2187 Beauty Contest (凸包最远点距旋转卡壳),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

http://poj.org/problem?id=2187


思路:算出凸包后枚举凸包上的点。复杂度为O(NlogN+M)

为什么可以枚举?

设坐标的绝对值不超过M,则凸包至多有O(√M)个顶点

证明:以(0,0)为起点画出如下“极限凸包”

(0,0)-(1,0)-(2,1)-(3,3)-(4,6)-...当x每次只增加1时,y增加速率是平方级的,所以凸包至多有O(√M)个顶点。


另一种思路就是用旋转卡壳(RC)算法。复杂度为O(NlogN+√M)


枚举算法的代码:

/*79ms,956KB*/#include<cstdio>
#include<algorithm>
using namespace std;
const int mx = 50005;struct P
{int x, y;P(int x = 0, int y = 0): x(x), y(y) {}void read(){scanf("%d%d", &x, &y);}P operator - (P& p){return P(x - p.x, y - p.y);}bool operator < (const P& p) const///加cosnt以便sort调用,其他函数不加const对速度没有影响{return x < p.x || x == p.x && y < p.y;}int dot(P p){return x * p.x + y * p.y;}int det(P p){return x * p.y - y * p.x;}
};P p[mx], ans[mx];
int n, len;///求凸包
void convex_hull()
{sort(p, p + n);len = 0;int i;for (i = 0; i < n; ++i){while (len >= 2 && (ans[len - 1] - ans[len - 2]).det(p[i] - ans[len - 1]) <= 0)--len;ans[len++] = p[i];}int tmp = len;for (i = n - 2; i >= 0; --i){while (len > tmp && (ans[len - 1] - ans[len - 2]).det(p[i] - ans[len - 1]) <= 0)--len;ans[len++] = p[i];}--len;
}int main()
{int i, j;scanf("%d", &n);for (i = 0; i < n; ++i) p[i].read();convex_hull();int mxdis = 0;for (i = 1; i < len; ++i)for (j = 0; j < i; ++j)mxdis = max(mxdis, (ans[i] - ans[j]).dot(ans[i] - ans[j]));printf("%d\n", mxdis);return 0;
}

旋转卡壳算法的代码:(速度差不多,看来是排序占用了大量时间)

/*79ms,956KB*/#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int mx = 50005;struct P
{int x, y;P(int x = 0, int y = 0): x(x), y(y) {}void read(){scanf("%d%d", &x, &y);}P operator - (P p){return P(x - p.x, y - p.y);}bool operator < (const P& p) const{return x < p.x || x == p.x && y < p.y;}int dot(P p){return x * p.x + y * p.y;}int det(P p){return x * p.y - y * p.x;}
};P p[mx], ans[mx];
int n, len;///求凸包
void convex_hull()
{sort(p, p + n);len = 0;int i;for (i = 0; i < n; ++i){while (len >= 2 && (ans[len - 1] - ans[len - 2]).det(p[i] - ans[len - 1]) <= 0)--len;ans[len++] = p[i];}int tmp = len;for (i = n - 2; i >= 0; --i){while (len > tmp && (ans[len - 1] - ans[len - 2]).det(p[i] - ans[len - 1]) <= 0)--len;ans[len++] = p[i];}--len;memcpy(p, ans, sizeof(P) * len);
}inline int next(int i)
{return (i + 1) % len;
}int RC(int si, int sj)
{int mxdis = 0, i = si, j = sj;while (i != sj || j != si){mxdis = max(mxdis, (p[i] - p[j]).dot(p[i] - p[j]));if ((p[next(i)] - p[i]).det(p[next(j)] - p[j]) > 0) j = next(j);else i = next(i);}return mxdis;
}int main()
{int i, si = 0, sj = 0;scanf("%d", &n);for (i = 0; i < n; ++i) p[i].read();convex_hull();if (len == 2) printf("%d\n", (p[0] - p[1]).dot(p[0] - p[1]));else{for (i = 0; i < len; ++i){if (!(p[si] < p[i])) si = i;if (p[sj] < p[i]) sj = i;}printf("%d\n", RC(si, sj));}return 0;
}

这篇关于POJ 2187 Beauty Contest (凸包最远点距旋转卡壳)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/777581

相关文章

计算绕原点旋转某角度后的点的坐标

问题: A点(x, y)按顺时针旋转 theta 角度后点的坐标为A1点(x1,y1)  ,求x1 y1坐标用(x,y)和 theta 来表示 方法一: 设 OA 向量和x轴的角度为 alpha , 那么顺时针转过 theta后 ,OA1 向量和x轴的角度为 (alpha - theta) 。 使用圆的参数方程来表示点坐标。A的坐标可以表示为: \[\left\{ {\begin{ar

KLayout ------ 旋转物体90度并做平移

KLayout ------ 旋转创建的物体 正文 正文 前段时间,有个小伙伴留言问我,KLayout 中如何旋转自己创建的物体,这里特来说明一下。 import pyapoly = pya.DPolygon([pya.DPoint(0, 0), pya.DPoint(0, 5), pya

剑指offer(C++)--左旋转字符串

题目 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZdef”,要求输出循环左移3位后的结果,即“XYZdefabc”。是不是很简单?OK,搞定它! class Solution {public:string LeftRotateStri

poj 3882(Stammering Aliens) 后缀数组 或者 hash

后缀数组:  构建后缀数组,注意要在字符串莫末尾加上一个没出现过的字符。然后可以2分或者直接扫描,直接扫描需要用单调队列来维护 VIEW CODE #include<cstdio>#include<algorithm>#include<iostream>#include<cmath>#include<queue>#include<stack>#include<string

poj 3294(Life Forms) 2分+ 后缀数组

我曾用字符串hash写,但是超时了。只能用后最数组了。大致思路:用不同的符号吧字符串连接起来,构建后缀数组,然后2分答案,依次扫描后缀数组,看是否瞒住条件。 VIEW CODE #include<cstdio>#include<vector>#include<cmath>#include<algorithm>#include<cstring>#include<cassert>#

poj 2391 Ombrophobic Bovines (网络流)

这是一道很经典的网络流的题目。首先我们考虑假如我们的时间为无穷大。我们吧每个点拆成2个点 i和i' .。虚拟源点s和汇点t。对于每个点建边(s,i, a[i])  (i‘,t,ib[i]) 。 其中a[i]为给点有多少牛,b[i]为容量。i和j连通 建边 (i,j',inf);如果最大流==所有牛的个数,就可能装下所有的牛。那么现在我们考虑时间。假设最大时间为T.那么如果i到j的的最短时间>T

poj 1330 LCA 最近公共祖先

水题目。直接上代码了。 VIEW CODE #include<cstdio>#include<algorithm>#include<iostream>#include<cmath>#include<queue>#include<stack>#include<string>#include<cstring>#include<map>#include<vector>#

poj 3160 Father Christmas flymouse 强连通+dp

首先我们可以确定的是,对于val值小于0的节点都变成0.   假设一个集合内2个房间都能任意到达,那么我就可以吧集合内的所有点的价值都取到,并且可以达到任一点。实际上集合内的每个点是相同的,这样的集合就是一个强连通分量。 那么我们就可以用tarjin算法进行强连通缩点, 最后形成一个dag的图。在dag的图上面进行dp。可以先用拓扑排序后dp。或者建反响边记忆化搜索 。 VIEW

Codeforces April Fools Day Contest 2014(附官方题解)

Codeforces2014年愚人节的坑题。。。但还是感觉挺好玩的。。。 A. The Great Game time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Two teams mee

Codeforces April Fools Day Contest 2013

2013年愚人节的坑题。。。 A. Mysterious strings time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Input The input contains a sin