POJ 2187 Beauty Contest(凸包_旋转卡壳之最远点对)
来源:互联网 发布:linux ide 编辑:程序博客网 时间:2024/04/29 15:04
题目链接:http://poj.org/problem?id=2187
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
题意:
求最远点对距离的平方!
借用bin神的模板
代码如下:
#include <stdio.h>#include <string.h>#include <algorithm>#include <iostream>using namespace std;struct Point{ int x,y; Point(int _x = 0, int _y = 0) { x = _x; y = _y; } Point operator -(const Point &b)const { return Point(x - b.x, y - b.y); } int operator ^(const Point &b)const//叉积 { return x*b.y - y*b.x; } int operator *(const Point &b)const { return x*b.x + y*b.y; } void input() { scanf("%d%d",&x,&y); }};int dist2(Point a,Point b){ return (a-b)*(a-b);//向量相乘}const int MAXN = 50010;Point list[MAXN];int Stack[MAXN],top;bool _cmp(Point p1,Point p2){ int tmp = (p1-list[0])^(p2-list[0]); if(tmp > 0)return true; else if(tmp == 0 && dist2(p1,list[0]) <= dist2(p2,list[0])) return true; else return false;}void Graham(int n){ Point p0; int k = 0; p0 = list[0]; for(int i = 1; i < n; i++) if(p0.y > list[i].y || (p0.y == list[i].y && p0.x > list[i].x)) { p0 = list[i]; k = i; } swap(list[0],list[k]); sort(list+1,list+n,_cmp); if(n == 1) { top = 1; Stack[0] = 0; return; } if(n == 2) { top = 2; Stack[0] = 0; Stack[1] = 1; return; } Stack[0] = 0; Stack[1] = 1; top = 2; for(int i = 2; i < n; i++) { while(top > 1 && ((list[Stack[top-1]]-list[Stack[top-2]])^(list[i]-list[Stack[top-2]])) <= 0 ) top--; Stack[top++] = i; }}//旋转卡壳,求两点间距离平方的最大值int rotating_calipers(Point p[],int n){ int ans = 0; Point v; int cur = 1; for(int i = 0; i < n; i++) { v = p[i]-p[(i+1)%n]; while((v^(p[(cur+1)%n]-p[cur])) < 0) cur = (cur+1)%n; //printf("%d %d\n",i,cur); ans = max(ans,max(dist2(p[i],p[cur]),dist2(p[(i+1)%n],p[(cur+1)%n])));//四边形对角线距离最远 } return ans;}Point p[MAXN];int main(){ int n; while(scanf("%d",&n) == 1) { for(int i = 0; i < n; i++) list[i].input(); Graham(n); for(int i = 0; i < top; i++) p[i] = list[Stack[i]]; printf("%d\n",rotating_calipers(p,top)); } return 0;}
1 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-2178 Beauty Contest 凸包+旋转卡壳求最远点对
- POJ 2187 Beauty Contest (凸包&最远点距&旋转卡壳)
- POJ2187 Beauty Contest(最远点对,凸包+旋转卡壳)
- POJ2187-Beauty Contest- 最远点对问题(凸包+旋转卡壳)
- poj 2187 Beauty Contest(旋转卡壳求最远点对)
- POJ-2187 Beauty Contest,旋转卡壳求解平面最远点对!
- POJ 2187 Beauty Contest(凸包:最远点对距离)
- POJ 2187 Beauty Contest(凸包:最远点对距离)
- hdu-2187 Beauty Contest(旋转卡壳求平面最远点对)
- AfxGetApp()->m_pMainWnd和AfxGetMainWnd() 的区别
- Lua 字符串查找函数 string.find(s, pattern [, init [, plain]] )
- mac os 10.10以上安装win系统,mac下搞个GPT分区表的磁盘
- nginx fpm
- 黄金法则&retainCount
- POJ 2187 Beauty Contest(凸包_旋转卡壳之最远点对)
- 网络字节转换inet_aton & inet_ntoa & inet_addr和inet_pton
- 【DP+SPFA】【ZJOI2006】【bzoj1003】【cogs1824】物流运输trans
- 图示rfid系统组成及其功能
- HIVE日期函数
- 各种类和视图之间相互调用的总结
- 《C++语言基础》实践参考——指向学生类的指针
- C++实践参考——分数类的雏形
- C++实践参考——用对象数组操作长方柱类