首页 > 代码库 > hdu 3934&&poj 2079 (凸包+旋转卡壳+求最大三角形面积)
hdu 3934&&poj 2079 (凸包+旋转卡壳+求最大三角形面积)
链接:http://poj.org/problem?id=2079
Triangle
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 8173 | Accepted: 2423 |
Description
Given n distinct points on a plane, your task is to find the triangle that have the maximum area, whose vertices are from the given points.
Input
The input consists of several test cases. The first line of each test case contains an integer n, indicating the number of points on the plane. Each of the following n lines contains two integer xi and yi, indicating the ith points. The last line of the input is an integer −1, indicating the end of input, which should not be processed. You may assume that 1 <= n <= 50000 and −104<= xi, yi <= 104 for all i = 1 . . . n.
Output
For each test case, print a line containing the maximum area, which contains two digits after the decimal point. You may assume that there is always an answer which is greater than zero.
Sample Input
33 42 62 752 63 92 08 06 5-1
Sample Output
0.5027.00
Source
Shanghai 2004 Preliminary
--------------------------------------------------------------------------------------------------------------------------
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
乱起八糟的凸包,参考了n个凸包构建,但是构建方式都不同,有的凸包都还倒过来扫一遍,,,,,,不知道其中的差别
还有叉乘,叉乘没有深入理解
旋转卡壳还要重新看
1 #include <math.h> 2 #include <stdio.h> 3 #include <string.h> 4 #include <stdlib.h> 5 #include <iostream> 6 #include <algorithm> 7 8 using namespace std; 9 10 #define eps 1e-8 11 #define MAXX 1000010 12 13 typedef struct point 14 { 15 16 double x; 17 double y; 18 }point; 19 20 bool dy(double x,double y){ 21 return x>y+eps; } 22 bool xy(double x,double y){ 23 return x<y-eps; } 24 bool dyd(double x,double y){ 25 return x>y-eps; } 26 bool xyd(double x,double y){ 27 return x<y+eps; } 28 bool dd(double x,double y){ 29 return fabs(x-y)<eps; } 30 31 double crossProduct(point a,point b,point c) 32 { 33 return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x); 34 } 35 36 double dist(point a,point b) 37 { 38 39 return sqrt((b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y)); 40 } 41 42 point c[MAXX]; 43 point stk[MAXX]; 44 int top; 45 46 bool cmp(point a,point b) 47 { 48 49 double len=crossProduct(c[0],a,b); 50 if(dd(len,0.0)) 51 return xy(dist(c[0],a),dist(c[0],b)); 52 return xy(len,0.0); 53 } 54 55 double max(double x,double y) 56 { 57 58 return xy(x,y) ? y : x; 59 } 60 61 void Graham(int n) 62 { 63 64 int tmp=0; 65 for(int i=1; i<n; i++) 66 { 67 68 if(xy(c[i].x,c[tmp].x) || dd(c[i].x,c[tmp].x) && xy(c[i].y,c[tmp].y)) 69 tmp=i; 70 } 71 swap(c[0],c[tmp]); 72 sort(c+1,c+n,cmp); 73 stk[0]=c[0]; 74 stk[1]=c[1]; 75 top=1; 76 for(int i=2; i<n; i++) 77 { 78 while(top>=1 && xyd(crossProduct(stk[top],stk[top-1],c[i]),0.0)) 79 top--; 80 stk[++top]=c[i]; 81 } 82 } 83 84 double rotating(int n) 85 { 86 int j=1,k=2; 87 double ans=0.0; 88 stk[n]=stk[0]; 89 for(int i=0; i<n; i++) 90 { 91 while(dy(fabs(crossProduct(stk[(k+1)%n],stk[i],stk[j])),fabs(crossProduct(stk[k],stk[i],stk[j])))) 92 k=(k+1)%n; 93 while(dy(fabs(crossProduct(stk[k],stk[i],stk[(j+1)%n])),fabs(crossProduct(stk[k],stk[i],stk[j])))) 94 j=(j+1)%n; 95 ans=max(ans,fabs(crossProduct(stk[k],stk[i],stk[j]))); 96 } 97 return ans*0.5; 98 } 99 100 int main()101 {102 103 int i,j,n;104 while(scanf("%d",&n)!=EOF&&n != -1)105 {106 107 for(i=0; i<n; i++)108 scanf("%lf%lf",&c[i].x,&c[i].y);109 Graham(n);//printf("%d**",top);110 double ans=rotating(top+1);111 printf("%.2lf\n",ans);112 }113 return 0;114 }115
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。