POJ 2187-Beauty Contest(凸包-Graham扫描法/旋转卡壳法)
2017-01-17 22:16
344 查看
Beauty Contest
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
Sample Output
Hint
Farm 1 (0, 0) and farm 3 (1, 1) have the longest distance (square root of 2)
Source
USACO 2003 Fall
求凸包的时候,可以用Graham扫描法或者旋转卡壳法。
代码一:Graham扫描法
代码二:旋转卡壳法
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 36053 | Accepted: 11183 |
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
题目意思:
平面上有N个牧场,i号牧场的位置在格点(xi,yi),所有的牧场位置互不相同。请计算最短的两个牧场间的距离,输出最远的距离的平方。解题思路:
先求凸包,然后枚举凸包上的所有点对并计算距离。求凸包的时候,可以用Graham扫描法或者旋转卡壳法。
代码一:Graham扫描法
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
const int maxn = 60050;
const int INF = 0x3f3f3f3f;
int EPS=1e-10;
int add(int a,int b)//int加法运算考虑误差
{
if(abs(a+b)<EPS*(abs(a)+abs(b))) return 0;
return a+b;
}
struct P//结构体定义二维向量
{
int x,y;
P() {}
P(int x,int y):x(x),y(y) {}
P operator +(P p)
{
return P(add(x,p.x),add(y,p.y));
}
P operator -(P p)
{
return P(add(x,-p.x),add(y,-p.y));
}
P operator *(int d)
{
return P(x*d,y*d);
}
int dot(P p)//内积
{
return add(x*p.x,y*p.y);
}
int det(P p)//外积
{
return add(x*p.y,-y*p.x);
}
};
bool cmp(const P& p,const P& q)//字典序比较
{
if(p.x!=q.x) return p.x<q.x;
else return p.y<q.y;
}
vector<P> convex_hull(P* ps,int n)//求凸包
{
sort(ps,ps+n,cmp);
int k=0;//凸包的顶点数
vector<P> qs(n*2);//构造中的凸包
for(int i=0; i<n; ++i)//构造凸包上侧
{
while(k>1&&(qs[k-1]-qs[k-2]).det(ps[i]-qs[k-1])<=0) --k;
qs[k++]=ps[i];
}
for(int i=n-2,t=k; i>=0; --i)//构造凸包下侧
{
while(k>t&&(qs[k-1]-qs[k-2]).det(ps[i]-qs[k-1])<=0) --k;
qs[k++]=ps[i];
}
qs.resize(k-1);
return qs;
}
double dist(P p,P q)//距离的平方
{
return (p-q).dot(p-q);
}
int N;
P ps[maxn];
void solve()
{
vector<P> qs=convex_hull(ps,N);
double res=0;
for(int i=0; i<qs.size(); ++i)
for(int j=0; j<i; ++j)
res=max(res,dist(qs[i],qs[j]));
printf("%.0f\n",res);
}
int main()
{
while (scanf("%d", &N) != EOF)
{
for(int i=0; i<N; ++i)
scanf("%d%d",&ps[i].x,&ps[i].y);
solve();
}
return 0;
}
/**
4 0 0 0 1 1 1 1 0**/
代码二:旋转卡壳法
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
const int maxn = 60050;
const int INF = 0x3f3f3f3f;
int EPS=1e-10;
int add(int a,int b)//int加法运算考虑误差
{
if(abs(a+b)<EPS*(abs(a)+abs(b))) return 0;
return a+b;
}
struct P//结构体定义二维向量
{
int x,y;
P() {}
P(int x,int y):x(x),y(y) {}
P operator +(P p)
{
return P(add(x,p.x),add(y,p.y));
}
P operator -(P p)
{
return P(add(x,-p.x),add(y,-p.y));
}
P operator *(int d)
{
return P(x*d,y*d);
}
int dot(P p)//内积
{
return add(x*p.x,y*p.y);
}
int det(P p)//外积
{
return add(x*p.y,-y*p.x);
}
};
bool cmp(const P& p,const P& q)//字典序比较
{
if(p.x!=q.x) return p.x<q.x;
else return p.y<q.y;
}
vector<P> convex_hull(P* ps,int n)//求凸包
{
sort(ps,ps+n,cmp);
int k=0;//凸包的顶点数
vector<P> qs(n*2);//构造中的凸包
for(int i=0; i<n; ++i)//构造凸包上侧
{
while(k>1&&(qs[k-1]-qs[k-2]).det(ps[i]-qs[k-1])<=0) --k;
qs[k++]=ps[i];
}
for(int i=n-2,t=k; i>=0; --i)//构造凸包下侧
{
while(k>t&&(qs[k-1]-qs[k-2]).det(ps[i]-qs[k-1])<=0) --k;
qs[k++]=ps[i];
}
qs.resize(k-1);
return qs;
}
double dist(P p,P q)//距离的平方
{
return (p-q).dot(p-q);
}
int N;
P ps[maxn];
void solve()
{
vector<P> qs = convex_hull(ps, N);
int n=qs.size();
if (n == 2) //特别处理凸包退化的情况
{
printf("%.0f\n", dist(qs[0],qs[1]));
return;
}
int i=0, j=0; //某个方向上的对踵点对
//求出x轴方向上的对踵点对
for (int k=0; k<n; k++)
{
if (!cmp(qs[i],qs[k]))
i=k;
if (cmp(qs[j],qs[k]))
j=k;
}
double res=0;
int si=i, sj=j;
while (i!=sj||j!=si) //将方向逐步旋转180度
{
res=max(res, dist(qs[i], qs[j]));
//判断先转到边i-(i+1)的法线方向还是边j-(j+1)的法线方向
if ((qs[(i+1)%n]-qs[i]).det(qs[(j+1)%n]-qs[j])<0)
i=(i+1)%n; //先转到边i-(i+1)的法线方向
else
j=(j+1)%n; //先转到边j-(j+1)的法线方向
}
printf("%.0f\n", res);
}
int main()
{
while (scanf("%d", &N) != EOF)
{
for(int i=0; i<N; ++i)
scanf("%d%d",&ps[i].x,&ps[i].y);
solve();
}
return 0;
}
/**
4 0 0 0 1 1 1 1 0**/
相关文章推荐
- 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 (凸包+旋转卡壳)
- POJ2187 Beauty Contest(凸包+对踵点对求解)
- 【poj 2187】Beauty Contest(凸包,graham扫描法,旋转卡壳法)
- POJ 2187 Beauty Contest【旋转卡壳求凸包直径】
- poj 2187 Beauty Contest , 旋转卡壳求凸包的直径的平方
- 【计算几何】 Andrew凸包算法 + 旋转卡壳(以求点对最长距离为例) -- 以 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(凸包暴力法)