poj 1113 凸包模板 (poj的远古c++居然不支持lambda)

本文详细介绍了一种用于计算凸包的经典算法——格拉姆扫描算法,并提供了完整的C++实现代码。通过该算法,可以有效地找出二维平面上一组点构成的凸包边界,适用于几何计算、计算机图形学等领域。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <vector>
#include <map>

#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define ll long long
#define ull unsigned long long
#define uint unsigned int
#define l(x) ((x)<<1)
#define r(x) ((x)<<1|1)
#define lowbit(x) ((x)&(-(x)))
#define ms(a,b) memset(a,b,sizeof(a))
#define NSYNC std::ios::sync_with_stdio(false);std::cin.tie(0);std::cout.tie(0);

using namespace std;

const double PI = acos(-1.0);
struct point {
	int x, y;
}list[1111];
int sta[1111], top;

int cross(point p0, point p1, point p2) {
	return (p1.x - p0.x)*(p2.y - p0.y) - (p1.y - p0.y)*(p2.x - p0.x);
}

double dis(point p1, point p2) {
	return sqrt((double)(p2.x - p1.x)*(p2.x - p1.x) + (p2.y - p1.y)*(p2.y - p1.y));
}

bool cmp(point p1, point p2) {
	int temp = cross(list[0], p1, p2);
	if (temp > 0) return true;
	return temp == 0 && dis(list[0], p1) < dis(list[0], p2);
}

bool tcmp(const point &p1, const point &p2) {
	if (p1.y == p2.y)return p1.x < p2.x;
	return p1.y < p2.y;
}

void init(int n) {
	for (int i = 0; i < n; i++)
		scanf("%d%d", &list[i].x, &list[i].y);
	/*sort(list, list + n, [](const point &p1,const point &p2) {
		if (p1.y == p2.y)return p1.x < p2.x;
		return p2.y < p2.y;
	});*/
	sort(list, list + n, tcmp);
	sort(list + 1, list + n, cmp);
}

void graham(int n) {
	if (n == 1) { top = 0; sta[0] = 0; }
	if (n == 2) {
		top = 1;
		sta[0] = 0;
		sta[1] = 1;
	}
	if (n > 2) {
		for (int i = 0; i <= 1; i++)
			sta[i] = i;
		top = 1;
		for (int i = 2; i < n; i++) {
			while (top > 0 && cross(list[sta[top - 1]], list[sta[top]], list[i]) <= 0)
				top--;
			top++;
			sta[top] = i;
		}
	}
}

int main() {
	int n, l;
	while (scanf("%d%d", &n, &l) != EOF) {
		init(n);
		graham(n);
		double res = 0;
		for (int i = 0; i < top; i++)
			res += dis(list[sta[i]], list[sta[i + 1]]);

		res += dis(list[sta[0]], list[sta[top]]);
		res += 2 * PI*l;

		printf("%d\n", (int)(res + 0.5));
	}

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值