旋转卡壳求凸包的直径的平方

板子题

#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 , 旋转卡壳求凸包的直径的平方的更多相关文章

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

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

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

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

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

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

  4. poj 2187 Beauty Contest(凸包求解多节点的之间的最大距离)

    /* poj 2187 Beauty Contest 凸包:寻找每两点之间距离的最大值 这个最大值一定是在凸包的边缘上的! 求凸包的算法: Andrew算法! */ #include<iostr ...

  5. UVa 1453 - Squares 旋转卡壳求凸包直径

    旋转卡壳求凸包直径. 参考:http://www.cppblog.com/staryjy/archive/2010/09/25/101412.html #include <cstdio> ...

  6. POJ 2187 Beauty Contest【旋转卡壳求凸包直径】

    链接: http://poj.org/problem?id=2187 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

  7. POJ 2187 - Beauty Contest - [凸包+旋转卡壳法][凸包的直径]

    题目链接:http://poj.org/problem?id=2187 Time Limit: 3000MS Memory Limit: 65536K Description Bessie, Farm ...

  8. POJ 2187 Beauty Contest(凸包,旋转卡壳)

    题面 Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, earning the ...

  9. POJ 2187 Beauty Contest(凸包+旋转卡壳)

    Description Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, ea ...

随机推荐

  1. .NET:CLR via C# Primitive Thread Synchronization Constructs

    User-Mode Constructs The CLR guarantees that reads and writes to variables of the following data typ ...

  2. java实时监控mysql数据库变化

    对于二次开发来说,很大一部分就找找文件和找数据库的变化情况 对于数据库变化.还没有发现比较好用的监控数据库变化监控软件. 今天,我就给大家介绍一个如何使用mysql自带的功能监控数据库变化 1.打开数 ...

  3. python异常信息获取

    1.python调试的时候获取异常信息 import traceback print '######################################################## ...

  4. 线性判别分析(Linear Discriminant Analysis, LDA)算法分析

    原文来自:http://blog.csdn.net/xiazhaoqiang/article/details/6585537 LDA算法入门 一. LDA算法概述:       线性判别式分析(Lin ...

  5. DELPHI中千万别直接使用CreateThread ,建议使用BeginThread(在C++中无大问题,可是到了DELPHI中情况就不一样了)

    以前在写个别程序的时候老是喜欢使用纯API编程. 在C++中无大问题,可是到了DELPHI中情况就不一样了. 当你用 DELPHI写的多线程程序莫名其妙的内存错误,特别是字符串(string)操作;  ...

  6. Informatica 常用组件Lookup之三 关系和平面文件查找

    创建查找转换时,您可以选择使用关系表或平面文件作为查找源. 关系查找 使用关系表作为查找源来创建查找转换时,您可以使用 ODBC 连接到查找源并导入表定义作为查找转换的结构. 仅可对关系查找使用以下选 ...

  7. go语言基础之数组做函数参数是值拷贝

    1.数组做函数参数是值拷贝 示例: package main //必须有个main包 import "fmt" //数组做函数参数,它是值传递 //实参数组的每个元素给形参数组拷贝 ...

  8. iOS开发-UI基础Demo

    现在更多的学习资料都是xCode4.X的,发现xCode6.1还是很多东西,如果有正在学习iOS开发的可以通过Demo简单了解下iOS的UI开发~ 1.新建单视图文件: 2.新建项目名称,语言选择OC ...

  9. windows server 2008 远程桌面(授权、普通用户登录)~ .

    大家好,因公司上ERP系统,用户端需要远程到服务器,但大家都知道微软默认只有2个,所以没有办法达到我公司的要求. 在网上找了很久也没有找到合适的文章,要不就这里说一点,那里说一点,没有一个全的,还有很 ...

  10. UITabBarController — 标签视图控制器

    UITabBarController - 标签视图控制器 UITabBarController 分为三层结构: (1).tab bar (2.)Custom Content (3.). Tab bar ...