POJ 2187 Beauty Contest(凸包暴力法)

Beauty Contest
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 24124 Accepted: 7364

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



求出凸包上的点,然后暴力枚举

开始过不了,后来把如果点的个数在100以内,就直接暴力A了!

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <cmath>
using namespace std;
#define eps 1e-8
#define PI 3.14159265
struct point
{
int x;
int y;
}po[55500],temp;
int n,pos;
bool zero(double a)
{
return fabs(a) < eps;
}
int dis(point &a,point &b)//返回两点之间距离的平方
{
return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y);
}
int across(point &a,point &b,point &c)//求a b and a c 的X积
{
return (b.x-a.x)*(c.y-a.y) - (b.y-a.y)*(c.x-a.x);
}
int cmp(const void *a,const void *b)
{
return across(po[0],*(point*)a,*(point*)b) > 0 ? -1 : 1;
}
int select()
{
int i,j,k=1;
for(i=2;i<n;i++)
{
if(across(po[0],po[k],po[i])==0)
{
if(dis(po[0],po[k]) < dis(po[0],po[i]))
po[k]=po[i];
}
else
po[++k]=po[i];
}
return k+1;
}
int graham(int num)
{
int i,j,k=2;
//
po[num]=po[0];//fangbian 
num++;
int ans=0;
int t;
if(num<20)
{
for(i=0;i<num;i++)
for(j=0;j<num;j++)
{
t=dis(po[i],po[j]);
if(t > ans)
ans=t;
}
printf("%d\n",ans);
return 0;
}
for(i=3;i<num;i++)
{
while(across(po[k-1],po[k],po[i]) < -eps)
{k--;}
po[++k]=po[i];//就这个循环结束,不需要了!
}
/*
for(i=0;i<k;i++)
printf("%lf %lf\n",po[i].x,po[i].y);
*/
for(i=0;i<k;i++)
for(j=0;j<k;j++)
{
t=dis(po[i],po[j]);
if(t > ans)
ans=t;
}
printf("%d\n",ans);
return 0;
}
int main()
{
int i,j,k;
point my_temp;
int ans;
int t;
while(scanf("%d",&n)!=EOF)
{
ans=0;
scanf("%d%d",&po[0].x,&po[0].y);
temp=po[0];
pos=0;
for(i=1;i<n;i++)
{
scanf("%d%d",&po[i].x,&po[i].y);
if(po[i].y < temp.y)
temp=po[i],pos=i;
}
if(n<100)
{
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
t=dis(po[i],po[j]);
if(t > ans)
ans=t;
}
printf("%d\n",ans);
continue;
}

my_temp=po[0];
po[0]=po[pos];
po[pos]=my_temp;
qsort(po+1,n-1,sizeof(po[0]),cmp);
graham(select());
}
return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值