http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1393

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

1393: Robert Hood

 

Description

1393: Robert Hood 旋转卡壳 凸包

Input

1393: Robert Hood 旋转卡壳 凸包

Output

1393: Robert Hood 旋转卡壳 凸包

Sample Input

5
-4 1
-100 0
0 4
2 -3
2 300

Sample Output

316.86590223

HINT

 

1393: Robert Hood 旋转卡壳 凸包

 

Source

 

 

分析:

给你 N 个点, 求所有点中最远两点距离。即是凸包直径。

凸包+旋转卡壳 

 

AC代码:

 1 #include<stdio.h>
 2 #include<math.h>
 3 #include<string.h>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 const int maxn = 100000+10;
 8 int n,m;
 9 
10 struct Point{
11     double x,y;
12     Point(){};
13     Point(double _x, double _y)
14     {
15         x = _x;
16         y = _y;
17     }
18 
19     Point operator - (const Point & B) const
20     {
21         return Point(x-B.x, y-B.y);
22     }
23 }p[maxn], ch[maxn];
24 
25 bool cmp(Point p1, Point p2)
26 {
27     if(p1.x == p2.x) return p1.y < p2.y;
28     return p1.x < p2.x;
29 }
30 
31 int squarDist(Point A, Point B) /**距离的平方*/
32 {
33     return (A.x-B.x)*(A.x-B.x) + (A.y-B.y)*(A.y-B.y);
34 }
35 
36 double Cross(Point A, Point B) /**叉积*/
37 {
38     return A.x*B.y-A.y*B.x;
39 }
40 
41 void ConvexHull() /** 基于水平的Andrew算法求凸包 */
42 {
43     sort(p,p+n,cmp); /**先按照 x 从小到大排序, 再按照 y 从小到大排序*/
44     m = 0;
45 
46     for(int i = 0; i < n; i++) /** 从前往后找 */
47     {
48         while(m > 1 && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) m--;
49         ch[m++] = p[i];
50     }
51     int k = m;
52     for(int i = n-2; i >= 0; i--) /**从后往前找, 形成完整的封闭背包*/
53     {
54         while(m > k && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) m--;
55         ch[m++] = p[i];
56     }
57     if(n > 1) m--;
58 }
59 
60 int rotating_calipers() /**旋转卡壳模板*/
61 {
62     int q = 1;
63     int ans = 0;
64     ch[m] = ch[0]; /**凸包边界处理*/
65     for(int i = 0; i < m; i++) /**依次用叉积找出凸包每一条边对应的最高点*/
66     {
67         while(Cross(ch[i+1]-ch[i], ch[q+1]-ch[i]) > Cross(ch[i+1]-ch[i], ch[q]-ch[i]))
68             q = (q+1)%m;
69         ans = max(ans, max(squarDist(ch[i], ch[q]), squarDist(ch[i+1], ch[q+1])));
70     }
71     return ans;
72 }
73 
74 int main()
75 {
76     while(scanf("%d", &n) != EOF)
77     {
78         if(n == 0) break;
79         for(int i = 0; i < n; i++)
80             scanf("%lf%lf", &p[i].x, &p[i].y);
81 
82         ConvexHull();
83 
84         printf("%.8lf\n", sqrt(rotating_calipers()));
85     }
86     return 0;
87 }
View Code

相关文章:

  • 2021-07-31
  • 2021-05-27
  • 2021-07-25
  • 2022-02-10
  • 2022-02-24
  • 2022-01-16
  • 2022-12-23
  • 2021-12-17
猜你喜欢
  • 2022-02-27
  • 2021-11-18
  • 2021-06-15
  • 2021-07-25
  • 2021-09-14
  • 2021-09-29
  • 2021-11-03
相关资源
相似解决方案