POJ 2187 Beauty Contest
Beauty ContestTime Limit: 3000MS Memory Limit: 65536K
Total Submissions: 22893 Accepted: 6994
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
USACO 2003 Fall
简单模版的运用
[cpp]
#include <stdio.h>
#include <string.h>
#include <math.h>
struct map
{
int x,y;
}a[50010];
struct num
{
int x,y;
}statck[50010];
int chacheng(int x1,int y1,int x2, int y2)
{
int t=x1*y2-y1*x2;
if(t>0)
{
return 1;
}else if(t<0)
{
return -1;
}else
{
return 0;
}
}
int cmp(const void *e,const void *f)
{
struct map *p1=(struct map *)e;
struct map* p2=(struct map *)f;
int x1,y1,x2,y2,t;
x1=p1->x-a[0].x; y1=p1->y-a[0].y;
x2=p2->x-a[0].x; y2=p2->y-a[0].y;
t=chacheng(x1,y1,x2,y2);
if(t==1)
{
return -1;
}else if(t==-1)
{
return 1;
}else
{
return (x1*x1+y1*y1-(x2*x2+y2*y2));
}
}
int main()
{
int i,j,n,m,s,t,pos1,top,dis;
int x1,y1,x2,y2;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<=n-1;i++)
{
scanf("%d %d",&a[i].x,&a[i].y);
if(i==0)
{
pos1=i;
}else if((a[i].y<a[pos1].y)||(a[i].y==a[pos1].y&&a[i].x<a[pos1].x))
{
pos1=i;
}
}
t=a[0].x; a[0].x=a[pos1].x ;a[pos1].x=t;
t=a[0].y; a[0].y=a[pos1].y; a[pos1].y=t;
qsort(a+1,n-1,sizeof(a[0]),cmp);
top=0;
statck[top].x=a[0].x; statck[top++].y=a[0].y;
statck[top].x=a[1].x; statck[top++].y=a[1].y;
for(i=2;i<=n-1;)
{
if(top>=2)
{
x1=statck[top-1].x-statck[top-2].x;
y1=statck[top-1].y-statck[top-2].y;
x2=a[i].x-statck[top-1].x;
y2=a[i].y-statck[top-1].y;
t=chacheng(x1,y1,x2,y2);
if(t==1||t==0)
{
statck[top].x=a[i].x;
statck[top++].y=a[i].y;
i++;
}else
{
top--;
}
}else
{
statck[top].x=a[i].x;
statck[top++].y=a[i].y;
i++;
}
}
for(i=0,dis=0;i<=top-1;i++)
{
for(j=i+1;j<=top-1;j++)
{
x1=statck[j].x-statck[i].x;
y1=statck[j].y-statck[i].y;
 
补充:软件开发 , C++ ,
- 更多C/C++疑问解答:
- 关于c++的cout输出的问题。
- 在学校里学过C和C++,不过学的很一般,现在自学C#,会不会很难?
- 全国计算机二级C语言笔试题
- 已知某树有2个2度结点,3个3度结点,4个4度结点,问有几个叶子结点?
- c++数据结构内部排序问题,整数排序
- 2012九月计算机二级C语言全国题库,,急求急求
- 如果assert只有一个字符串作为参数,是什么意思呢?
- C语言中,哪些运算符具有左结合性,哪些具有右结合性,帮忙总结下,谢谢了!
- 为什么用结构体编写的程序输入是,0输不出来啊~~~
- 将IEEE—754的十六进制转化为十进制浮点类型,用C或C++都行,多谢各位大侠啊,非常感谢!
- 为什么这个程序求不出公式?
- 这个链表倒置的算法请大家分析下
- c语言函数库调用
- C语言unsigned int纠错
- C语言快排求解啊