poj 3348 Cows

来源:互联网 发布:淘宝医药商城 编辑:程序博客网 时间:2024/05/16 01:06

题意:给出若干点的位置,计算由这些点组成凸包的面积S(农场面积),每头牛至少要50平方米,

所以输出S/50的整数部分即可



计算凸包面积

n边形可以划成n-2个三角形

已知三角形顶点坐标,求三角形面积最直接的公式

设A(x1,y1),B(x2,y2),C(x3,y3)在坐标系中中顺序为三点按逆时针排列

S=1/2[(x1y2-x2y1)+(x2y3-x3y2)+(x3y1-x1y3)]


#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;const int MAXN = 10005;struct node{    double x,y;}p[MAXN];int res[MAXN],top;int n;bool cmp(const node &a, const node &b){    if(a.y == b.y)        return a.x < b.x;    return a.y < b.y;}double multi(node &a, node &b, node &c){    return (a.x - c.x)*(b.y - c.y) - (b.x - c.x)*(a.y - c.y);}double square(node &op, node &sp, node &ep){    return (op.x*sp.y - sp.x*op.y + sp.x*ep.y - ep.x*sp.y + ep.x*op.y - op.x*ep.y)/2;}void Graham(){    int len;    top = 1;    sort(p,p+n,cmp);    for(int i = 0; i < 3; i++)        res[i] = i;    for (int i = 2; i < n; ++i)    {        while(top && multi(p[i], p[res[top]], p[res[top-1]]) >= 0)            top--;        res[++top] = i;    }    len = top;    res[++top] = n-2;    for(int i = n-3; i >= 0; i--)    {        while(top != len && multi(p[i], p[res[top]], p[res[top-1]]) >= 0)            top--;        res[++top] = i;    }}int main(int argc, char const *argv[]){    double ans;    while(~scanf("%d", &n))    {        for(int i = 0; i < n; i++)            scanf("%lf %lf", &p[i].x, &p[i].y);        Graham();        ans = 0;        for(int i = 1; i < top-1; i++)        {            ans += square(p[res[i]], p[res[i+1]], p[res[0]]);//开始求n-2个三角形的面积        }        ans /= 50;        int hub = (int)ans;        printf("%d\n", hub);    }    return 0;}


0 0
原创粉丝点击