Andrew

 1 /**************************************************
 2           Target: Andrew ——Convex Hull
 3           Author: Xue Zhonghao
 4           Date: 2014-04-07 12:24:54 
 5 **************************************************/
 6 #include<cstdio>
 7 #include<cstdlib>
 8 #include<cmath>
 9 #include<iostream>
10 using namespace std;
11 
12 #define MAXN 1000
13 
14 struct Point {
15     int x, y;
16     Point operator - (Point A) {
17       Point p;
18       p.x = x - A.x;
19       p.y = y - A.y;
20       return p;
21     }
22 };
23 
24 Point P[MAXN];
25 Point ch[MAXN];
26 
27 int cmp(const void *a, const void *b) {
28     Point p1 = *(Point *)a;
29     Point p2 = *(Point *)b;
30     if(p1.x == p2.x) return p1.y - p2.y;
31     else return p1.x - p2.x;
32 }
33 
34 int Cross(Point p1, Point p2) {
35     return p1.x*p2.y - p1.y*p2.x;
36 }
37 
38 int Andrew(Point* p, int N, Point* ch) {
39     qsort(p, N, sizeof(Point), cmp);
40     int m = 0;
41     for(int i = 0; i < N; ++i) {
42       while(m > 1 && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) m--;
43       ch[m++] = p[i];
44     }
45     int k = m;
46     for(int i = N-1; i >= 0; --i) {
47       while(m > k && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) m--;
48       ch[m++] = p[i];
49     }
50     if(N > 1) m--;
51     return m;
52 }
53 
54 double cal(Point p) {
55     return sqrt(p.x*p.x + p.y*p.y);
56 }
57 
58 double getlong(int n, Point* ch) {
59    double l = 0;
60    for(int i = 0; i < n; ++i)
61      l += cal(ch[i+1] - ch[i]);
62    return l;
63 }
64 
65 int main(void)
66 {
67     int N, m;
68     double length;
69     cin>>N;
70     for(int i = 0; i < N; ++i) cin>>P[i].x>>P[i].y;
71     m = Andrew(P, N, ch);
72     length = getlong(m, ch);
73     cout<<length<<endl;
74     system("pause");
75     return 0;
76 }

 

posted on 2014-04-07 16:56  AlanXue  阅读(133)  评论(0编辑  收藏  举报

导航