Link: http://poj.org/problem? Id = 2079 triangle
Time limit:3000 Ms |
|
Memory limit:30000 K |
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 shoshould 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 variable | | | | convex Hull, I have referenced n convex hull construction, but the construction methods are different. Some convex hull are also scanned over and over again. I don't know the difference between them and the cross multiplication, if you don't have a deep understanding of the Cross-ride, you have to re-view the rotary jamming case.
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
View code