poj3348 Cow

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<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>

using namespace std;

const int N=10010;
const double eps=1e-8;
struct node{
    double x,y;
    node (double xx=0,double yy=0)
    {
        x=xx; y=yy;
    }
}po[N];
int sta[N*2],top,n;

node operator + (const node &a,const node &b)
{
    return node(a.x+b.x,a.y+b.y);
}
node operator - (const node &a,const node &b)
{
    return node(a.x-b.x,a.y-b.y);
}
node operator * (const node &a,const double &b)
{
    return node(a.x*b,a.y*b);
}
node operator / (const node &a,const double &b)
{
    return node(a.x/b,a.y/b);
}

int dcmp(double x)
{
    if (fabs(x)<eps) return 0;
    else
    if (x>0) return 1;
    else return -1;
}

int cmp(const node &a,const node &b)
{
    if (dcmp(a.x-b.x)!=0) return a.x<b.x;
    else return a.y<b.y; 
}

double Cross(node a,node b)
{
    return a.x*b.y-a.y*b.x;
}

double SS()
{
    int i;
    double area=0;
    for (i=2;i<top;i++)  //这里要注意是i<top 
        area+=Cross(po[sta[i]]-po[sta[1]],po[sta[i+1]]-po[sta[1]]);
    return fabs(area)/2;
}

void TuB()
{
    int i,j;
    top=0;
    for (i=1;i<=n;i++)
    {
        while (top>1&&dcmp(Cross(po[i]-po[sta[top-1]],po[sta[top]]-po[sta[top-1]]))<=0) top--;
        sta[++top]=i;
    }
    int k=top;
    for (i=n-1;i>=1;i--)
    {
        while (top>k&&dcmp(Cross(po[i]-po[sta[top-1]],po[sta[top]]-po[sta[top-1]]))<=0) top--;
        sta[++top]=i;
    }
    if (n>1) top--;
    if (top<3) printf("0");
    else
    {
        double ans=SS();
        printf("%d",(int)ans/50);
    }
    return;
}

int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++) 
        scanf("%lf%lf",&po[i].x,&po[i].y);
    sort(po+1,po+1+n,cmp);
    if (n<3) printf("0");
    else TuB();
    return 0;
}
原文地址:https://www.cnblogs.com/wutongtong3117/p/7673614.html