1. 程式人生 > >POJ3348 求凸包並計算面積

POJ3348 求凸包並計算面積

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

#include<algorithm>
#include<stdio.h>
#include<math.h> 
using namespace std;
int n,i,tot;
struct point
{
    double x,y;
};
point a[1005],p[1005];
 
double dis(point A,point B)            //A B兩點距離       
{
    return sqrt((B.x-A.x)*(B.x-A.x)+(B.y-A.y)*(B.y-A.y));
}
 
double xmul(point A,point B,point C)         
{
    return (B.x-A.x)*(C.y-A.y)-(B.y-A.y)*(C.x-A.x);//叉積,多邊形(凸或非凸)直接計算,結果/2; 
}
 
int cmp(point A,point B)                     
{
    return (A.x<B.x||(A.x==B.x&&A.y<B.y));   
}
 
void Andrew()        //沒有返回值                        
{
    sort(a,a+n,cmp);
    tot=0;
    for(int i=0;i<n;i++)
    {
        while(tot>1&&xmul(p[tot-2],p[tot-1],a[i])<=0) tot--; 
        p[tot++]=a[i];
    }
    int k=tot;
    for(int i=n-2;i>=0;i--)
    {
        while(tot>k&&xmul(p[tot-2],p[tot-1],a[i])<0) tot--;
        p[tot++]=a[i];
    }
    if(n>1) tot--;//共tot個點 
}
double ans=0;
int main()
{
    double ans=0;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    scanf("%lf%lf",&a[i].x,&a[i].y);//存進a[i]裡面但是p[i]好像同步了,不知道??? 
    Andrew();
    double x1,x2,y1,y2;
    for(i=1;i<tot-1;i++)
    {
        ans+=xmul(p[i],p[i+1],p[0]);
        //x1=p[i].x -p[0].x ;
        //y1=p[i].y -p[0].y ;這樣寫不能AC,WHY??? 
        //x2=p[i+1].x -p[0].x ;
        //y2=p[i+1].y -p[0].y ;
        //ans+=(x1*y2+x2*y1);
    }
    ans=fabs(ans);
    printf("%d\n",int(ans/100));//前面沒有除2故是100 
}