1. 程式人生 > >Cows (Andrew演算法描述,計算凸包)

Cows (Andrew演算法描述,計算凸包)

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 yseparated 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

程式碼:

(1)常規做法

#include<iostream>
#include<algorithm>
#include<iomanip>
#include<stdio.h>
using namespace std;
struct P{
    int x;
    int y;
};
P p[10005],ch[10005];
double area(int n)//求面積模板 
{
	int i;
	double sum=0;
	for(i=2;i<n;i++)
	{   //百度公式 
	    sum+=1.0/2.0*((ch[i-1].x-ch[0].x)*(ch[i].y-ch[0].y)-(ch[i].x-ch[0].x)*(ch[i-1].y-ch[0].y));
	}
	return sum;
} 

bool cmp(P x,P y)
{
	if(x.x==y.x) return x.y<y.y;//x從小到大排序,如果x相同則y從小到大排序
	else return x.x<y.x;
    //return x.x<y.x||(x.x==y.x&&x.y<y.y);//也可以這樣 
}
int Cross(P x,P y,P z)//這一部分其實就是求面積 
{                     //(向量)a x (向量)b = axby - aybx 
    int x1=x.x-y.x;   //叉乘是有正負性的 
    int y1=x.y-y.y;
    int x2=z.x-y.x;
    int y2=z.y-y.y;
    if((x1*y2-x2*y1)<=0) return 0;//小於0(=0可能有兩個點相同)說明這個點應該捨去 
    return 1;
}

int ConvexHull(P *p,int n,P *ch)//求邊界模板,p為原陣列,ch為邊界陣列 
{
    sort(p,p+n,cmp);
    int m=0,i;
    for(i=0;i<n;i++)//從左到右掃描
    {
        while( m>1 && !Cross(ch[m-1],ch[m-2],p[i])) m--;
        ch[m++]=p[i];
    }
    int k=m;
    for(i=n-2;i>=0;i--)//從右到左掃描
    {
        while( m>k && !Cross(ch[m-1],ch[m-2],p[i])) m--;
        ch[m++]=p[i];
    }
    if(n>1) m--;//凸包有m個頂點
    return m;
}

int main()
{
    int n;
    while(cin>>n&&n)
	{                        
	    double sum=0;        
        for(int i=0;i<n;i++)//n邊形 
            cin>>p[i].x>>p[i].y;
		int m,num=0; 
		m=ConvexHull(p,n,ch);	    
        sum=area(m);
        num=sum/50;
        cout<<num<<endl;
    }
    return 0;
}

(2)運算子過載(個人感覺這個更簡單)

#include<iostream>
#include<cstdio>
#include<iomanip>
#include<cmath>
#include<algorithm>
using namespace std;
struct Pt
{
    int x;
    int y;
    Pt(int x=0,int y=0):x(x),y(y){}
};
Pt p[1005],ch[1005];

Pt operator - (Pt a,Pt b){//過載運算子 
   return Pt(a.x-b.x,a.y-b.y);
}

double det (Pt a, Pt b) {//求劃分的每一個小三角形的面積 
   return a.x * b.y - a.y * b.x; 
}

bool cmp(Pt x,Pt y){
	if(x.x==y.x) return x.y<y.y;
	else return x.x<y.x; 
}

double PolygonArea (Pt* p,int n){//求n邊行面積 
    double area =0;
    for(int i=1;i<n -1; i++)
    area += det (ch[i]-ch[0] ,ch[i+1] -ch [0]);
    return fabs ( area /2);
}
int ConvexHull ( Pt * p,int n, Pt * ch ){//求凸包 
   sort (p,p+n,cmp ); 
   int m=0;
   for (int i=0;i<n;i ++){
     while (m >1&& det(ch[m -1] - ch[m -2] ,p[i]-ch[m -2]) <=0)
	       m --;
     ch[m ++]= p[i];
   }
   int k=m;
   for (int i=n -2;i >=0;i--){
     while (m>k&& det(ch[m -1] - ch[m -2] ,p[i]-ch[m -2]) <=0) 
	      m --;
     ch[m ++]= p[i];
   }
   if(n >1) m --;
   return m;
}

int main()
{
    int n;
    while(cin>>n&&n)
	{                        
	    double sum=0;        
        for(int i=0;i<n;i++)
            cin>>p[i].x>>p[i].y; 
		int m=ConvexHull (p,n,ch );		    
        sum=PolygonArea(ch,m);
        int k;
        k=sum/50;
        cout<<k<<endl;
    }
    return 0;
}