hdu1348 凸包

 1 #include<stdio.h>
 2 #include<math.h>
 3 int x[100005],y[100005],ch[100005];
 4 void sort(int l,int r)
 5 {
 6     int i,j,xx,yy,x1;
 7     i=l; j=r;
 8     xx=x[(i+j)/2]; x1=y[(i+j)/2];
 9     while (i<=j)
10     {
11         while ((x[i]<xx)||(x[i]==xx&&y[i]<x1)) i++;
12         while ((xx<x[j])||(xx==x[j]&&x1<y[j])) j--;
13         if (i<=j)
14         {
15             yy=x[i]; x[i]=x[j]; x[j]=yy;
16             yy=y[i]; y[i]=y[j]; y[j]=yy;
17             i++; j--;
18         }
19     }
20     if (l<j) sort(l,j);
21     if (i<r) sort(i,r);
22 }
23 int cross(int i,int j,int k)
24 {
25     int x1,x2,y1,y2;
26     x1=x[j]-x[i]; y1=y[j]-y[i];
27     x2=x[k]-x[i]; y2=y[k]-y[i];
28     return(x1*y2-x2*y1);
29 }
30 int main()
31 {
32     int t,n,i,m,k,judge=0;
33     double sum,r;
34     while (~scanf("%d",&t))
35     while (t--)
36     {
37         scanf("%d%lf",&n,&r);
38         for (i=1;i<=n;i++) scanf("%d%d",&x[i],&y[i]);
39         sort(1,n);
40         //͹°ü,n>=3 
41         m=1;
42         for (i=1;i<=n;i++)
43         {
44             while (m>=3&&cross(ch[m-2],ch[m-1],i)<=0) m--;
45             ch[m]=i; m++;
46         }
47         k=m-1;
48         for (i=n-1;i>=1;i--)
49         {
50             while (m>=k+2&&cross(ch[m-2],ch[m-1],i)<=0) m--;
51             ch[m]=i; m++;
52         }
53         m--; //mŽóÓÚµÈÓÚ3ÇÒ²»ÔÚÒ»ÌõÏßÉÏ 
54         printf("%d ",m); for (i=1;i<=m;i++) printf("%d ",ch[i]);
55         sum=0.0;
56         for (i=1;i<m;i++) 
57          sum+=sqrt(1.0*(x[ch[i]]-x[ch[i+1]])*(x[ch[i]]-x[ch[i+1]])+
58                    1.0*(y[ch[i]]-y[ch[i+1]])*(y[ch[i]]-y[ch[i+1]]));
59         sum+=2*3.1415926*r;
60         printf("%.0lf\n",sum);
61         if (t!=0) printf("\n");
62     }
63     return(0);
64 } 

posted on 2014-07-16 16:28  xiao_xin  阅读(79)  评论(0编辑  收藏  举报

导航