题意:
求凸包周长
题解:
注意特判就好,两个点时特判,不知为什么。。
View Code
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #include <algorithm> 6 #include <cmath> 7 8 #define N 120 9 10 using namespace std; 11 12 struct PO 13 { 14 int x,y; 15 }p[N]; 16 17 int stk[N],top,n; 18 19 inline bool cmp(const PO &a,const PO &b) 20 { 21 if(a.x==b.x) return a.y<b.y; 22 return a.x<b.x; 23 } 24 25 inline double getdis(PO &a,PO &b) 26 { 27 return sqrt(double((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y))); 28 } 29 30 inline int cross(PO &a,PO &b,PO &c) 31 { 32 return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x); 33 } 34 35 inline void read() 36 { 37 for(int i=1;i<=n;i++) scanf("%d%d",&p[i].x,&p[i].y); 38 } 39 40 inline void graham() 41 { 42 sort(p+1,p+1+n,cmp); 43 top=0; 44 stk[++top]=1; stk[++top]=2; 45 for(int i=3;i<=n;i++) 46 { 47 while(top>=2&&cross(p[stk[top-1]],p[stk[top]],p[i])<=0) top--; 48 stk[++top]=i; 49 } 50 int tmp=top; 51 stk[++top]=n-1; 52 for(int i=n-1;i>=1;i--) 53 { 54 while(top>=tmp+1&&cross(p[stk[top-1]],p[stk[top]],p[i])<=0) top--; 55 stk[++top]=i; 56 } 57 } 58 59 inline void go() 60 { 61 if(n==1) {printf("0.00\n");return;} 62 else if(n==2) {printf("%.2lf\n",getdis(p[1],p[2]));return;}//两个点要特判,不知为什么,三点共线就没事。。 63 graham(); 64 double ans=0.0; 65 for(int i=1;i<top;i++) ans+=getdis(p[stk[i]],p[stk[i+1]]); 66 printf("%.2lf\n",ans); 67 } 68 69 int main() 70 { 71 while(scanf("%d",&n),n) read(),go(); 72 return 0; 73 }