POJ2187 Beauty Contest(旋转卡壳)

题目链接:

  http://poj.org/problem?id=2187

题目描述:

Beauty Contest
 

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) 

 

题目大意:

  给二维平面上一些点,求最远点距离的平方

思路:

  就是最远点对,先求凸包,然后旋转卡壳

 

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <cmath>
 6 #include <vector>
 7 using namespace std;
 8 
 9 struct Point {
10     int x, y;
11     Point(int x = 0, int y = 0) :x(x), y(y) {}
12     const bool operator < (const Point A)const {
13         return x == A.x ? y < A.y : x < A.x;
14     }
15 };    //点的定义
16 
17 typedef Point Vector;    //向量的定义
18 
19 Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); }    //向量减法
20 bool operator == (const Point& a, const Point& b) {
21     return a.x == b.x && a.y == b.y;
22 }    //点相等
23 
24 int Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; }    //向量叉乘
25 
26 vector<Point> ConvexHull(vector<Point>& p) {
27     // 预处理,删除重复点
28     sort(p.begin(), p.end());
29     p.erase(unique(p.begin(), p.end()), p.end());
30 
31     int n = p.size();
32     int m = 0;
33     vector<Point> ch(n + 1);
34     for (int i = 0; i < n; i++) {
35         while (m > 1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--;
36         ch[m++] = p[i];
37     }
38     int k = m;
39     for (int i = n - 2; i >= 0; i--) {
40         while (m > k && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--;
41         ch[m++] = p[i];
42     }
43     if (n > 1) m--;
44     ch.resize(m);
45     return ch;
46 }
47 
48 int Dist2(Point A, Point B) { return (A.x - B.x)*(A.x - B.x) + (A.y - B.y)*(A.y - B.y); }
49 
50 int diameter2(vector<Point>& points) {
51     vector<Point> p = ConvexHull(points);
52     int n = p.size();
53     if (n == 1) return 0;
54     if (n == 2) return Dist2(p[0], p[1]);
55     p.push_back(p[0]); // 免得取模
56     int ans = 0;
57     for (int u = 0, v = 1; u < n; u++) {
58         // 一条直线贴住边p[u]-p[u+1]
59         while (1) {
60             int diff = Cross(p[u + 1] - p[u], p[v + 1] - p[v]);
61             if (diff <= 0) {
62                 ans = max(ans, Dist2(p[u], p[v])); // u和v是对踵点
63                 if (diff == 0) ans = max(ans, Dist2(p[u], p[v + 1])); // diff == 0时u和v+1也是对踵点
64                 break;
65             }
66             v = (v + 1) % n;
67         }
68     }
69     return ans;
70 }    // 返回点集直径的平方
71 
72 int main() {
73     int n, a, b;
74     vector<Point> P;
75     cin >> n;
76     for (int i = 0; i < n; ++i) {
77         scanf("%d%d", &a, &b);
78         P.push_back(Point(a, b));
79     }
80     printf("%d\n", diameter2(P));
81 }

 

转载于:https://www.cnblogs.com/hyp1231/p/7100259.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值