嘟嘟嘟




旋转卡壳模板题。




首先求出凸包。

然后\(O(n ^ 2)\)的算法很好想,但那就不叫旋转卡壳了。

考虑优化:直观的想是在枚举点的时候,对于第二层循环用二分或者三分优化,但实际上两点距离是不满足单调性的,见下图:



对于\(A\)点,\(AB < AC < AD > AE < AF\)。

那怎么办呢?

转换一下思路,如果枚举边,会发现每一个不在这条边上的顶点到边的距离是一个单峰函数!因此就能想到三分这个点,复杂度变成\(O(nlogn)\)。

不过实际上还可以优化,如果逆时针枚举的话,对于边\(e_i\)的下一条边\(e_{i + 1}\),会发现到\(e_{i + 1}\)的最远点一定在\(e _ i\)的最远点的逆时针方向。换句话说,如果边是逆时针枚举的,那么最远点也是逆时针方向的。

因此维护两个指针,一个代表边,一个代表最远点。因为这两个指针最多转一圈,所以复杂度为\(O(n)\)。

一个优化就是判断距离的时候,因为底边是固定的,所以比较距离就是在比较三角形面积。(还能防止掉精度)

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 5e4 + 5;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
} int n;
struct Point
{
int x, y;
Point operator - (const Point& oth)const
{
return (Point){x - oth.x, y - oth.y};
}
int operator * (const Point& oth)const
{
return x * oth.y - oth.x * y;
}
friend inline int dis(const Point& A)
{
return A.x * A.x + A.y * A.y;
}
inline friend void swap(Point& A, Point& B)
{
swap(A.x, B.x); swap(A.y, B.y);
}
}p[maxn], S; bool cmp(Point A, Point B)
{
int s = (A - S) * (B - S);
if(s != 0) return s > 0;
return dis(A - S) < dis(B - S);
} int st[maxn], top = 0;
void Graham()
{
int id = 1;
for(int i = 2; i <= n; ++i)
if(p[i].x < p[id].x || (p[i].x == p[id].x && p[i].y < p[id].y)) id = i;
if(id != 1) swap(p[id], p[1]);
S.x = p[1].x, S.y = p[1].y;
sort(p + 2, p + n + 1, cmp);
st[++top] = 1;
for(int i = 2; i <= n; ++i)
{
while(top > 1 && (p[st[top]] - p[st[top - 1]]) * (p[i] - p[st[top - 1]]) < 0) top--;
st[++top] = i;
}
} int area(Point A, Point B, Point C)
{
return abs((A - B) * (A - C));
}
int nxt(int x)
{
if(++x > top) x = 1;
return x;
}
int rota()
{
if(top == 2) return dis(p[st[1]] - p[st[2]]);
int ret = 0;
st[top + 1] = 1;
for(int i = 1, j = 3; i <= top; ++i)
{
while(nxt(j) != i && area(p[st[i]], p[st[i + 1]], p[st[j]]) <= area(p[st[i]], p[st[i + 1]], p[st[j + 1]])) j = nxt(j);
ret = max(ret, dis(p[st[i]] - p[st[j]]));
ret = max(ret, dis(p[st[i + 1]] - p[st[j]]));
}
return ret;
} int main()
{
n = read();
for(int i = 1; i <= n; ++i) p[i].x = read(), p[i].y = read();
Graham();
write(rota()), enter;
return 0;
}

POJ2187 Beauty Contest(旋转卡壳)的更多相关文章

  1. poj2187 Beauty Contest(旋转卡壳)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Beauty Contest Time Limit: 3000MS   Memor ...

  2. poj 2187:Beauty Contest(旋转卡壳)

    Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 32708   Accepted: 10156 Description Bes ...

  3. poj 2187 Beauty Contest , 旋转卡壳求凸包的直径的平方

    旋转卡壳求凸包的直径的平方 板子题 #include<cstdio> #include<vector> #include<cmath> #include<al ...

  4. poj 2187 Beauty Contest——旋转卡壳

    题目:http://poj.org/problem?id=2187 学习材料:https://blog.csdn.net/wang_heng199/article/details/74477738 h ...

  5. P1452 Beauty Contest 旋转卡壳

    \(\color{#0066ff}{题目描述}\) 贝茜在牛的选美比赛中赢得了冠军"牛世界小姐".因此,贝西会参观N(2 < = N < = 50000)个农场来传播善 ...

  6. poj 2187 Beauty Contest —— 旋转卡壳

    题目:http://poj.org/problem?id=2187 学习资料:https://blog.csdn.net/wang_heng199/article/details/74477738 h ...

  7. POJ-2187 Beauty Contest,旋转卡壳求解平面最远点对!

     凸包(旋转卡壳) 大概理解了凸包A了两道模板题之后在去吃饭的路上想了想什么叫旋转卡壳呢?回来无聊就搜了一下,结果发现其范围真广. 凸包: 凸包就是给定平面图上的一些点集(二维图包),然后求点集组成的 ...

  8. poj2187 Beauty Contest (凸包 + 旋转卡壳)

    Beauty Contest Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 38349   Accepted: 11851 ...

  9. POJ2187 Beauty Contest (旋转卡壳算法 求直径)

    POJ2187 旋转卡壳算法如图 证明:对于直径AB 必然有某一时刻 A和B同时被卡住 所以旋转卡壳卡住的点集中必然存在直径 而卡壳过程显然是O(n)的 故可在O(n)时间内求出直径 凸包具有良好的性 ...

  10. [USACO2003][poj2187]Beauty Contest(凸包+旋转卡壳)

    http://poj.org/problem?id=2187 题意:老题了,求平面内最远点对(让本渣默默想到了悲剧的AHOI2012……) 分析: nlogn的凸包+旋转卡壳 附:http://www ...

随机推荐

  1. 【13】MD5编码、Zlib压缩解压缩

    1.MD5加密 /// <summary> /// 使用MD5加密算法 /// </summary> /// <param name="md5MessageSt ...

  2. C#Winform实时更新数据库信息Demo(使用Scoket)

    最近在贴吧上看到有个提问就是关于怎么在Winform上实时的更新数据 提问者提到的是利用Timer去轮询,但最后经过网上查了下资料,感觉Socket也是可行的, 于是就写了这个Demo 这个Demo的 ...

  3. 九、sparkStream的scala示例

    简介 sparkStream官网:http://spark.apache.org/docs/latest/streaming-programming-guide.html#overview spark ...

  4. 动态页面技术之JSP

    1.什么是JSP技术 JSP全名为Java Server Pages,中文名叫java服务器页面,其根本是一个简化的Servlet设计,它是由Sun Microsystems公司倡导.许多公司参与一起 ...

  5. oracle数据库字符集和客户端字符集(2%)是不同的,字符集转化可能会造成不可预期的后果

    转载请在文章显眼位置注明出处:https://www.cnblogs.com/sunshine5683/p/10036321.html 今天在plsql连接oracle时候报错提示“数据库字符集和客户 ...

  6. String拾遗

    简介: String作为日常最常用的类,还是有必要对其中的细节做一些了解的,这篇就结合源码来看看这个常用的类. 一. 总述 类图如下: 从图中可以看到String是实现了 java.io.Serial ...

  7. C++学习笔记: 智能指针

    c++ 智能指针学习新的 class Simple { public: Simple() { number = param; std::cout << "Simple: &quo ...

  8. css3 伪元素和伪类选择器详解

    转自脚本之家:http://www.jb51.net/css/213779.html 无论是伪类还是伪元素,都属于CSS选择器的范畴.所以它们的定义可以在CSS标准的选择器章节找到.分别是 CSS2. ...

  9. drupal7 为视图添加 过滤标准 内容类型

    1.单击 FILTER CRITERIA 右边的“添加”按钮 2.在弹出的对话框中输入“类型”,单击搜索结果中的“内容:类型” 3.确定之后,选择需要的内容类型即可,例如添加“书评”内容类型的过滤 4 ...

  10. php里单引和双引的用法区别和连接符(.)

    " "双引号里面的字段会经过编译器解释,然后再当作HTML代码输出. ' '单引号里面的不进行解释,直接输出. 例如: $abc='my name is tome'; echo $ ...