poj2187
求最远点对,这是一道经典的旋转卡壳的题目
话说由于是前年写的,之后就没怎么研究过计算几何了……
感觉都不大记得清了,来稍微回忆一下……
首先最远点对一定出现在凸包上显然,然后穷举肯定不行,这时候就需要旋转卡壳了
http://www.cnblogs.com/Booble/archive/2011/04/03/2004865.html 这里面介绍的非常详细
var q,x,y:array[..] of longint;
p,ans,i,j,h,r,k,t,n:longint; function cross(i,j,k,r:longint):longint;
begin
exit((x[i]-x[j])*(y[k]-y[r])-(x[k]-x[r])*(y[i]-y[j]));
end; function dis(i,j:longint):longint;
begin
exit(sqr(x[i]-x[j])+sqr(y[i]-y[j]));
end; function max(a,b:longint):longint;
begin
if a>b then exit(a) else exit(b);
end; procedure swap(var a,b:longint);
var c:longint;
begin
c:=a;
a:=b;
b:=c;
end; procedure sort(l,r: longint);
var i,j,p,q: longint;
begin
i:=l;
j:=r;
p:=x[(l+r) shr ];
q:=y[(l+r) shr ];
repeat
while (x[i]<p) or (x[i]=p) and (y[i]<q) do inc(i);
while (p<x[j]) or (p=x[j]) and (q<y[j]) do dec(j);
if not(i>j) then
begin
swap(x[i],x[j]);
swap(y[i],y[j]);
inc(i);
j:=j-;
end;
until i>j;
if l<j then sort(l,j);
if i<r then sort(i,r);
end;
begin
readln(n);
for i:= to n do
readln(x[i],y[i]);
sort(,n);
q[]:=;
t:=;
for i:= to n do
begin
while (t>) and (cross(q[t],q[t-],i,q[t-])>=) do dec(t);
inc(t);
q[t]:=i;
end;
k:=t;
for i:=n- downto do
begin
while (t>k) and (cross(q[t],q[t-],i,q[t-])>=) do dec(t);
inc(t);
q[t]:=i;
end;
h:=;
r:=k;
ans:=dis(h,r);
while (h<=k) and (r<=t) do
begin
p:=cross(q[h],q[h+],q[r+],q[r]); //两条直线谁先贴住凸包,这个可以用叉积解决,具体画个图就明白了
if p<= then inc(h) else inc(r);
ans:=max(ans,dis(q[h],q[r]));
end;
writeln(ans);
end.
poj2187的更多相关文章
- 【poj2187】 Beauty Contest
http://poj.org/problem?id=2187 (题目链接) 题意 求点集上两点间最长距离 Solution 凸包+旋转卡壳. 旋转卡壳是看起来很难,但是很好意会也很好实现的算法,但是要 ...
- poj2187 Beauty Contest(旋转卡壳)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Beauty Contest Time Limit: 3000MS Memor ...
- [POJ2187][BZOJ1069]旋转卡壳
旋转卡壳 到现在依然不确定要怎么读... 以最远点对问题为例,枚举凸包上的两个点是最简单的想法,时间复杂度O(n2) 我们想象用两条平行线卡着这个凸包,当其中一个向某个方向旋转的时候另一个显然也是朝同 ...
- POJ2187 Beauty Contest (旋转卡壳算法 求直径)
POJ2187 旋转卡壳算法如图 证明:对于直径AB 必然有某一时刻 A和B同时被卡住 所以旋转卡壳卡住的点集中必然存在直径 而卡壳过程显然是O(n)的 故可在O(n)时间内求出直径 凸包具有良好的性 ...
- poj2187 旋转卡(qia)壳(ke)
题意:求凸包的直径 关于对踵点对.旋转卡壳算法的介绍可以参考这里: http://www.cnblogs.com/Booble/archive/2011/04/03/2004865.html http ...
- [USACO2003][poj2187]Beauty Contest(凸包+旋转卡壳)
http://poj.org/problem?id=2187 题意:老题了,求平面内最远点对(让本渣默默想到了悲剧的AHOI2012……) 分析: nlogn的凸包+旋转卡壳 附:http://www ...
- POJ2187 Beauty Contest
Description Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, ea ...
- poj2187(未完、有错)
凸包求直径(socalled..) 采用Graham+Rotating_Calipers,Graham复杂度nlogn,RC算法复杂度n,所以时间复杂度不会很高. 学习RC算法,可到http://cg ...
- POJ2187(旋转卡壳)
Beauty Contest Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 35459 Accepted: 10978 ...
随机推荐
- 粵語/廣東話/Cantonese 資料/Material
一.粵語歌詞網 1.海闊天空(粵語) 歌詞 今天我 寒夜裡看雪飄過 gam1 tin1 ngo5 hon4 je6 leoi5 hon3 syut3 piu1 gwo3 懷著冷卻了的心窩漂遠方 waa ...
- [leetcode] 405. Convert a Number to Hexadecimal
https://leetcode.com/contest/6/problems/convert-a-number-to-hexadecimal/ 分析:10进制转换成16进制,不能用库函数,刚开始,我 ...
- [翻译][MVC 5 + EF 6] 5:Code First数据库迁移与程序部署
原文:Code First Migrations and Deployment with the Entity Framework in an ASP.NET MVC Application 1.启用 ...
- osi七层模型和两主机传输过程:
osi七层模型和两主机传输过程: http://www.zhihu.com/question/24002080/answer/31817536 注:笔记部分可能参考其他作者内容的一个记录,仅为加深自 ...
- Sass:一种CSS预处理器语言
http://sass-lang.com/ Sass是一种CSS预处理器语言,通过编程方式生成CSS代码.因为可编程,所以操控灵活性自由度高,方便实现一些直接编写CSS代码较困难的代码. 同时,因为S ...
- 遍历 TextBox控件
foreach (System.Windows.Forms.Control control in this.Controls) { if (control is System.Windows.Form ...
- jquery.animate用法
<!DOCTYPE html><html><head><script src="http://libs.baidu.com/jquery/1.10. ...
- RasAPI函数实现PPPOE拨号
unit uDial; interface uses Windows,Messages, SysUtils, Ras;// Classes; var //EntryName,UserName,Pass ...
- 不相交集合ADT
不相交集合数据结构保持一组不相交的动态集合S={S1,S2,...,SK},每个集合通过一个代表来识别,代表即集合中的某个成员. 如果x表示一个对象,不相交集合支持以下操作: MAKE-SET(x): ...
- Substring的简单使用
string myString = "测试一下函数Substring()是怎么用的"; //Substring()在C#中有两个重载函数 //分别如下示例 //如果参数为一个长整数 ...