POJ 3348 Cows (凸包模板+凸包面积)
Posted 抓不住Jerry的Tom
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 3348 Cows (凸包模板+凸包面积)相关的知识,希望对你有一定的参考价值。
Description
Your friend to the south is interested in building fences and turning plowshares into swords. In order to help with his overseas adventure, they are forced to save money on buying fence posts by using trees as fence posts wherever possible. Given the locations of some trees, you are to help farmers try to create the largest pasture that is possible. Not all the trees will need to be used.
However, because you will oversee the construction of the pasture yourself, all the farmers want to know is how many cows they can put in the pasture. It is well known that a cow needs at least 50 square metres of pasture to survive.
Input
The first line of input contains a single integer, n (1 ≤ n ≤ 10000), containing the number of trees that grow on the available land. The next n lines contain the integer coordinates of each tree given as two integers x and y separated by one space (where -1000 ≤ x, y ≤ 1000). The integer coordinates correlate exactly to distance in metres (e.g., the distance between coordinate (10; 11) and (11; 11) is one metre).
Output
You are to output a single integer value, the number of cows that can survive on the largest field you can construct using the available trees.
Sample Input
4
0 0
0 101
75 0
75 101
Sample Output
151
求凸包的面积/50
我们求出凸包以后将凸包化成三角形用叉积求面积再加和
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <algorithm> 5 #include <string> 6 #include <cstring> 7 using namespace std; 8 const double eps = 1e-8; 9 const double dblinf = 9999999999.9; 10 const int maxn = 1e4+50; 11 struct Point 12 { 13 double x,y; 14 }p[maxn]; 15 int stk[maxn]; 16 int top; 17 int dblcmp(double k) 18 { 19 if (fabs(k)<eps) return 0; 20 return k>0?1:-1; 21 } 22 double multi (Point p0,Point p1,Point p2)//叉乘 23 { 24 return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x); 25 } 26 double dis (Point a,Point b) 27 { 28 return sqrt( (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); 29 } 30 bool anglecmp (Point a,Point b)//极角排序 31 { 32 int d = dblcmp(multi(p[0],a,b)); 33 if (!d) return dis(p[0],a)<dis(p[0],b); 34 return d>0; 35 } 36 int n; 37 int main() 38 { 39 while (~scanf("%d",&n)){ 40 double tx = dblinf,ty = dblinf; 41 int k; 42 for (int i=0;i<n;++i){ 43 scanf("%lf%lf",&p[i].x,&p[i].y); 44 int d = dblcmp(ty-p[i].y); 45 if (!d&&dblcmp(tx-p[i].x)>0){ 46 k=i;tx = p[i].x; 47 } 48 else if (d>0){ 49 k=i; 50 tx = p[i].x,ty = p[i].y; 51 } 52 } 53 p[k].x = p[0].x,p[k].y = p[0].y; 54 p[0].x = tx,p[0].y = ty; 55 sort(p+1,p+n,anglecmp); 56 stk[0] = 0, 57 stk[1] = 1; 58 top = 1; 59 for (int i=2;i<n;++i){ 60 while (top>=1&&dblcmp(multi(p[stk[top-1]] , p[i], p[stk[top]] ))>=0) top--; 61 stk[++top] = i; 62 } 63 double area = 0; 64 for (int i=1;i<top;++i){ 65 area+=fabs(multi(p[stk[0]] , p[stk[i]] , p[stk[i+1]] )); 66 } 67 area = area /2.0;//三角形面积和别忘/2.0 68 printf("%d\n",(int)(area/50.0)); 69 } 70 return 0; 71 }
以上是关于POJ 3348 Cows (凸包模板+凸包面积)的主要内容,如果未能解决你的问题,请参考以下文章