SWERC2017 Blowing Candles 凸包+旋转卡壳(模板题)

本文介绍了一种通过计算凸包并使用旋转卡尺算法来确定生日蛋糕上蜡烛所形成的两条平行线间最短距离的方法。该方法利用了C++编程语言中的几何运算和数据结构,适用于解决类似的几何问题。

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

生日蛋糕上有蜡烛,问把蜡烛包括的两条平行线间的最短距离;

#include <iostream>
#include <string.h>
#include <algorithm> 
#include <math.h>
#include <vector>
using namespace std;
const int maxn=2e5+10;
struct point{
	double x, y;
    point(){}
    point(double x, double y) : x(x), y(y) {}
    friend point operator + (const point &a, const point &b){
        return point(a.x + b.x, a.y + b.y);
    }
    friend point operator - (const point &a, const point &b){
        return point(a.x - b.x, a.y - b.y);
    }
    friend point operator * (const point &a, const double &b){
        return point(a.x * b, a.y * b);
    }
    friend point operator / (const point &a, const double &b){
        return point(a.x / b, a.y / b);
    }
    friend bool operator == (const point &a, const point &b){
        return a.x == b.x && a.y == b.y;
    }
};
int r;
double dis(point a,point b)
{
	return sqrt((a.x-b.x)*(a.x-b.x)*1.0+(a.y-b.y)*(a.y-b.y));
}
double cross(point a,point b,point c) //计算叉乘 
{	
	return (c.x - a.x) * (b.y - a.y) - (b.x - a.x) * (c.y - a.y);	
} 
double  det(point a, point b)
{
    return a.x * b.y - a.y * b.x;
}
double dot(point a, point b)
{
    return a.x * b.x + a.y * b.y;
}
struct polygon_convex
{
    vector<point> p;
    polygon_convex(int size = 0){
        p.resize(size);
    }
};
bool comp_less(const point &a, const point &b)
{
    return a.x - b.x < 0 || a.x - b.x == 0 && a.y - b.y < 0;
}
polygon_convex convex_hull(vector<point> a)
{
    polygon_convex res(2 * a.size() + 5);
    sort(a.begin(), a.end(), comp_less);
    a.erase(unique(a.begin(), a.end()), a.end());
    int m = 0;
    for(int i = 0; i < a.size(); i ++){
        while(m > 1 && det(res.p[m - 1] - res.p[m - 2], a[i] - res.p[m - 2]) <= 0) -- m;
        res.p[m ++] = a[i];
    }
    int k = m;
    for(int i = int(a.size()) - 2; i >= 0; i --){
        while(m > k && det(res.p[m - 1] - res.p[m - 2], a[i] - res.p[m - 2]) <= 0) -- m;
        res.p[m ++] = a[i];
    }
    res.p.resize(m);
    if(a.size() > 1) res.p.resize(m - 1);
    return res;
}
double rotating_caliper(point *p,int n)
{
	 int j=1;
	 double minn=2.0*r;
	 p[n]=p[0];
	 for (int i=0;i<n;i++)
	 {
		 while (cross(p[i],p[i+1],p[j+1])-cross(p[i],p[i+1],p[j])<=0)
		 	j=(j+1)%n;
		//cout<<dis(p[i],p[i+1])<<"++++"<<endl;
		minn=min(minn,fabs(cross(p[i],p[i+1],p[j]))/dis(p[i],p[i+1]));
		//cout<<minn<<"^^^^^"<<endl;
	}
	return minn;
}
polygon_convex a;
point t,p[maxn];
int main()
{
	int nu,n;
	double s=0.0;
	scanf("%d%d",&nu,&r);
	for (int i=0;i<nu;i++)
	{
		scanf("%lf%lf",&t.x,&t.y);
		a.p.push_back(t);
	}
	a=convex_hull(a.p);
	n=a.p.size();
	for (int i=0;i<n;i++)
		p[i]=a.p[i];
//	for (int i=0;i<n;i++)
//		cout<<p[i].x<<"$$$$"<<p[i].y<<endl;
	if (n<=2)
		printf("0.00000000\n");
	else 
		printf("%.12lf\n",rotating_caliper(p,n));
	return 0;
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值