题目:求平面最远点对。
分析:计算几何、旋转卡壳。求出凸包、利用旋转卡壳枚举即可。
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
typedef struct pnode
{
int x,y,d;
}point;
point P[ 50005 ];
//两点间距离
int dist( point a, point b )
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
//叉乘ab*ac
int crossproduct( point a, point b, point c )
{
return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
//坐标排序
bool cmp1( point a, point b )
{
return (a.x == b.x)?(a.y < b.y):(a.x < b.x);
}
//级角排序
bool cmp2( point a, point b )
{
int cp = crossproduct( P[0], a, b );
if ( !cp ) return a.d < b.d;
else return cp > 0;
}
int Graham( int n )
{
//点集排序
sort( P+0, P+n, cmp1 );
for ( int i = 1 ; i < n ; ++ i )
P[i].d = dist( P[0], P[i] );
sort( P+1, P+n, cmp2 );
//计算凸包
int top = 1;
for ( int i = 2 ; i < n ; ++ i ) {
while ( top > 0 && crossproduct( P[i], P[top-1], P[top] ) <= 0 ) -- top;
P[++ top] = P[i];
}
P[++ top] = P[0];
//旋转卡壳
int R = 1,D = 0;
for ( int L = 0 ; L < top ; ++ L ) {
while ( crossproduct( P[L], P[L+1], P[R] ) < crossproduct( P[L], P[L+1], P[R+1] ) )
R = (R+1)%top;
D = max( D, max( dist( P[L], P[R] ), dist( P[L+1], P[R+1] ) ) );
}
return D;
}
int main()
{
int n;
while ( scanf("%d",&n) != EOF ) {
for ( int i = 0 ; i < n ; ++ i )
scanf("%d%d",&P[i].x,&P[i].y);
printf("%d\n",Graham( n ));
}
return 0;
}
分享到:
相关推荐
北大POJ2187-Beauty Contest 解题报告+AC代码
O(nlogn)凸包问题 poj2187
NULL 博文链接:https://128kj.iteye.com/blog/1749213
POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类
poj上的测室数据,需要的下载........
poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题...
POJ第1861题源码 POJ第1861题源码 POJ第1861题源码
poj分类poj分类poj分类poj分类
北大POJ1159-Palindrome 解题报告+AC代码
poj 3414解题报告poj 3414解题报告poj 3414解题报告poj 3414解题报告
C语言 poj npu 西工大 C语言Poj答案全完整打包,给有需要的朋友
poj 1012解题报告poj 1012解题报告poj 1012解题报告poj 1012解题报告
poj 2329解题报告poj 2329解题报告poj 2329解题报告poj 2329解题报告
poj 1659解题报告poj 1659解题报告poj 1659解题报告poj 1659解题报告
POJ1503解答 POJ1503解答,正确答案(已通过POJ)
北大POJ2002-Squares 解题报告+AC代码
POJ1048,加强版的约瑟夫问题 难度中等
POJ1083的代码,POJ1083的代码,POJ1083的代码
poj 百练 题目分类 poj 百练 题目分类
poj 1001答案