您的位置:首页 > 其它

poj 2187 Beauty Contest(凸包)

2015-08-18 19:50 363 查看
不知道这道题在比较的时候为什么用qsort()才能过,不过找到了一组数据

10
0 0
10000 0
1 100
2 199
9999 100
9998 199
100 -900
200 -1799
9800 -1799
9900 -900

答案应该是100000000


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

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define LL long long
#define INF 10001
#define MAX 500100
using namespace std;

struct node
{
int x,y;
} ls[MAX];
int n;
int Point[MAX];
int pos;
node *p;

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

int dit(int x1,int y1,int x2,int y2)
{
return x1 * y2 - x2 * y1;
}
int Cross(node a,node b,node c,node d)
{
return dit(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 *s = (node*)a;
node *t = (node*)b;

int temp = Cross(*p,*s,*p,*t);
if(temp>0)
return -1;
else if(temp==0)
return Dis(*p,*t)-Dis(*p,*s);
return 1;
}
int main()
{
//    freopen("in.txt","r",stdin);
std::ios::sync_with_stdio(false);
while(cin>>n)
{
pos = 0;
int MX = INF;
for(int i=1;i<=n;i++)
{
cin>>ls[i].x>>ls[i].y;
if(MX > ls[i].x)
{
pos = i;
MX = ls[i].x;
}
else if(MX == ls[i].x)
{
if(ls[i].y < ls[pos].y)
{
pos = i;
}
}
}
swap(ls[pos],ls
);
p=&ls
;
qsort(ls+1,n,sizeof(node),cmp);

int cnt = 2;
int p = 2;
Point[1] = n;
Point[2] = 1;

for(int i=2;i<=n;)
{
if(Cross(ls[Point[p-1]],ls[Point[p]],ls[Point[p]],ls[i]) >= 0)
{
Point[++p] = i++;
cnt++;
}
else
{
p--;
cnt--;
}
}
int MAX_len = 0;
for(int i=1;i<cnt-1;i++)
{
for(int j=i+1;j<cnt;j++)
{
int d = Dis(ls[Point[i]],ls[Point[j]]);
if(d > MAX_len)
MAX_len = d;
}
}
cout<<MAX_len<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: