hdu3934 凸包

 1 #include<stdio.h>
 2 #include<math.h>
 3 typedef struct dian
 4 {
 5     int x,y;
 6 };
 7 dian p[50005],ch[50005];
 8 double area(int i,int j,int k)
 9 {
10     double a,b,c,q;
11     a=sqrt(1.0*(ch[j].x-ch[i].x)*(ch[j].x-ch[i].x)+1.0*(ch[j].y-ch[i].y)*(ch[j].y-ch[i].y));
12     b=sqrt(1.0*(ch[k].x-ch[i].x)*(ch[k].x-ch[i].x)+1.0*(ch[k].y-ch[i].y)*(ch[k].y-ch[i].y));
13     c=sqrt(1.0*(ch[j].x-ch[k].x)*(ch[j].x-ch[k].x)+1.0*(ch[j].y-ch[k].y)*(ch[j].y-ch[k].y));
14     q=(a+b+c)/2;
15     return(sqrt(q*(q-a)*(q-b)*(q-c)));
16 }
17 int cross(dian p1,dian p2,dian p3)
18 {
19     int x1=p2.x-p1.x,y1=p2.y-p1.y;
20     int x2=p3.x-p1.x,y2=p3.y-p1.y;
21     return(x1*y2-x2*y1);
22 }
23 void sort(int l,int r)
24 {
25     int i,j,x,x1,y;
26     i=l; j=r;
27     x=p[(i+j)/2].x; x1=p[(i+j)/2].y;
28     while (i<=j)
29     {
30         while (p[i].x<x||(p[i].x==x&&p[i].y<x1)) i++;
31         while (x<p[j].x||(p[j].x==x&&x1<p[j].y)) j--;
32         if (i<=j){
33             y=p[i].x; p[i].x=p[j].x; p[j].x=y;
34             y=p[i].y; p[i].y=p[j].y; p[j].y=y;
35             i++; j--;
36         }
37     }
38     if (i<r) sort(i,r);
39     if (l<j) sort(l,j);
40 }
41 int main()
42 {
43     int n,i,sum,j,k;
44     int xx,yy,zz;
45     double max;
46     while (~scanf("%d",&n)&&n!=-1)
47     {
48         for (i=1;i<=n;i++) scanf("%d%d",&p[i].x,&p[i].y);
49         
50         sort(1,n);
51         sum=1;
52         for (i=1;i<=n;i++){
53             while (sum>=3&&cross(ch[sum-2],ch[sum-1],p[i])<=0) sum--;
54             ch[sum]=p[i];
55             sum++;
56         }
57         k=sum-1;
58         for (i=n-1;i>=1;i--){
59             while (sum>=k+2&&cross(ch[sum-2],ch[sum-1],p[i])<=0) sum--;
60             ch[sum]=p[i];
61             sum++;
62         }
63         sum--;
64 //        for (i=1;i<=sum;i++) printf("%d %d\n",ch[i].x,ch[i].y);
65         max=0.00;
66 
67         j=3; k=2;
68         ch[sum+1]=ch[2];
69         for (i=1;i<=sum;i++)
70         {
71 //         j=i+1; k=j+1;
72          while (area(i,j,k)<area(i,j,k+1)) {k++; if (k>sum) k=1; }
73          if (area(i,j,k)>max) {max=area(i,j,k);
74          xx=i; yy=j; zz=k;}
75          while (area(i,j,k)<area(i,j+1,k)) {j++; if (j>sum) j=1; }
76          if (area(i,j,k)>max) {max=area(i,j,k);
77          xx=i; yy=j; zz=k;}
78 //         for (j=i+1;j<=sum-1;j++)
79 //          for (k=j+1;k<=sum;k++)
80 //          {
81 //            if (area(i,j,k)>max) max=area(i,j,k);
82 //             if (k<sum&&area(i,j,k+1)<=area(i,j,k)) break;
83 //          }
84         }
85         printf("%.2lf\n",max);  
86     }
87 }

http://acm.hdu.edu.cn/showproblem.php?pid=3934

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

导航