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.
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
* 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;
}
思路:
若两两暴力计算,定会超时
距离最远的两点定在散点集的凸包上
故先求出凸包,后再两两计算凸包上的点即可