[luogu1452]Beauty Contest【凸包+旋转卡壳】
题目大意
求出平面最远点对距离的平方。
分析
此题我wa了好久,第一是凸包写错了,后面又是旋转卡壳写错了。。自闭3s。
题解应该是旋转卡壳,但是有人用随机化乱搞过掉了Orz。
讲讲正解。
我们先求出所有点的凸包,然后每一次更新对踵点,就像一个尺子一样卡着这个凸包的每一条边,然后计算两个点对之间的距离就可以了。
代码(借鉴了一下别人的代码)
#include <bits/stdc++.h>
#define ll long long
#define ms(a, b) memset(a, b, sizeof(a))
#define inf 0x3f3f3f3f
#define db double
#define N 50005
#define Vector2 Point
using namespace std;
template <typename T>
inline void read(T &x) {
x = 0; T fl = 1; char ch = 0;
for (; ch < '0' || ch > '9'; ch = getchar())
if (ch == '-') fl = -1;
for (; ch >= '0' && ch <= '9'; ch = getchar())
x = (x << 1) + (x << 3) + (ch ^ 48);
x *= fl;
}
template <typename T> T sqr(T x) { return x * x; }
struct Point {
ll x, y;
}P[N], S[N];
int tot = 0;
ll Dist(Point p1, Point p2) {
return sqr(p1.x - p2.x) + sqr(p1.y - p2.y);
}
Vector2 operator -(Vector2 v1, Vector2 v2) {
return (Vector2){v1.x - v2.x, v1.y - v2.y};
}
ll Cross(Vector2 v1, Vector2 v2) {
return v1.x * v2.y - v1.y * v2.x;
}
bool cmp(Point p1, Point p2) {
if (Cross(p1 - P[1], p2 - P[1]) < 0) return 0;
if (Cross(p1 - P[1], p2 - P[1]) > 0) return 1;
return Dist(P[1], p1) < Dist(P[1], p2);
}
bool check(Point p1, Point p2, Point p3, Point p4) {
return Cross(p2 - p1, p4 - p3) <= 0;
}
void Get_Hull(Point *P, int n) {
sort(P + 2, P + 1 + n, cmp);
tot = 0;
S[++ tot] = P[1];
for (int i = 2; i <= n; i ++) {
while (tot > 1 && check(S[tot - 1], S[tot], S[tot], P[i])) -- tot;
S[++ tot] = P[i];
}
S[tot + 1] = S[1];
}
int n;
ll Rotate_Get_ans() {
if (tot == 1) return 0;
if (tot == 2) return Dist(S[1], S[2]);
ll j = 2, ans = 0;
for (int i = 1; i <= tot; i ++) {
while (Cross(S[i + 1] - S[i], S[j] - S[i + 1]) <= Cross(S[i + 1] - S[i], S[j + 1] - S[i + 1])) j = j == tot ? 1 : j + 1;
ans = max(ans, max(Dist(S[i], S[j]), Dist(S[i + 1], S[j])));
}
return ans;
}
int main() {
read(n);
for (int i = 1; i <= n; i ++) {
scanf("%lld%lld", &P[i].x, &P[i].y);
if (P[i].y < P[1].y || (P[i].y == P[1].y && P[i].x < P[1].x)) swap(P[i], P[1]);
}
Get_Hull(P, n);
printf("%lld\n", Rotate_Get_ans());
return 0;
}
[luogu1452]Beauty Contest【凸包+旋转卡壳】的更多相关文章
- POJ 2187 - Beauty Contest - [凸包+旋转卡壳法][凸包的直径]
题目链接:http://poj.org/problem?id=2187 Time Limit: 3000MS Memory Limit: 65536K Description Bessie, Farm ...
- Beauty Contest 凸包+旋转卡壳法
Beauty Contest Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 27507 Accepted: 8493 D ...
- POJ 2187 Beauty Contest [凸包 旋转卡壳]
Beauty Contest Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 36113 Accepted: 11204 ...
- 【POJ 2187】Beauty Contest 凸包+旋转卡壳
xuán zhuǎn qiǎ ké模板题 是这么读吧(≖ ‿ ≖)✧ 算法挺简单:找对踵点即可,顺便更新答案. #include<cstdio> #include<cstring&g ...
- 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(旋转卡壳)
http://poj.org/problem?id=2187 显然直径在凸包上(黑书上有证明).(然后这题让我发现我之前好几次凸包的排序都错了QAQ只排序了x轴.....没有排序y轴.. 然后本题数据 ...
- POJ-2187 Beauty Contest,旋转卡壳求解平面最远点对!
凸包(旋转卡壳) 大概理解了凸包A了两道模板题之后在去吃饭的路上想了想什么叫旋转卡壳呢?回来无聊就搜了一下,结果发现其范围真广. 凸包: 凸包就是给定平面图上的一些点集(二维图包),然后求点集组成的 ...
- POJ2187 Beauty Contest(旋转卡壳)
嘟嘟嘟 旋转卡壳模板题. 首先求出凸包. 然后\(O(n ^ 2)\)的算法很好想,但那就不叫旋转卡壳了. 考虑优化:直观的想是在枚举点的时候,对于第二层循环用二分或者三分优化,但实际上两点距离是不满 ...
- POJ2187 Beauty Contest (旋转卡壳算法 求直径)
POJ2187 旋转卡壳算法如图 证明:对于直径AB 必然有某一时刻 A和B同时被卡住 所以旋转卡壳卡住的点集中必然存在直径 而卡壳过程显然是O(n)的 故可在O(n)时间内求出直径 凸包具有良好的性 ...
- poj 2187 Beauty Contest(二维凸包旋转卡壳)
D - Beauty Contest Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
随机推荐
- 运行Maven项目时出现invalid LOC header (bad signature)错误,Tomcat不能正常启动
作为Maven小白,今天这问题困扰了我好久,经过多次在网上查询,终于找到了原因.明明一个小问题却耗费很多时间,着实不应该,所以必须记录一下. 报错信息如下: 对话框: 控制台: <span st ...
- hadoop实例-网站用户行为分析
一.数据集 网站用户购物行为数据集2030万条,包括raw_user.csv(2000万条)和small_user.csv(30万条,适合新手) 字段说明: user_id 用户编号,item_id ...
- CentOS6.5配置 cron
CentOS6.5配置 cron 任务 - mengjiaoduan的博客 - CSDN博客https://blog.csdn.net/mengjiaoduan/article/details/649 ...
- vue组件封装选项卡
<template> <myMenu :arr='arr' :arrcontent='content'></myMenu> </template> &l ...
- 谷歌浏览器报错 Active resource loading counts reached to a per-frame
Active resource loading counts reached to a per-frame limit while the tab is in background. Network ...
- APP-SERVICE-SDK:setStorageSync:fail;at page/near/pages/shops/shops page lifeCycleMethod onUnload function
APP-SERVICE-SDK:setStorageSync:fail;at page/near/pages/shops/shops page lifeCycleMethod onUnload fun ...
- rem 自适应、整体缩放
html{ font-size: calc(100vw/7.5); } 说明: 100vw是设备的宽度,除以7.5可以让1rem的大小在iPhone6下等于100px. 若是低版本的设备不支持rem, ...
- 接触Struts2的ModelDriven<>接口
最近在学SSH框架,实战项目,用到了Struts2的ModelDriven<>接口,在这做一点记录 ModelDriven,意为模型驱动,意思是直接把实体类当成页面数据的收集对象 参考他人 ...
- 利用 ajax自定义Form表单的提交方式
需求场景:有时候单纯的form表单无法向后端传递额外的参数 比如需要action传递js异步生成的参数 ,form表单默认的action就无法满足需求,这时就需要我们自定义form表单的提交方式. h ...
- indexOf刚开始写成IndexOf出错
{{# if(d.fronturlmin ==null||d.fronturlmin ==""){ }} <img src="@System.Configurati ...