POJ 2187 Beauty Contest

Beauty Contest
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 24305   Accepted: 7428

Description

Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, earning the title 'Miss Cow World'. As a result, Bessie will make a tour of N (2 <= N <= 50,000) farms around the world in order to spread goodwill between farmers and their cows. For simplicity, the world will be represented as a two-dimensional plane, where each farm is located at a pair of integer coordinates (x,y), each having a value in the range -10,000 ... 10,000. No two farms share the same pair of coordinates.

Even though Bessie travels directly in a straight line between pairs of farms, the distance between some farms can be quite large, so she wants to bring a suitcase full of hay with her so she has enough food to eat on each leg of her journey. Since Bessie refills her suitcase at every farm she visits, she wants to determine the maximum possible distance she might need to travel so she knows the size of suitcase she must bring.Help Bessie by computing the maximum distance among all pairs of farms.

Input

* Line 1: A single integer, N

* Lines 2..N+1: Two space-separated integers x and y specifying coordinate of each farm

Output

* Line 1: A single integer that is the squared distance between the pair of farms that are farthest apart from each other.

Sample Input

4
0 0
0 1
1 1
1 0

Sample Output

2

Hint

Farm 1 (0, 0) and farm 3 (1, 1) have the longest distance (square root of 2)

Source

 
題意:
求出所給散點集相距最遠的兩個點
 
代碼:
#include<iostream>
#include<string>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#define INF 0x3f3f3f3f
using namespace std;

typedef struct
{
	int x,y;
}node;
int N;
node *s;

int distsquare(node a,node b)
{
	return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}

double dist(node a,node b)
{
	return sqrt((double)(distsquare(a,b)));
}

int make(int a,int b,int c,int d)
{
	return a*d-b*c;
}

int cross(node a,node b,node c,node d)
{
	return make(b.x-a.x,b.y-a.y,d.x-c.x,d.y-c.y);
}

int cmp(const void *a,const void *b)
{
	node *c=(node *)a,*d=(node *)b;

	int tmp=cross(*s,*c,*s,*d);
	if(tmp>0) return -1;
	else if(tmp==0) return dist(*s,*d)-dist(*s,*c);
	else return 1;
}

int main()
{
	while(scanf("%d",&N)!=EOF)
	{
		node *point=new node[N+1];

		int min_point=0;
		int min_x=INF;
		for(int i=1;i<=N;i++)
		{
			scanf("%d%d",&point[i].x,&point[i].y);

			if(min_x>point[i].x)
			{
				min_x=point[i].x;
				min_point=i;
			}
			else if(min_x==point[i].x)
			{
				if(point[min_point].y>point[i].y)
					min_point=i;
			}
		}

		point[0]=point[N];
		point[N]=point[min_point];
		point[min_point]=point[0];

		s=&point[N];
		qsort(point+1,N,sizeof(node),cmp);

		int *bag=new int [N+2];
		bag[1]=N;
		bag[2]=1;
		int cnt=2;
		for(int i=2;i<=N;)
			if(cross(point[bag[cnt-1]],point[bag[cnt]],point[bag[cnt]],point[i])>=0)
				bag[++cnt]=i++;
			else cnt--;

		int max_len=0;
		for(int i=1;i<=cnt-2;i++)
			for(int j=i+1;j<=cnt-1;j++)
			{
				int tmp=distsquare(point[bag[i]],point[bag[j]]);
				if(tmp>max_len)
					max_len=tmp;
			}

		printf("%d\n",max_len);
	}
	return 0;
}

思路:
若兩兩暴力計算,定會超時
距離最遠的兩點定在散點集的凸包上
故先求出凸包,後再兩兩計算凸包上的點即可
發佈了2 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章