[bzoj1007]水平可见直线

本文介绍了一种使用单调栈实现的凸包算法,通过将直线按斜率排序并维护交点递增的特性,有效地解决了计算凸包的问题。算法首先对直线按斜率和截距排序,然后利用单调栈维护从左到右的直线交点递增序列。

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

注意到图形一定是下凸的,使k为第一关键字,b为第二关键字对直线进行排序,考虑从左到右直线的交点一定是递增的, 单调栈维护

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>

using namespace std;
const int N = 100010;
const double eps = 1e-9;
const double inf = 1e18;
inline int dcmp(double x){
    if (fabs(x) <= eps) return 0;
    return x < 0 ? -1 : 1;
}
int n;
struct line{
    double k, b;
    int id;
    bool operator < (const line &t) const {
        return k == t.k ? b < t.b : k < t.k;
    }
}l[N];
struct node{
    int id;
    double x;
    bool operator < (const node &t) const {
        return l[id].id < l[t.id].id;
    }
}st[N];
double cross(const line &a, const line &b){
    return a.k == b.k ? 0 : (b.b - a.b) / (a.k - b.k);
}

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i)scanf("%lf%lf",&l[i].k, &l[i].b), l[i].id = i;
    sort(l + 1, l + n + 1);
    int tp = 0;
    for (int i = 1; i <= n; ++i){
        while(tp && (l[i].k == l[st[tp].id].k || cross(l[i], l[st[tp].id]) <= st[tp].x)) tp--;
        if (tp == 0) st[tp + 1] = (node) {i, -inf};
            else st[tp + 1] = (node){i, cross(l[st[tp].id], l[i])};
        tp++;
    }
    sort(st + 1, st + tp + 1);
    for (int i = 1; i < tp; ++i)printf("%d ", l[st[i].id].id);
    printf("%d", l[st[tp].id].id);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值