【BZOJ3348】Cows【凸包】【凸包面积】

【题目链接】

题意:求凸包,求面积,然后给面积除以50,向下取整。

用的Andrew算法,即跑两次,先求出下凸包,然后再求出上凸包。

求面积时候应该用凸包数组求,结果写成了原来的点的数组,调了20多分钟。弱智+2

/* Telekinetic Forest Guard */
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

typedef double DB;

const int maxn = 10005;
const DB eps = 1e-15;

int n, m;

inline int dcmp(DB x) {
	if(fabs(x) < eps) return 0;
	return x < 0 ? -1 : 1;
}

struct Vector {
	DB x, y;

	Vector(DB a = 0.0, DB b = 0.0) {
		x = a; y = b;
	}

	Vector operator - (const Vector &A) const {
		return Vector(x - A.x, y - A.y);
	}

	bool operator < (const Vector &A) const {
		return fabs(x - A.x) > eps ? x < A.x : y < A.y;
	}

	bool operator == (const Vector &A) const {
		return dcmp(x - A.x) == 0 && dcmp(y - A.y) == 0;
	}
} p[maxn], cov[maxn];

typedef Vector Point;

inline DB Cross(Vector A, Vector B) {
	return A.x * B.y - A.y * B.x;
}

inline void Andrew() {
	sort(p + 1, p + 1 + n);
	int k; m = 0;
	k = 1;
	for(int i = 1; i <= n; i++) {
		for(; m > k && Cross(cov[m] - cov[m - 1], p[i] - cov[m - 1]) <= 0; m--);
		cov[++m] = p[i];
	}
	k = m;
	for(int i = n - 1; i; i--) {
		for(; m > k && Cross(cov[m] - cov[m - 1], p[i] - cov[m - 1]) <= 0; m--);
		cov[++m] = p[i];
	}
}

inline DB getArea() {
	DB res = 0.0;
	for(int i = 2; i <= m; i++) res += Cross(cov[i - 1] - cov[1], cov[i] - cov[1]) / 2.0;
	return res;
}

int main() {
	scanf("%d", &n);
	for(int i = 1; i <= n; i++) {
		DB x, y; scanf("%lf%lf", &x, &y);
		p[i] = Point(x, y);
	}
	
	Andrew();
	printf("%d\n", (int)(getArea() / 50));
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值