POJ 3348 Cows
大概题意:n棵树,围篱笆,如果使得面积最大。
题解:凸包,再求面积,面积除以100即可
代码:
1 #include <iostream> 2 #include <cstdlib> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 #include <queue> 7 #include <cmath> 8 using namespace std; 9 const double eps = 1E-8; 10 struct POINT{ 11 double x,y; 12 POINT( double a = 0, double b = 0 ){ x = a; y = b; } 13 }; 14 POINT pointset[10000+50]; 15 POINT ch[100000+50]; 16 double multiply(POINT sp, POINT ep, POINT op) 17 { 18 return (sp.x - op.x)*(ep.y - op.y) - ( sp.y - op.y )*( ep.x - op.x ); 19 } 20 double dist(POINT p1,POINT p2) 21 { 22 return sqrt( (p1.x - p2.x)*(p1.x - p2.x) + ( p1.y - p2.y )*( p1.y - p2.y ) ); 23 } 24 double area_of_polygon( int vcount,POINT ch[] ) 25 { 26 if(vcount<3) return 0; 27 double s; 28 s = ch[0].y*(ch[vcount-1].x-ch[1].x); 29 for(int i = 1;i < vcount ; i++) 30 s+=ch[i].y*(ch[i-1].x-ch[(i+1)%vcount].x); 31 return s/2; 32 } 33 void Graham_scan(POINT pointset[],POINT ch[],int n,int &len) 34 { 35 int k=0,top=2; 36 POINT tmp; 37 for(int i = 1; i < n; i++ ) 38 if(pointset[i].y<pointset[k].y || ( pointset[i].y == pointset[k].y ) && 39 pointset[i].x<pointset[k].x) k = i; 40 tmp = pointset[0]; 41 pointset[0] = pointset[k]; 42 pointset[k] = tmp; 43 for(int i = 1;i < n;i++) 44 { 45 k = i; 46 for(int j = i+1; j < n; j++) 47 if(multiply(pointset[j],pointset[k],pointset[0])>0|| 48 (multiply(pointset[j],pointset[k],pointset[0])==0&& 49 dist(pointset[0],pointset[k])-dist(pointset[0],pointset[j])>eps)) 50 k=j; 51 tmp = pointset[i]; 52 pointset[i] = pointset[k]; 53 pointset[k] = tmp; 54 } 55 ch[0] = pointset[0]; 56 ch[1] = pointset[1]; 57 ch[2] = pointset[2]; 58 for(int i = 3;i < n; i++) 59 { 60 while(multiply(pointset[i],ch[top],ch[top-1])>=0) 61 top--; 62 ch[++top] = pointset[i]; 63 } 64 len = top + 1; 65 } 66 int main() 67 { 68 int n; 69 cin>>n; 70 for(int i = 0; i < n; i++) 71 scanf("%lf%lf",&pointset[i].x,&pointset[i].y); 72 int len; 73 Graham_scan(pointset,ch,n,len); 74 double s = area_of_polygon(len,ch); 75 int ss = s/50; 76 cout<<ss<<endl; 77 }
浙公网安备 33010602011771号