码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1113&&HDU 1348

时间:2015-10-03 14:24:15      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

题意:凸包周长+一个完整的圆周长。因为走一圈,经过拐点时,所形成的扇形的内角和是360度,故一个完整的圆。

模板题,之前写的Graham模板不对,WR了很多发。。。。POJ上的AC代码

技术分享
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<set>
 6 #include<stdio.h>
 7 #include<stdlib.h>
 8 #include<math.h>
 9 #define clc(a,b) memset(a,b,sizeof(a))
10 #define eps 1e-8
11 #define PI acos(-1.0)
12 typedef long long LL;
13 const int mod=47000;
14 const int inf=0x3f3f3f3f;
15 const int MAXN=1000;
16 using namespace std;
17 
18 const int N = 1005;
19 
20 int top;
21 struct point
22 {
23     double x;
24     double y;
25 } p[N], Stack[N];
26 
27 double dis(point A, point B)
28 {
29     return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
30 }
31 
32 double crossProd(point A, point B, point C)
33 {
34     return (B.x-A.x)*(C.y-A.y) - (B.y-A.y)*(C.x-A.x);
35 }
36 
37 
38 int cmp(const void *a, const void *b)
39 {
40     point *c = (point *)a;
41     point *d = (point *)b;
42     double k = crossProd(p[0], *c, *d);
43     if (k<0 || !k && dis(p[0], *c)>dis(p[0], *d))
44     return 1;
45     return -1;
46 }
47 
48 void Graham(int n)
49 {
50     double x = p[0].x;
51     double y = p[0].y;
52     int mi = 0;
53     for (int i=1; i<n; ++i)
54     {
55         if (p[i].x<x || (p[i].x==x && p[i].y<y))
56         {
57             x = p[i].x;
58             y = p[i].y;
59             mi = i;
60         }
61     }
62     point tmp = p[mi];
63     p[mi] = p[0];
64     p[0] = tmp;
65     qsort(p+1, n-1, sizeof(point), cmp);
66     p[n] = p[0];
67     Stack[0] = p[0];
68     Stack[1] = p[1];
69     Stack[2] = p[2];
70     top = 2;
71     for (int i=3; i<=n; ++i)
72     {
73         while (crossProd(Stack[top-1], Stack[top], p[i])<=0 && top>=2) --top;
74         Stack[++top] = p[i];
75     }
76 }
77 
78 int main()
79 {
80     int n, l;
81     while (scanf("%d%d",  &n, &l) != EOF)
82     {
83             for (int i=0; i<n; ++i)
84             {
85                 scanf ("%lf%lf", &p[i].x, &p[i].y);
86             }
87             Graham(n);
88             double ans=0;
89             for(int i=0;i<top;i++)
90             {
91                 ans+=dis(Stack[i], Stack[i+1]);
92             }
93             ans += PI * (l + l);
94             printf ("%d\n", (int)(ans+0.5));
95     }
96     return 0;
97 }
View Code

 

POJ 1113&&HDU 1348

标签:

原文地址:http://www.cnblogs.com/ITUPC/p/4853333.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!