POJ 2187 Beauty Contest
来源:互联网 发布:cf出现网络出现异常 编辑:程序博客网 时间:2024/05/22 00:25
Beauty Contest
Time Limit: 3000MS Memory Limit: 65536KTotal 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.
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
* 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
40 00 11 11 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
简单模版的运用
#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; if((x1*x1+y1*y1)>dis) { dis=x1*x1+y1*y1; } } } printf("%d\n",dis); } 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
- 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
- poj 2187 Beauty Contest
- poj 2187 Beauty Contest
- Android环境搭建(XP/Win7)
- 算法(数学)与外语是程序员未来发展的基石
- 关于usb inrush current test 日记
- #R#R的一些注意事项
- 在win7下找回丢失的桌面IE图标
- POJ 2187 Beauty Contest
- jstat用法详解
- 苹果开发帐户续费
- 展讯平台Linux下升级patch方法
- 《项目经验》--简单三层使用DataTable向数据库表批量导入数据---向SqlServer一张表中导入数据
- ubuntu12.04英文版输入中文问题
- CLOB、BLOB , CLOB与BLOB的区别
- 织梦(dedecms)仿站教程第二十二讲——列表页制作(一)
- 2013工资新规定,未来的八种人将会被淘汰!