http://poj.org/problem?id=1113
题意:给出一些点的坐标,和一个半径r,求出这些点围成的凸包的周长再加上一个半径为r的圆的周长。
1 #include <stdio.h> 2 #include <algorithm> 3 #include <math.h> 4 const double PI=acos(-1.0); 5 const int N=1002; 6 using namespace std; 7 8 struct Point 9 { 10 double x; 11 double y; 12 Point (double x = 0,double y = 0):x(x),y(y) {} 13 bool friend operator < (const Point &a, const Point &b) 14 { 15 return a.x < b.x||(a.x==b.x&&a.y < b.y); 16 } 17 } p[N],c[N]; 18 typedef Point Vector; 19 double dis(Point A,Point B)//两点间的距离 20 { 21 return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y)); 22 } 23 Vector operator- (Point A,Point B)//求向量 24 { 25 return Vector(B.x-A.x,B.y-A.y); 26 } 27 double Cross(Vector A,Vector B)//求叉积 28 { 29 return A.x*B.y-A.y*B.x; 30 } 31 int ConvexHull(Point *p,int n,Point *c)//计算凸包 32 { 33 int k = 0; 34 for (int i = 0; i < n; i++) 35 { 36 while(k > 1 && Cross(c[k-2]-c[k-1],c[k-2]-p[i])<=0) 37 k--; 38 c[k++] = p[i]; 39 40 } 41 int m = k; 42 for (int i = n-2; i >= 0; i--) 43 { 44 while(k > m && Cross(c[k-2]-c[k-1],c[k-2]-p[i])<=0) 45 k--; 46 c[k++] = p[i]; 47 } 48 c[k] = c[0]; 49 return k; 50 } 51 52 int main() 53 { 54 int n; 55 double r; 56 scanf("%d %lf",&n,&r); 57 for (int i = 0; i < n; i++) 58 scanf("%lf %lf",&p[i].x,&p[i].y); 59 sort(p,p+n); 60 int cnt = ConvexHull(p,n,c); 61 double len = 0; 62 for (int i = 1; i <= cnt; i++) 63 { 64 len+=dis(c[i],c[i-1]); 65 } 66 len+=2*PI*r; 67 printf("%.0f",len); 68 return 0; 69 }