POJ 3348 Cows 第一个凸包问题

http://poj.org/problem?id=3348

第一个凸包,代码参考网上凸包的做法,先记录下来,慢慢学习
Cows
Time Limit:2000MS Memory Limit:65536K



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 nextnlines contain the integer coordinates of each tree given as two integersxandyseparated 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
/* Author : yan
 * Question :
 * Date && Time :
 * Compiler : gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3
*/
#include <stdio.h>
#define N 10005
#define inf 1e-6

typedef struct
{
    int x;
    int y;
}point;
point points[N]; //点集

point chs[N];     //栈

int sp; //栈顶指针


//计算两点之间距离

double dis(point a, point b)
{
    return sqrt((a.x - b.x) * (a.x - b.x) * 1.0 + (a.y - b.y) * (a.y - b.y));
}

//通过矢量叉积求极角关系(p0p1)(p0p2)

//k > 0 ,p0p1在p0p2顺时针方向上

double multi(point p0, point p1, point p2)
{
    return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);
}

int cmp(const void *p, const void *q)
{
    point a = *(point *)p;
    point b = *(point *)q;
    double k = multi(points[0], a, b);

    if(k < -inf)
        return 1;
    else if(fabs(k) < inf && (dis(a, points[0]) - dis(b, points[0])) > inf) //两点在同一直线上的话,用最近的

        return 1;
    else return -1;
}

void convex_hull(int n)
{
    int i, k, d;
    int miny = points[0].y, index = 0;

    for(i=1; i<n; i++) //找最左下顶点

    {
        if(points[i].y < miny) //找到y坐标最小的点

        {
            miny = points[i].y;
            index = i;
        }
        else if(points[i].y == miny && points[i].x < points[index].x) //相同的话找到x最小的

        {
            index = i;
        }
    }
    //把最左下顶点放到第一个

    point temp;
    temp = points[index];
    points[index] = points[0];
    points[0] = temp;

    qsort(points+1, n-1, sizeof(points[0]), cmp); //p[1:n-1]按相对p[0]的极角从小到大排序

    chs[0] = points[n-1];
    chs[1] = points[0];
    sp = 1;
    k = 1;
    while(k <= n-1)
    {
        double d = multi(chs[sp], chs[sp-1], points[k]);
        if(d <= 0)
        {
            sp++;
            chs[sp] = points[k];
            k++;
        }
        else sp--;
    }
}

int main()
{
    int i, j, n;
    //float sum,
    float area;
    freopen("input", "r", stdin);
    scanf("%d", &n);

    for(i=0; i<n; i++)
        scanf("%d%d", &points[i].x, &points[i].y);
    convex_hull(n);

    /*for(i=0; i<=sp; i++)
        printf("(%d, %d) /n ", chs[i].x, chs[i].y);
    sum = 0;
    for(i=1; i<=sp; i++) //求周长

        sum += dis(chs[i-1], chs[i]);
    sum += dis(chs[0], chs[sp]);
    printf("周长:%f/n", sum);
    */
    double k;
    area = 0;
    for(i=1; i<sp; i++) //求面积
    {
        k = multi(chs[0], chs[i], chs[i+1]); //三角形面积是向量叉积的1/2

        area += k;
    }
    area = fabs(area) / 2;
    printf("%d", (int)(area/50));

    return 0;
}

参考:http://hi.baidu.com/fandywang_jlu/blog/item/aa4abd18e49762b74bedbc21.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值