poj 2187 Beauty Contest , 旋转卡壳求凸包的直径的平方
旋转卡壳求凸包的直径的平方
板子题
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std; struct Point {
int x, y;
Point(int x=0, int y=0):x(x),y(y) { }
}; typedef Point Vector; Vector operator - (const Point& A, const Point& B) {
return Vector(A.x-B.x, A.y-B.y);
} int Cross(const Vector& A, const Vector& B) {
return A.x*B.y - A.y*B.x;
} int Dot(const Vector& A, const Vector& B) {
return A.x*B.x + A.y*B.y;
} int Dist2(const Point& A, const Point& B) {
return (A.x-B.x)*(A.x-B.x) + (A.y-B.y)*(A.y-B.y);
} bool operator < (const Point& p1, const Point& p2) {
return p1.x < p2.x || (p1.x == p2.x && p1.y < p2.y);
} bool operator == (const Point& p1, const Point& p2) {
return p1.x == p2.x && p1.y == p2.y;
} // 点集凸包
// 假设不希望在凸包的边上有输入点,把两个 <= 改成 <
// 注意:输入点集会被改动
vector<Point> ConvexHull(vector<Point>& p) {
// 预处理。删除反复点
sort(p.begin(), p.end());
p.erase(unique(p.begin(), p.end()), p.end()); int n = p.size();
int m = 0;
vector<Point> ch(n+1);
for(int i = 0; i < n; i++) {
while(m > 1 && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) m--;
ch[m++] = p[i];
}
int k = m;
for(int i = n-2; i >= 0; i--) {
while(m > k && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) m--;
ch[m++] = p[i];
}
if(n > 1) m--;
ch.resize(m);
return ch;
} //返回点集直径的平方
int diameter2(vector<Point> & points) {
vector<Point> p = ConvexHull(points);
int n = p.size();
if(n==1) return 0;
if(n==2) return Dist2(p[0], p[1]);
p.push_back(p[0]);
int ans = 0;
for(int u = 0, v = 1; u < n; ++u) {
for(;;) {
int diff = Cross(p[u+1]-p[u], p[v+1]-p[v]);
if(diff<=0) {
ans = max(ans, Dist2(p[u], p[v]));
if(diff==0) ans = max(ans, Dist2(p[u], p[v+1]));
break;
}
v = (v+1) % n;
}
}
return ans;
}
int main() {
int n;
scanf("%d", &n);
vector<Point> P;
for(int i=0; i<n; ++i) {
int x, y;
scanf("%d%d", &x, &y);
P.push_back(Point(x, y));
}
printf("%d\n", diameter2(P));
return 0;
}
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 —— 旋转卡壳
题目: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 ...
- UVa 1453 - Squares 旋转卡壳求凸包直径
旋转卡壳求凸包直径. 参考:http://www.cppblog.com/staryjy/archive/2010/09/25/101412.html #include <cstdio> ...
- 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 Time Limit: 3000MS Memory Limit: 65536K Description Bessie, Farm ...
- 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 ...
随机推荐
- 基于curl的异步http实现
简述用于windowsclient的一个异步http模块的实现 1.须要实现的feature 1.1 非常easy地发起异步http请求,然后回调. 1.2 可以管理http并发数. 1.3 可以支持 ...
- Java JDBC 基础知识
一.JDBC常用接口.类介绍 JDBC提供对独立于数据库统一的API,用以执行SQL命令.API常用的类.接口如下: DriverManager 管理JDBC驱动的服务类,主要通过它获取Connect ...
- Android图片加载框架最全解析(六),探究Glide的自定义模块功能
不知不觉中,我们的Glide系列教程已经到了第六篇了,距离第一篇Glide的基本用法发布已经过去了半年的时间.在这半年中,我们通过用法讲解和源码分析配合学习的方式,将Glide的方方面面都研究了个遍, ...
- [Android Pro] 控制硬加速 hardwareAccelerated 在3.0才有的
从Android3.0 (API level11)开始,Android的2D显示管道被被设计得更加支持硬加速了.硬加速使用GPU承担了所有在View的canvas上执行的绘制操作. 启用硬加速最简单的 ...
- Android制作曲线、柱状图、饼形等图表——使用AChartEngine
之前在java开发中实现图表使用JFreeChar组件,最近有个小项目要求在Android端进行数据分析,如何实现图表呢?查了一下google提供了一个开源组件Achartengine非常好用,可实现 ...
- mac利用Synergy操作多台电脑
话说,我现在桌子上有3台电脑,但是我只有一个鼠标和键盘,我该怎么玩呢,就像win一样,可以外接一个显示器,鼠标到达了显示器边缘自动翻越到另一个显示器上,这个没问题,win已经实现了. 我今天推荐一款牛 ...
- E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?
sudo rm /var/lib/dpkg/locksudo dpkg --configure -a
- 程序员们必备的10款免费jquery插件
本周带来10款免费的jquery插件.如果你也有好的作品,欢迎分享到社区中来,在得到帮助的同时,也能与更多人分享来自你的作品. jQuery导航菜单置顶插件 - stickyUp . 在线演示 sti ...
- java编程思想 第四版 第六章 个人练习
欢迎加群:239063848 进群须知:本群仅用于技术分享与交流.问题公布与解答 禁止闲聊.非诚勿扰 练习1:(1)在某个包中创建一个类,在这个类所处的包的外部创建该类的一个实例. import mi ...
- 【转载】Remote System Explorer Operation总是运行后台服务,卡死eclipse解决办法
原来是eclipse后台进程在远程操作,就是右下角显示的“Remote System Explorer Operation”.折腾了半天,在Stack Overflow找到答案(源地址).把解决方案翻 ...