POJ 2187 Beauty Contest(凸包暴力法)
2013-07-18 21:57
411 查看
Beauty Contest
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 USACO 2003 Fall |
开始过不了,后来把如果点的个数在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;
}
相关文章推荐
- POJ_2187_Beauty Contest(凸包+暴力)
- poj 2187 Beauty Contest (凸包暴力求最远点对+旋转卡壳)
- poj 2187 Beauty Contest(凸包)
- POJ 2187 Beauty Contest [凸包,旋转卡壳]
- POJ 2187 Beauty Contest(凸包,旋转卡壳)
- POJ 2187 Beauty Contest(凸包求最远点距离)
- POJ 2187 Beauty Contest(凸包 + 旋转卡壳)
- poj 2187 Beauty Contest (凸包 Graham)
- poj 2187 Beauty Contest(凸包求解多节点的之间的最大距离)
- 【POJ 2187】Beauty Contest(凸包直径、旋转卡壳)
- POJ 2187 Beauty Contest 凸包的应用
- poj 2187 Beauty Contest , 旋转卡壳求凸包的直径的平方
- POJ 2187 Beauty Contest (凸包+旋转卡壳)
- poj 2187 Beauty Contest(凸包+旋转卡壳)
- 【poj 2187】Beauty Contest(凸包,graham扫描法,旋转卡壳法)
- 【计算几何】 Andrew凸包算法 + 旋转卡壳(以求点对最长距离为例) -- 以 POJ 2187 Beauty Contest 为例
- Poj 2187 Beauty Contest_旋转凸包卡壳
- poj 2187 Beauty Contest , 旋转卡壳求凸包的直径的平方
- poj 2187 Beauty Contest(二维凸包旋转卡壳)
- Poj 2187 Beauty Contest_旋转凸包卡壳