POJ2187 Beauty Contest 水平序GrahamScan法
来源:互联网 发布:linux安装oracle实例 编辑:程序博客网 时间:2024/04/29 18:08
Beauty Contest
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 35550 Accepted: 11012
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
利用水平序的GrahamScan算法能够迅速建立凸包,然后进行暴力枚举,找到凸包中距离最远的两个点。
比较好的方法其实是旋转卡壳,由于数据不强,暴力的方法才能过
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>using namespace std;const int maxn=50000+10;int n,top;struct point{int x,y;point() {}point(int _x,int _y){x=_x;y=_y;}};point operator - (point a,point b){return point(a.x-b.x,a.y-b.y);}int operator * (point a,point b){return a.x*b.x+a.y*b.y;}int operator ^ (point a,point b){return a.x*b.y-a.y*b.x;}bool cmp(point a,point b){return a.y<b.y || (a.y==b.y && a.x<b.x);}int dis(point a,point b){return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);}point p[maxn],stack[maxn];void graham(){if(n==1){top=1;stack[0]=p[0];return ;}else if(n==2){top=2;stack[0]=p[0];stack[1]=p[1];return ;}top=2;stack[0]=p[0];stack[1]=p[1];for(int i=2;i<n;i++){ //构造左链while(top>0 &&((stack[top-1]-stack[top-2])^(p[i]-stack[top-1]))<0)top--;stack[top++]=p[i];}int temp=top;for(int i=n-2;i>=0;i--){ //构造右链while(top>temp &&((stack[top-1]-stack[top-2])^(p[i]-stack[top-1]))<0)top--;stack[top++]=p[i];}top--; //注意,最开始的点会重复}int solve(){int res=0;sort(p,p+n,cmp);graham(); //水平序graham//for(int i=0;i<top;i++){ //加上注释可知凸包构建是否正确//printf("(%d %d)\n",stack[i].x,stack[i].y);//}for(int i=0;i<top; ++i) for(int j=i+1; j < top; ++j) res=max(res,dis(stack[i],stack[j]));return res;}int main(){//freopen("input.txt","r",stdin);while(~scanf("%d",&n)){memset(stack,0,sizeof(stack));int ans;for(int i=0;i<n;i++)scanf("%d%d",&p[i].x,&p[i].y);ans=solve();printf("%d\n",ans);}}
0 0
- POJ2187 Beauty Contest 水平序GrahamScan法
- POJ2187-Beauty Contest
- poj2187--Beauty Contest(凸包首杀)
- POJ2187---Beauty Contest
- POJ2187:Beauty Contest
- 【poj2187】 Beauty Contest
- poj2187 Beauty Contest
- poj2187 Beauty Contest
- poj2187 Beauty Contest
- POJ2187--凸包--Beauty Contest
- 【旋转卡壳】Beauty Contest POJ2187
- POJ2187——Beauty Contest
- poj2187 Beauty Contest 凸包
- 【POJ2187】Beauty Contest【旋转卡壳】
- POJ2187 Beauty Contest 凸包+旋转卡壳
- POJ2187 Beauty Contest(旋转卡壳)
- poj2187 Beauty Contest 凸包+旋转卡壳
- poj2187 Beauty Contest(旋转卡壳)
- javascript 5种方式实现继承
- X265-Android
- R aggregate函数
- LeetCode240. Search a 2D Matrix II二分查找+分治法
- 戏说-activity四种启动模式
- POJ2187 Beauty Contest 水平序GrahamScan法
- JavaScript 创建对象 构造函数
- 安卓内存泄漏(三) 泄漏场景
- 【剑指offer】疯狂跳台阶
- Leetcode 204.Count Primes 素性检测
- noip2011 观光公交
- MySQL 主键与索引的联系与区别分析
- vs2015 key
- 正则表达式详解详例