poj 3348:Cows(计算几何,求凸包面积)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 6199 | Accepted: 2822 |
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
Source
1 #include <iostream>
2 #include <iomanip>
3 #include <cmath>
4 #include <stdio.h>
5 using namespace std;
6
7 struct Point{
8 double x,y;
9 };
10 double dis(Point p1,Point p2)
11 {
12 return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
13 }
14 double xmulti(Point p1,Point p2,Point p0) //求p1p0和p2p0的叉积,如果大于0,则p1在p2的顺时针方向
15 {
16 return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
17 }
18 int graham(Point p[],int n,int pl[]) //求凸包,返回凸包中点的个数+1
19 {
20 //找到纵坐标(y)最小的那个点,作第一个点
21 int i;
22 int t = 1;
23 for(i=1;i<=n;i++)
24 if(p[i].y < p[t].y)
25 t = i;
26 pl[1] = t;
27 //顺时针找到凸包点的顺序,记录在 int pl[]
28 int num = 1; //凸包点的数量
29 do{ //已确定凸包上num个点
30 num++; //该确定第 num+1 个点了
31 t = pl[num-1]+1;
32 if(t>n) t = 1;
33 for(int i=1;i<=n;i++){ //核心代码。根据叉积确定凸包下一个点。
34 double x = xmulti(p[i],p[t],p[pl[num-1]]);
35 if(x<0) t = i;
36 }
37 pl[num] = t;
38 } while(pl[num]!=pl[1]);
39 return num-1;
40 }
41 double getS(Point a,Point b,Point c) //返回三角形面积
42 {
43 return ((b.x - a.x) * (c.y - a.y) - (b.y - a.y)*(c.x - a.x))/2;
44 }
45 double getPS(Point p[],int pl[],int n) //返回多边形面积。必须确保 n>=3,且多边形是凸多边形
46 {
47 double sumS=0;
48 for(int i=2;i<=n-1;i++)
49 sumS+=fabs(getS(p[pl[1]],p[pl[i]],p[pl[i+1]]));
50 return sumS;
51 }
52 int main()
53 {
54 int n;
55 while(cin>>n){
56 Point p[10005];
57 int pl[10005];
58 int i;
59 for(i=1;i<=n;i++) //输入点
60 scanf("%lf%lf",&p[i].x,&p[i].y);
61 int num = graham(p,n,pl); //求凸包,并返回凸包点个数
62 printf("%d\n",int(getPS(p,pl,num)/50)); //求凸包面积
63 }
64 return 0;
65 }
Freecode : www.cnblogs.com/yym2013