转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents
by---cxlove
题目:求出凸包的最大直径。
http://poj.org/problem?id=2187
先对多边形求凸包,以前的知识不多说。
然后用旋转卡壳求出最大直径。
其实就是两条平行线夹出凸包,旋转。
如果pa,pb为最远点对的话,那么过pa,pb的平行线经过旋转,肯定有一条与多边形的边重合。
利用这点,枚举每一条边,找到离边最远的点,看上去这还是N^2的,和枚举一样。
不过由于是凸多边形,对于某一条边,点到边的距离呈现单峰函数。
那么就可以利用上一条边的最远点依次后移得到。
这篇说得不错:http://www.cnblogs.com/xdruid/archive/2012/07/01/2572303.html
#include<iostream>
#include<fstream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<sstream>
#include<ctime>
#include<cassert>
#define LL long long
#define eps 1e-8
#define inf 999999.0
#define zero(a) abs(a)<eps
#define N 20
#define pi acos(-1.0)
using namespace std;
struct Point{
int x,y;
}p[50005];
int n;
vector<Point >s;
int dist(Point p1,Point p2){
return (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y);
}
int xmul(Point p0,Point p1,Point p2){
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
//Graham_scan求凸包
bool cmp(Point p1,Point p2){
if(xmul(p[0],p1,p2)>eps) return true;
else if(zero(xmul(p[0],p2,p1))&&dist(p[0],p1)<dist(p[0],p2)) return true;
return false;
}
void Graham_scan(){
for(int i=1;i<n;i++)
if(p[i].y<p[0].y||(p[i].y==p[0].y&&p[i].x<p[0].x))
swap(p[i],p[0]);
sort(p+1,p+n,cmp);
s.clear();
s.push_back(p[0]);s.push_back(p[1]);
for(int i=2;i<n;i++){
while(s.size()>=2&&xmul(s[s.size()-2],s[s.size()-1],p[i])<eps) s.pop_back();
s.push_back(p[i]);
}
}
//旋转卡壳求凸包直径
void Rotating_Calipers(){
int ans=0,len=s.size();
int j=1;
s.push_back(s[0]);
for(int i=0;i<len;i++){
//找到离直线最远的点
while(abs(xmul(s[i],s[i+1],s[j+1]))>abs(xmul(s[i],s[i+1],s[j])))
j=(j+1)%len;
ans=max(ans,max(dist(s[i+1],s[j]),dist(s[i],s[j])));
}
printf("%d\n",ans);
}
int main(){
while(scanf("%d",&n)!=EOF){
for(int i=0;i<n;i++)
scanf("%d%d",&p[i].x,&p[i].y);
Graham_scan();
Rotating_Calipers();
}
return 0;
}
分享到:
相关推荐
北大POJ2187-Beauty Contest 解题报告+AC代码
O(nlogn)凸包问题 poj2187
西北工业大学POJ试题C++答案代码+课程设计
北大POJ1207-The 3n + 1 problem 解题报告+AC代码
POJ-2870 Light Up + DFS(1级DFS+1级DFS) + Python - 思维导图 链接:https://blog.csdn.net/xxdragon126/article/details/122095922?spm=1001.2014.3001.5501
1011,1012,1013,1014,1015,1017,1026,1028,1032,1035,1041,1046,1129 1149 1154 1165 1182 1185 1190 1191 1201 1251 1273 1275 1276 1286 1322 1338 1363 1364 1401 1456 1459 1564 1579 1637 1657 1658 ...
北大POJ1113-Wall【凸包】 解题报告+AC代码
NULL 博文链接:https://128kj.iteye.com/blog/1749213
北大POJ初级-简单搜索 解题报告+AC代码
北大POJ3009-Curling 2.0【DFS+Vector+回溯+剪枝】 解题报告+AC代码
北大POJ3026-Borg Maze【BFS+Prim】 解题报告+AC代码
很好很强大的POJ分类 新手+进阶+题目完全分类 赶快下载
北大POJ初级-所有题目AC代码+解题报告
北大POJ1159-Palindrome 解题报告+AC代码
北大POJ3733-Changing Digits【DFS+强剪枝】 解题报告+AC代码
NULL 博文链接:https://128kj.iteye.com/blog/1750462
POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类
北大POJ2002-Squares 解题报告+AC代码
北大POJ3373-Changing Digits【DFS+强剪枝】 解题报告+AC代码
POJ2186-Popular Cows 【Tarjan+极大强连通分量+缩点】 解题报告+AC代码 http://hi.csdn.net/!s/BGDH68 附:我所有的POJ解题报告链接 . http://blog.csdn.net/lyy289065406/article/details/6642573