poj2187 Beauty Contest 凸包 Graham扫描
来源:互联网 发布:淘宝关键词的大中小词 编辑:程序博客网 时间:2024/06/05 04:04
Beauty Contest
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 35619 Accepted: 11040
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)
题目大意:
平面上有n个牧场,i号牧场的位置在格点(xi,yi),所有牧场的位置互不相同,请计算距离最远的两个牧场的距离,输出最远距离的平方。
思路:
显然暴力枚举每两个点的距离会超时,需要避免计算一些不必要的点对。
由于最远点对一定在凸包上,所以只要枚举凸包上所有点对,计算其距离即可。
凸包算法有很多,这里用比较易于实现的Graham扫描算法。
#include <cstdio> #include <string> #include <iostream>#include <cstring>#include <cctype>#include <algorithm>#include <functional>#include <cstdlib>#include <list>#include <utility>#include <vector>#include <bitset>using namespace std;typedef long long LL;const double EPS = 1e-6;//考虑误差的加法运算double Add(double a, double b) { if (abs(a + b) < EPS * (abs(a) + abs(b))) return 0; return a + b;}//二维向量结构体struct Point2 { double x, y; Point2() {} Point2(double x, double y) : x(x), y(y) {} Point2 operator+(const Point2& p) const { return Point2(Add(x, p.x), Add(y, p.y)); } Point2 operator-(const Point2& p) const { return Point2(Add(x, -p.x), Add(y, -p.y)); } Point2 operator*(double d) { return Point2(x*d, y*d); } //內积 double Dot(const Point2& p) const { return Add(x*p.x, y*p.y); } //外积 double Det(const Point2& p) const { return Add(x*p.y, -y*p.x); }};bool CmpXY(const Point2& p, const Point2& q) { if (p.x != q.x) return p.x < q.x; return p.y < q.y;}//求凸包vector<Point2> GrahamScan(Point2* ps, int n) { sort(ps, ps + n, CmpXY); int k = 0; //凸包的顶点数 vector<Point2> qs(n << 1); //构造中的凸包 //构造凸包的下侧 for (int i = 0; i < n; ++i) { while (k > 1 && (qs[k - 1] - qs[k - 2]).Det(ps[i] - qs[k - 1]) <= 0) --k; qs[k++] = ps[i]; } //构造凸包上侧 for (int i = n - 2, t = k; i >= 0; --i) { while (k > t && (qs[k - 1] - qs[k - 2]).Det(ps[i] - qs[k - 1]) <= 0) --k; qs[k++] = ps[i]; } qs.resize(k - 1); return qs;}double Dist(const Point2& p, const Point2& q) { return (p - q).Dot(p - q);}const int maxn = 5e4 + 5;int n;Point2 ps[maxn];int main() {#ifdef NIGHT_13 freopen("in.txt", "r", stdin);#endif std::ios::sync_with_stdio(false); while (scanf("%d", &n) == 1) { for (int i = 0; i < n; ++i) { scanf("%lf%lf", &ps[i].x, &ps[i].y); } vector<Point2> ConvexHull = GrahamScan(ps, n); double ans = 0; for (int i = 0; i < ConvexHull.size(); ++i) { for (int j = 0; j < i; ++j) { ans = max(ans, Dist(ConvexHull[i], ConvexHull[j])); } } printf("%.0f\n", ans); } return 0;}
0 0
- poj2187 Beauty Contest 凸包 Graham扫描
- POJ2187--凸包--Beauty Contest
- poj2187 Beauty Contest 凸包
- POJ 2187 Beauty Contest(凸包-Graham扫描法)
- 【POJ 2187】 Beauty Contest (凸包-Graham扫描算法)
- POJ2187 Beauty Contest 凸包+旋转卡壳
- poj2187 Beauty Contest 凸包+旋转卡壳
- 【POJ2187】Beauty Contest-凸包+旋转卡壳
- poj 2187 Beauty Contest 凸包 Graham
- POJ 2187-Beauty Contest(凸包-Graham扫描法/旋转卡壳法)
- poj2187 Beauty Contest 平面最远点对(凸包)
- POJ2187 Beauty Contest(凸包+对踵点对求解)
- [POJ2187]Beauty Contest(凸包+旋转卡壳)
- poj2187——Beauty Contest(凸包+旋转卡壳)
- POJ2187:Beauty Contest(凸包 & 旋转卡壳)
- poj2187-Beauty Contest(极角排序+Graham Scan)
- poj 2187 Beauty Contest (凸包 Graham)
- POJ2187-Beauty Contest
- 关于提升效率的思考
- 让Win10管理员账户运行Windows应用
- 判断Emacs是否在图形环境中的正确方法
- Service隐示启动
- HDU 1233 还是畅通工程(最小生成树kruskal)
- poj2187 Beauty Contest 凸包 Graham扫描
- JavaScript特效—遮罩层
- Balanced Binary Tree(Java)
- 注册MiniFilter简单的隐藏自身
- tjut 5894
- c语言词法分析器的简单实现
- 对gcc一些优化项目的具体说明
- POJ 2778 DNA Sequence AC自动机+DP+快速幂
- U8+12.5相关服务和端口