Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 24305 | Accepted: 7428 |
Description
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
* Lines 2..N+1: Two space-separated integers x and y specifying coordinate of each farm
Output
Sample Input
4 0 0 0 1 1 1 1 0
Sample Output
2
Hint
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;
}
思路: