POJ 3348 Cows (凸包面积)

题目:传送门

题意:求凸包面积 / 50

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>
#define LL long long
#define mem(i, j) memset(i, j, sizeof(i))
#define rep(i, j, k) for(int i = j; i <= k; i++)
#define dep(i, j, k) for(int i = k; i >= j; i--)
#define pb push_back
#define make make_pair
#define INF INT_MAX
#define inf LLONG_MAX
#define PI acos(-1)
using namespace std;

const int N = 5e4 + 5;

struct Point {
    int x, y;
    Point(int x = 0, int y = 0) : x(x), y(y) { } /// 构造函数
};

typedef Point Vector;
/// 向量+向量=向量, 点+向量=向量
Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
///点-点=向量
Vector operator - (Point A, Point B) { return Vector(A.x - B.x, A.y - B.y); }
///向量*数=向量
Vector operator * (Vector A, int p) { return Vector(A.x * p, A.y * p); }
///向量/数=向量
Vector operator / (Vector A, int p) { return Vector(A.x / p, A.y / p); }

const int eps = 1e-10;
int dcmp(double x) {
    if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1;
}

bool operator < (const Point& a, const Point& b) {
    return a.x == b.x ? a.y < b.y : a.x < b.x;
}

bool operator == (const Point& a, const Point &b) {
    return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
}

int Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; } /// 点积
int Length(Vector A) { return sqrt(Dot(A, A)); } /// 计算向量长度
int Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); } /// 向量A、B夹角
int Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; } /// 叉积

bool Onseg(Point p, Point a1, Point a2) { /// 判断点 p 是否在线段 a1a2 上(含端点)
    return Cross(a1 - p, a2 - p) == 0 && Dot(a1 - p, a2 - p) <= 0;
}

Point P[N], Q[N];

int ConvexHull(Point* p, int n, Point* ch) { /// 求凸包
    sort(p, p + n);
    int m = 0;
    rep(i, 0, n - 1) {
        while(m > 1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--;
        ch[m++] = p[i];
    }
    int k = m;
    dep(i, 0, n - 2) {
        while(m > k && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--;
        ch[m++] = p[i];
    }
    if(n > 1) m--;
    return m;
}

void solve() {
    int n; scanf("%d", &n);
    rep(i, 0, n - 1) scanf("%d %d", &P[i].x, &P[i].y);

    if(n < 3) { puts("0"); return ; }

    int cnt = ConvexHull(P, n, Q);

    if(cnt < 3) { puts("0"); return ; }

    int ans = 0;

    rep(i, 1, cnt - 2) { /// 求面积
        ans += Cross(Q[i] - Q[0], Q[i + 1] - Q[0]);
    }

    printf("%d
", ans / 100); return ;
}

int main() {

    solve();

    return 0;
}
一步一步,永不停息
原文地址:https://www.cnblogs.com/Willems/p/12432993.html