poj 3348 Cows

凸包模板题,求出凸包面积,再除以50就可以了。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;
#define eps 1e-10
#define N 1005
struct Point
{
      double x, y;
}p[N], res[N];
int dcmp(double x)
{
      if (fabs(x) < eps)
            return 0;
      return x>eps?1:-1;
}
double Cross(Point a, Point b, Point c)
{
      return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}
double dist(Point a, Point b)
{
      return sqrt( (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y) );
}
int cmp(const void *a, const void *b)
{
      Point c = *(Point *)a;
      Point d = *(Point *)b;
      double t = Cross(p[0], c, d);
      if (t)
            return -t;
      return dist(p[0], d)-dist(p[0], c);
}
int graham(int n)
{
      int i, j, k, top = 2;
      j = 0;
      for (i=1; i<n; i++)
      {
            if ( p[i].y<p[j].y || (p[i].y==p[j].y && p[i].x<p[j].x) )
                  j = i;
      }
      if (j)
      {
            Point temp = p[j];
            p[j] = p[0];
            p[0] = temp;
      }
      qsort(p+1, n-1, sizeof(p[0]), cmp);
      res[0] = p[0];
      res[1] = p[1];
      res[2] = p[2];
      for (int i=3; i<n; i++)
      {
            while ( top>1 && dcmp(Cross(res[top-1], res[top], p[i]))<=0 )
                  --top;
            res[++top] = p[i];
      }
      return top;
}


int main()
{
      int n, r, t, area;
      while (~scanf("%d", &n))
      {
            for (int i=0; i<n; i++)
                  scanf("%lf%lf", &p[i].x, &p[i].y);
            t = graham(n);
            area = 0;
            for (int i=1; i<t; i++)
                  area += Cross(res[0], res[i], res[i+1]);
            printf("%.f\n", area(Length/100));
      }
      return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值