POJ - 3348 Cows (凸包 求多邊形面積 Graham掃描算法)

Cows

 

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

題目鏈接:http://poj.org/problem?id=3348

題目大意:給n個點,求凸包的面積/50

思路:凸包模板+叉積求面積

代碼:

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
const double pi=acos(-1.0);
const int N=100005;
struct node
{
    int x,y;
} e[N],s[N];
bool cmp(node a,node b)
{
    if(a.y==b.y) return a.x<b.x;
    return a.y<b.y;
}
int cross(node a,node b,node c)
{
    return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}
int dis(node a,node b)
{
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
bool cmp1(node a,node b)
{
    int mm=cross(e[0],a,b);
    if(mm>0) return 1;
    else if(mm==0&&dis(e[0],a)-dis(e[0],b)<=0)
        return 1;
    else return 0;
}
int n,tot;
void graham()
{
    sort(e,e+n,cmp);
    sort(e+1,e+n,cmp1);
    s[0]=e[0],s[1]=e[1];
    tot=1;
    for(int i=2; i<n; i++)
    {
        while(tot&&cross(s[tot-1],s[tot],e[i])<0)
            tot--;
        s[++tot]=e[i];
    }
}
int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=0; i<n; i++)
            scanf("%d%d",&e[i].x,&e[i].y);
        graham();
        int ans=0;
        for(int i=1; i<tot; i++)
            ans+=abs(cross(e[0],s[i],s[i+1]));
        printf("%d\n",ans/100);
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章