POJ 3348 Cows

POJ_3348

    先将凸包找到,然后再用叉积算出多边形的面积之后除以50就是结果。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXD 10010
struct point
{
int x, y;
}p[MAXD], res[MAXD];
int N, P;
int cmp(const void *_p, const void *_q)
{
point *p = (point *)_p, *q = (point *)_q;
if(p->y == q->y)
return p->x - q->x;
return p->y - q->y;
}
int det(int x1, int y1, int x2, int y2)
{
return x1 * y2 - x2 * y1;
}
void init()
{
int i, j, k;
for(i = 0; i < N; i ++)
scanf("%d%d", &p[i].x, &p[i].y);
qsort(p, N, sizeof(p[0]), cmp);
}
int del(int top, int i)
{
if(det(res[top].x - res[top - 1].x, res[top].y - res[top - 1].y, p[i].x - res[top].x, p[i].y - res[top].y) < 0)
return 1;
return 0;
}
int graham()
{
int i, j, k, top = 1, mint;
res[0] = p[0], res[1] = p[1];
for(i = 2; i < N; i ++)
{
while(top && del(top, i))
-- top;
res[++ top] = p[i];
}
mint = top;
res[++ top] = p[N - 2];
for(i = N - 3; i >= 0; i --)
{
while(top != mint && del(top, i))
-- top;
res[++ top] = p[i];
}
return top;
}
void solve()
{
int i, j, k;
long long int ans = 0;
if(N <= 2)
printf("0\n");
else
{
P = graham();
for(i = 0; i < P; i ++)
ans += det(res[i].x, res[i].y, res[i + 1].x, res[i + 1].y);
ans = ans < 0 ? -ans : ans;
printf("%lld\n", ans / 100);
}
}
int main()
{
while(scanf("%d", &N) == 1)
{
init();
solve();
}
return 0;
}


你可能感兴趣的:(poj)