bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘

同上(什么都不用改)

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<cmath>
 5 #include<map>
 6 #define N 100005
 7 #define eps 1e-8
 8 using namespace std;
 9 int top,n;
10 double ans;
11 struct point{double x,y;}p[N],s[N];
12 inline double dis(point a, point b)
13 {
14     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
15 }
16 inline double cross(point p1, point p2, point p0)
17 {
18     return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
19 }
20 inline bool cmp(point a, point b)
21 {
22     if (cross(a,b,p[0])==0) return dis(a,p[0])<dis(b,p[0]);
23     return cross(a,b,p[0])>0;
24 } 
25 void Graham()
26 {
27     int k=0; top=2;
28     for (int i=1; i<n; i++) 
29         if (p[k].y>p[i].y || (p[i].y==p[k].y && p[k].x>p[i].x)) k=i;
30     point t=p[0]; p[0]=p[k]; p[k]=t;
31     sort(p+1,p+n,cmp);
32     s[0]=p[0]; s[1]=p[1]; s[2]=p[2];
33     for (int i=3; i<n; i++)
34     {
35         while (top && cross(p[i],s[top],s[top-1])>=0) top--;
36         s[++top]=p[i];
37     }
38     s[++top]=p[0];
39     for (int i=0; i<top; i++)
40         ans+=dis(s[i],s[i+1]);
41 }
42 int main()
43 {
44     scanf("%d",&n);
45     for (int i=0; i<n; i++) 
46         scanf("%lf%lf",&p[i].x,&p[i].y);
47     Graham();
48     printf("%.2lf",ans);
49     return 0;
50 }

 

posted @ 2017-02-28 20:57  ws_ccd  阅读(198)  评论(0编辑  收藏  举报