hdu2202 凸包+旋转卡壳

 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     double max;
45     while (~scanf("%d",&n)&&n!=-1)
46     {
47         for (i=1;i<=n;i++) scanf("%d%d",&p[i].x,&p[i].y);
48         
49         sort(1,n);
50         sum=1;
51         for (i=1;i<=n;i++){
52             while (sum>=3&&cross(ch[sum-2],ch[sum-1],p[i])<=0) sum--;
53             ch[sum]=p[i];
54             sum++;
55         }
56         k=sum-1;
57         for (i=n-1;i>=1;i--){
58             while (sum>=k+2&&cross(ch[sum-2],ch[sum-1],p[i])<=0) sum--;
59             ch[sum]=p[i];
60             sum++;
61         }
62         sum--;
63 //        for (i=1;i<=sum;i++) printf("%d %d\n",ch[i].x,ch[i].y);
64         max=0.00;
65 
66 //        j=2; k=3;
67         ch[sum+1]=ch[2];
68         for (i=1;i<=sum-2;i++)
69         {
70          j=i+1; k=j+1;
71          while (area(i,j,k)<area(i,j,k+1)) {k++; if (k>sum) k=1; }
72          if (area(i,j,k)>max) max=area(i,j,k);
73          while (area(i,j,k)<area(i,j+1,k)) {j++; if (j>sum) j=1; }
74          if (area(i,j,k)>max) max=area(i,j,k);
81         }
82         printf("%.2lf\n",max);  
83     }
84 }
85 
86 
87 
88 
89  

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

导航