首页 > 代码库 > POJ 3348 - Cows 凸包面积

POJ 3348 - Cows 凸包面积

求凸包面积。求结果后不用加绝对值,这是BBS()排序决定的。

//Ps 熟练了template <class T>之后用起来真心方便= =

//POJ 3348
//凸包面积
//1A 2016-10-15

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#define MAXN (10000 + 10)

struct point{
    int x, y;
    point(){}
    point(int X, int Y): x(X), y(Y){}
    friend int operator ^ (const point &p1, const point &p2){
        return p1.x * p2.y - p1.y * p2.x;
    }
    friend point operator >> (const point &p1, const point &p2){
        return point(p2.x - p1.x, p2.y - p1.y);
    }
    friend bool operator < (const point &p1, const point &p2){
        return (p1.x < p2.x)||(p1.x == p2.x)&&(p1.y < p2.y);
    }
}pt[MAXN];

template <class T>
void swap(T &a, T & b){
    T t = a;
    a = b;
    b = t;
}

template <class T>
void BBS(T a[], int n){
    for (int i = 0; i < n; i++)
        for (int j = 0; j < i; j++)
            if (a[i] < a[j]) swap(a[j], a[i]);
}

double convex_hull(point p[], int n){
    int cur = 0;
    double area = 0;
    BBS(p, n);
    while (1){
        int tmp = - 1;
        for (int i = 0; i < n; i++){
            if (cur != i){
                if (!(tmp + 1)||((p[cur] >> p[i]) ^ (p[cur] >> p[tmp])) > 0)
                    tmp = i;
            }
        }
        if (tmp + 1){
            area += p[cur] ^ p[tmp];
        }
        if (!tmp||!(tmp + 1)) return area / 2;
        cur = tmp;
    }
}

int main(){
    int n;
    freopen("fin.c", "r", stdin);
    scanf("%d", &n);
    for (int i = 0; i < n; i++){
        scanf("%d%d", &pt[i].x, &pt[i].y);
    }
    printf("%d\n", int(convex_hull(pt, n)/50));
}

 

POJ 3348 - Cows 凸包面积