cogs 896 圈奶牛

 

 求凸包裸题

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<vector>
 6 #include<cmath>
 7 #define dd double
 8 using namespace std;
 9 const int N=10006;
10 const dd tiny=0.0000001;
11 
12 struct son
13 {
14     dd x,y;
15     friend bool operator < (son a,son b){return a.x<b.x;}
16     friend son operator - (son a,son b){return (son){a.x-b.x,a.y-b.y};}
17 }ji[N];
18 int n,he;
19 son zhan[N*5];
20 vector<son> q;
21 
22 dd chacheng(son a,son b)
23 {
24     return a.x*b.y-b.x*a.y;
25 }
26 
27 int check(son a,son b,son c)
28 {
29     return chacheng(b-a,c-b)>tiny;
30 }
31 
32 void xiatubao()
33 {
34     he=0;zhan[++he]=ji[1];zhan[++he]=ji[2];
35     for(int i=3;i<=n;++i)
36     {
37         while(he>=2&&!check(zhan[he-1],zhan[he],ji[i]))--he;
38         zhan[++he]=ji[i];
39     }
40     for(int i=1;i<=he;++i)
41       q.push_back(zhan[i]);
42 }
43 
44 void shangtubao()
45 {
46     he=0;zhan[++he]=ji[n];zhan[++he]=ji[n-1];
47     for(int i=n-2;i>=1;--i)
48     {
49         while(he>=2&&!check(zhan[he-1],zhan[he],ji[i]))--he;
50         zhan[++he]=ji[i];
51     }
52     for(int i=1;i<=he;++i)
53       q.push_back(zhan[i]);
54 }
55 void out11();
56 void Graham()
57 {
58     xiatubao();
59     //out11();
60     shangtubao();
61     //out11();
62 }
63 
64 dd jisuan()
65 {
66     dd ans=0;
67     for(int i=0,s=q.size()-1;i<s;++i)
68         ans+=sqrt( (q[i].x-q[i+1].x)*(q[i].x-q[i+1].x)+(q[i].y-q[i+1].y)*(q[i].y-q[i+1].y) );
69     return ans;
70 }
71 
72 void out11()
73 {
74     printf("\n");
75     for(int i=1;i<=n;++i)
76       printf("%.2lf %.2lf\n",ji[i].x,ji[i].y);
77     printf("\n");
78     printf("\n");
79     for(int i=0,s=q.size();i<s;++i)
80       printf("%.2lf %.2lf\n",q[i].x,q[i].y);
81     printf("\n");
82 }
83 
84 int main(){
85     freopen("fc.in","r",stdin);
86     freopen("fc.out","w",stdout);
87     scanf("%d",&n);
88     for(int i=1;i<=n;++i)
89       scanf("%lf%lf",&ji[i].x,&ji[i].y);
90     sort(ji+1,ji+1+n);
91     Graham();
92     printf("%.2lf",jisuan());
93     //while(1);
94     return 0;
95 }
水平序

 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<vector>
 6 #include<cmath>
 7 #define dd double
 8 using namespace std;
 9 const int N=10006;
10 struct son
11 {
12     dd x,y;
13 }ji[N];
14 dd dis(son a,son b){return sqrt( (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y) );}
15 son operator - (son a,son b){return (son){a.x-b.x,a.y-b.y};}
16 dd operator * (son a,son b){return a.x*b.y-a.y*b.x;}
17 void swap(son &x,son &y)
18 {
19     son temp=x;x=y;y=temp;
20 }
21 bool operator < (son a,son b)
22 {
23     dd s=(a-ji[1])*(b-ji[1]);
24     if(s==0)return dis(ji[1],a)<dis(ji[1],b);
25     return s>0;
26 }
27 
28 int n,he;
29 son zhan[N*5];
30 dd ans;
31 
32 void Graham()
33 {
34     he=0;zhan[++he]=ji[1];
35     for(int i=2;i<=n;++i)
36     {
37         while(he>=2&&(zhan[he]-zhan[he-1])*(ji[i]-zhan[he])<0)--he;
38         zhan[++he]=ji[i];
39     }
40 }
41 
42 int main(){
43     freopen("fc.in","r",stdin);
44     freopen("fc.out","w",stdout);
45     //freopen("2.txt","w",stdout);
46     scanf("%d",&n);
47     int k=1;
48     for(int i=1;i<=n;++i)
49     {
50       scanf("%lf%lf",&ji[i].x,&ji[i].y);
51       if(ji[i].x<ji[k].x&&ji[i].y<ji[k].y)k=i;
52     }
53     swap(ji[1],ji[k]);
54     sort(ji+2,ji+1+n);
55     Graham();
56     for(int i=1;i<he;++i)
57       ans+=dis(zhan[i],zhan[i+1]);
58     ans+=dis(zhan[he],zhan[1]);
59     printf("%.2lf",ans);
60     //while(1);
61     return 0;
62 }
极角序

 

posted @ 2017-08-05 17:31  A_LEAF  阅读(132)  评论(0编辑  收藏  举报