poj3348-Cows 求凸包及其面积

来源:互联网 发布:越国以鄙远 君知其难也 编辑:程序博客网 时间:2024/06/07 06:15
Cows
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 9586 Accepted: 4201

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

40 00 10175 075 101

Sample Output

151
题目大意:给你n个点坐标,求这n个坐标构成的凸包的面积(直接套用凸包模板求面积就可以了)
刚开始以为只要求多边形面积就可以了,然后才意识到这些点的坐标不是顺序给你的。
ac代码如下:
#include<iostream>#include<algorithm>using namespace std;const int Max = 10005; struct Point{    double x, y;}p[Max];int n, res[Max], top; bool cmp(Point &a, Point &b){    if(a.y == b.y) return a.x < b.x;    return a.y < b.y;} bool mult(Point sp, Point ep, Point op){    return (sp.x-op.x)*(ep.y-op.y) >= (ep.x-op.x)*(sp.y-op.y);} void Graham(){    int i, len;    top = 1;    sort(p, p+n, cmp);    for(i = 0; i < 3; i ++) res[i] =i;    for(i = 2; i < n; i ++){        while(top && mult(p[i], p[res[top]], p[res[top-1]])) top --;        res[++ top] = i;    }    len = top;    res[++ top] = n - 2;    for(i = n - 3; i >= 0; i --){        while(top != len && mult(p[i], p[res[top]], p[res[top-1]])) top --;        res[++ top] = i;    }} double area(){    double sum;    sum = p[res[0]].y * (p[res[top-1]].x-p[res[1]].x);    for(int i = 1; i < top; i ++)        sum += p[res[i]].y * (p[res[i-1]].x-p[res[(i+1)%top]].x);    return sum / 2;} int main(){    while(scanf("%d", &n) != EOF){        for(int i = 0; i < n; i ++)            scanf("%lf%lf", &p[i].x, &p[i].y);        if(n < 3){     //  特殊情况1:树不够2棵。            printf("0\n"); continue;        }        Graham();        if(top < 3){   //  特殊情况2:树拍成一排,当然这里对求面积没有影响,可以省去。            printf("0\n"); continue;        }        double ans = area();        printf("%d\n", (int)(ans/50));    }    return 0;}


题目网址点击打开链接http://poj.org/problem?id=3348
 
原创粉丝点击