[USACO5.1]圈奶牛Fencing the Cows —— 二维凸包

题目描述

传送门

解析

G r a h a m Graham Graham算法
PS:现已退役,就随便写写。
知识点:平面向量叉乘(顺时针旋转值为负,逆时针为正),单调栈维护
注意:按 ( x , y ) (x,y) (x,y)排序;第一个点,最有一个点必定在凸包上;以第一个点与最后一个点为界限,分两半分别求解

PS:貌似按极角排序的话,可以只扫一遍的样纸

代码

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>

#define IL inline

using namespace std; 

IL int read()
{
	int sum = 0;
	bool f = 0;
	char c = getchar();
	for(; '0' > c || c > '9'; c = getchar())
	if(c == '-') f = 1;
	for(; '0' <= c && c <= '9'; c = getchar())
		sum = sum * 10 + (c - '0');
	return f ? -sum : sum;
}

struct Point
{
	double x, y;
	IL Point(double x_ = 0, double y_ = 0) { x = x_; y = y_; }
	
	IL bool operator < (const Point &b) const
	{
		if(x == b.x) return y < b.y;
		return x < b.x;
	}	
};

Point num[10005];
int stk[10005], size;

IL double mul(int p1, int p2, int p3)
{
	return (num[p2].x - num[p1].x) * (num[p3].y - num[p2].y) - (num[p2].y - num[p1].y) * (num[p3].x - num[p2].x);
}

IL double dis(int p1, int p2)
{
	return sqrt((num[p1].x - num[p2].x) * (num[p1].x - num[p2].x) + (num[p1].y - num[p2].y) * (num[p1].y - num[p2].y));
}

int main()
{
	
	int n = read();
	for(int i = 1; i <= n; ++i)
		scanf("%lf %lf", &num[i].x, &num[i].y);
	sort(num + 1, num + n + 1);
	
	stk[++size] = 1; stk[++size] = 2;
	
	double ans = 0;
	
	for(int i = 3; i <= n; ++i)
	{
		for(; size > 1 && (mul(stk[size - 1], stk[size], i) <= 0);) --size;
		stk[++size] = i;
	}
		
	for(int i = 2; i <= size; ++i)
		ans += dis(stk[i], stk[i - 1]);
	
	size = 0;
	
	stk[++size] = 1; stk[++size] = 2;
	
	for(int i = 3; i <= n; ++i)
	{
		for(; size > 1 && (mul(stk[size - 1], stk[size], i) >= 0);) --size;
		stk[++size] = i;
	}
	
	for(int i = 2; i <= size; ++i)
		ans += dis(stk[i], stk[i - 1]);	
		
	printf("%.2lf\n", ans);
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值