POJ 3348 Cows (计算凸包面积)

 
#include<iostream>
#include <stdio.h>
#include<cmath>
#include<algorithm>
#include<vector>
#include<string>
#include<sstream>
#include<queue>
#define min(a,b) (((a)<(b))?(a):(b))
#define max(a,b) (((a)>(b))?(a):(b))
#define multi(a, b, c) ((b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x))
#define sqr(a) ((a) * (a))
#define dis2(a, b) sqrt(sqr(a.x - b.x) + sqr(a.y - b.y))

const double pi=acos(-1.0);
double r;
struct point
{
    int x, y;
    int operator<(point &b)
    {
        return ((y < b.y) || (y == b.y && x < b.x));
    }
}ptmp;

int cmp(const void *a, const void *b)
{
    point *c = (point *)a;
    point *d = (point *)b;
    point e, f;
    e = *c;
    f = *d;
    int t = multi(ptmp, e, f);
    if(t > 0) return -1;
    if(t == 0 && dis2(ptmp, e) <= dis2(ptmp, f)) return -1;
    return 1;
}
void GrahamScanGram(point p[], int stack[], int &top, int n)
{
    int u = 0, i;
    for(int i = 0;i < n;i++)
        if(p[i] < p[u]) u = i;
    ptmp = p[0], p[0] = p[u], p[u] = ptmp;
    ptmp = p[0];
    qsort(p + 1, n - 1, sizeof(p[0]), cmp);
    stack[0] = 0, stack[1] = 1, stack[2] = 2;
    top = 3;
    for(i = 3;i < n;i++)	
    {
        while(top >= 2 && 
            multi(p[stack[top - 2]], p[stack[top - 1]], p[i]) <= 0) 
        top--;
	stack[top++] = i;	
    }
}
int main()
{
    point p[100005];
    int stack[100005], top;
    int n,  i;
    
    while(scanf("%d", &n)!=EOF)
    {
        for(i = 0;i < n;i++)
        scanf("%d%d", &p[i].x, &p[i].y);
        GrahamScanGram(p, stack, top, n);
        double area = 0;
        for(i = 1;i < top - 1;i++)
            area += fabs(multi(p[stack[0]], p[stack[i]], p[stack[i + 1]]));
            int ans=(int)area/100;
        printf("%d\n", ans);
    }
    return 0;	
}

你可能感兴趣的:(POJ 3348 Cows (计算凸包面积))