凸包P2742

#include <iostream>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <chrono>
#include <ctime>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdio>
#include <iomanip>


#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <iterator>
using namespace std;

#define _CRT_SECURE_NO_WARNINGS

//void rfIO()
//{
//	FILE *stream1;
//	freopen_s(&stream1,"in.txt", "r", stdin);
//	freopen_s(&stream1,"out.txt", "w", stdout);
//}

inline int read(int& x) {
	char ch = getchar();
	int f = 1; x = 0;
	while (ch > '9' || ch < '0') { if (ch == '-')f = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); }
	return x * f;
}
//void ReadFile() {
//	FILE* stream1;
//	freopen_s(&stream1,"in.txt", "r", stdin);
//	freopen_s(&stream1,"out.txt", "w", stdout);
//}

static auto speedup = []() {ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); return nullptr; }();

const int maxn = 1e5 + 7;

struct Point{
	double x, y;
	Point(){ x = y = 0; }
	Point(double _x, double _y) :x(_x), y(_y){}
	void input(){
		cin >> x >> y;
	}
	//点积 区分上下 cosx
	double dot(const Point &a) {
		return x + a.x + y + a.y;
	}
	//叉积 区分左右 sinx
	double cross(const Point &a){
		return x * a.y - y * a.x;
	}
	//距离
	double dis(const Point &a){
		return sqrt((x - a.x) * (x - a.x) + (y - a.y) * (y - a.y));
	}
	//距离的平方
	double dis2(const Point &a){
		return (x - a.x) * (x - a.x) + (y - a.y) * (y - a.y);
	}
}p[maxn],s[maxn];

double cross(const Point &a, const Point & b, const Point &c, const Point &d){
	Point t(a.x - b.x, a.y - b.y),t2(c.x - d.x,c.y - d.y);
	return t.cross(t2);
}

bool cmp(const Point &a, const Point &b){
	double c1 = cross(p[1], a, p[1], b);
	if (c1 > 0) return true;
	else if (c1 == 0)return p[1].dis2(a) < p[1].dis2(b);
	return false;
}

int main()
{
	int n,top = 1,idx = 1;
	cin >> n;
	
	for (int i = 1; i <= n; i++){
		p[i].input();
		if (p[idx].y > p[i].y) idx = i;
		else if (p[idx].y == p[i].y && p[idx].x > p[i].x)idx = i;
	}
	swap(p[1], p[idx]);
	sort(p + 2, p + n + 1, cmp);
	s[1] = p[1];
	for (int i = 2; i <= n; i++){
		while (top > 1 && cross(s[top - 1],s[top],s[top],p[i]) <= 0){
			top--;
		}
		s[++top] = p[i];
	}
	s[++top] = p[1];
	double ans = 0;
	for (int i = 1; i < top; i++){
		ans += s[i].dis(s[i + 1]);
	}
	cout << fixed << setprecision(2) << ans << endl;
	return 0;
}
### 凸包算法详解及实现方法 凸包(Convex Hull)是计算几何中的一个基础问题,其目标是在给定的二维点集中找出最小的凸多边形,使得所有点都在该多边形内部或边界上。凸包的顶点集合即为所求的“凸包点”。 #### 常见的凸包算法 1. **Graham扫描法** Graham扫描法是一种经典的凸包构造算法,时间复杂度为 $ O(n \log n) $。其基本步骤如下: - 选择一个基准点(通常是最左下角的点)。 - 将其余点按照相对于基准点的极角进行排序。 - 使用栈结构依次处理这些点,确保每一步添加的点保持凸性。 ```python import math def graham_scan(points): # 找到最左下角的点作为基准点 start_point = min(points, key=lambda p: (p[1], p[0])) # 按照极角排序 def polar_angle(p1, p2): return math.atan2(p2[1]-p1[1], p2[0]-p1[0]) sorted_points = sorted(points, key=lambda p: polar_angle(start_point, p)) # 初始化栈 hull = [start_point] for point in sorted_points: # 确保当前点在凸包内 while len(hull) >= 2 and cross_product(hull[-2], hull[-1], point) <= 0: hull.pop() hull.append(point) return hull def cross_product(o, a, b): return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]) ``` 2. **Andrew算法(上下凸包法)** Andrew算法通过将点集分为上下两部分分别构造凸包,最终合并得到完整的凸包。其时间复杂度同样为 $ O(n \log n) $,且无需计算极角,效率更高。 ```python def andrew_algorithm(points): points = sorted(points) if len(points) <= 2: return points lower = [] for p in reversed(points): while len(lower) >= 2 and cross_product(lower[-2], lower[-1], p) <= 0: lower.pop() lower.append(p) upper = [] for p in points: while len(upper) >= 2 and cross_product(upper[-2], upper[-1], p) <= 0: upper.pop() upper.append(p) # 合并上下凸包,去掉重复点 return upper[:-1] + lower[:-1] def cross_product(o, a, b): return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]) ``` 3. **分治法** 分治法将点集分成两个子集,分别计算它们的凸包,然后合并这两个凸包。这种方法适用于大规模数据集,但实现较为复杂。 4. **QuickHull算法** QuickHull是一种基于快速排序思想的凸包算法,通过递归划分点集并构建凸包。它的时间复杂度在平均情况下为 $ O(n \log n) $,但在最坏情况下可能退化为 $ O(n^2) $。 #### 应用场景 - **计算机图形学**:用于三维模型的简化和碰撞检测。 - **模式识别与图像处理**:用于形状分析和特征提取。 - **地理信息系统(GIS)**:用于区域划分和空间分析。 - **机器人路径规划**:用于障碍物包围区域的表示[^1]。 凸包算法在许多实际应用中扮演着重要角色,尤其是在需要高效处理大量空间数据的场景中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值