poj 2187 Beauty Contest(凸包)
2016-05-31 17:33
323 查看
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
题意:有N个牧场,求最远两个牧场的距离的平方。
如果一个一个搜索肯定会超时,因此只需枚举凸包上的点就可以了。
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
int n,top;
struct point
{
int x,y;
} p[50005],s[50005],t;
int cross(point t1,point t2,point t3,point t4) //求向量t1t2和向量t3t4的叉积
{
return (t2.x-t1.x)*(t4.y-t3.y)-(t2.y-t1.y)*(t4.x-t3.x);
}
int dis1(point t1,point t2) //求距离平方
{
int z=(t2.x-t1.x)*(t2.x-t1.x)+(t2.y-t1.y)*(t2.y-t1.y);
return z;
}
double dis(point t1,point t2) //求距离
{
double z=(t2.x-t1.x)*(t2.x-t1.x)+(t2.y-t1.y)*(t2.y-t1.y);
return sqrt(z);
}
bool cmp(point t1,point t2)
{
double z=cross(p[0],t1,p[0],t2);
return z?z>0:dis(p[0],t1)>dis(p[0],t2);
}
void findpoint() //找基点,按y从小到大,如果y相同,按x从左到右
{
int i,j=0;
t=p[0];
for(i=1; i<n; i++)
{
if(t.y>p[i].y||(t.y==p[i].y&&t.x>p[i].x))
{
j=i;
t=p[i];
}
}
t=p[0];
p[0]=p[j];
p[j]=t;
}
void scanner()
{
int i;
findpoint();
sort(p+1,p+n,cmp);
s[0]=p[0];
s[1]=p[1];
top=1;
for(i=2; i<n; i++)
{
while(cross(s[top-1],s[top],s[top],p[i])<0)
top--;
top++;
s[top]=p[i];
}
}
int main()
{
int i,j;
while(~scanf("%d",&n))
{ int ans=-1;
for(i=0; i<n; i++)
{
scanf("%d%d",&p[i].x,&p[i].y);
}
scanner();
for(i=0; i<=top; i++)
for(j=0;j<=top;j++)
{
ans=max(ans,dis1(s[i],s[j]));
}
printf("%d\n",ans);
}
return 0;
}
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
题意:有N个牧场,求最远两个牧场的距离的平方。
如果一个一个搜索肯定会超时,因此只需枚举凸包上的点就可以了。
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
int n,top;
struct point
{
int x,y;
} p[50005],s[50005],t;
int cross(point t1,point t2,point t3,point t4) //求向量t1t2和向量t3t4的叉积
{
return (t2.x-t1.x)*(t4.y-t3.y)-(t2.y-t1.y)*(t4.x-t3.x);
}
int dis1(point t1,point t2) //求距离平方
{
int z=(t2.x-t1.x)*(t2.x-t1.x)+(t2.y-t1.y)*(t2.y-t1.y);
return z;
}
double dis(point t1,point t2) //求距离
{
double z=(t2.x-t1.x)*(t2.x-t1.x)+(t2.y-t1.y)*(t2.y-t1.y);
return sqrt(z);
}
bool cmp(point t1,point t2)
{
double z=cross(p[0],t1,p[0],t2);
return z?z>0:dis(p[0],t1)>dis(p[0],t2);
}
void findpoint() //找基点,按y从小到大,如果y相同,按x从左到右
{
int i,j=0;
t=p[0];
for(i=1; i<n; i++)
{
if(t.y>p[i].y||(t.y==p[i].y&&t.x>p[i].x))
{
j=i;
t=p[i];
}
}
t=p[0];
p[0]=p[j];
p[j]=t;
}
void scanner()
{
int i;
findpoint();
sort(p+1,p+n,cmp);
s[0]=p[0];
s[1]=p[1];
top=1;
for(i=2; i<n; i++)
{
while(cross(s[top-1],s[top],s[top],p[i])<0)
top--;
top++;
s[top]=p[i];
}
}
int main()
{
int i,j;
while(~scanf("%d",&n))
{ int ans=-1;
for(i=0; i<n; i++)
{
scanf("%d%d",&p[i].x,&p[i].y);
}
scanner();
for(i=0; i<=top; i++)
for(j=0;j<=top;j++)
{
ans=max(ans,dis1(s[i],s[j]));
}
printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- 简单的四则运算
- 数的奇偶性
- ACMer博客瀑布流分析
- ACM程序设计大赛题目分类
- 计算字符串最后一个单词长度
- ACM网址
- 1272 小希的迷宫
- 1272 小希的迷宫
- hdu 1250 大数相加并用数组储存
- 矩阵的乘法操作
- 蚂蚁爬行问题
- 蚂蚁爬行问题
- 求两个数的最大公约数【ACM基础题】
- 打印出二进制中所有1的位置
- 杭电题目---一只小蜜蜂
- HDOJ 1002 A + B Problem II (Big Numbers Addition)
- 初学ACM - 半数集(Half Set)问题 NOJ 1010 / FOJ 1207
- 初学算法 - 求凸包的Garham's Scan算法的C++实现
- 初学ACM - 组合数学基础题目PKU 1833
- POJ ACM 1002