poj 2187 Beauty Contest

题目:求平面最远点对。

分析:计算几何、旋转卡壳。求出凸包、利用旋转卡壳枚举即可。

#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;
}


你可能感兴趣的:(poj 2187 Beauty Contest)