[BZOJ 1007] [HNOI2008]水平可见直线

http://www.lydsy.com/JudgeOnline/problem.php?id=1007

题意:略

思路:容易想到最后的可见直线会围成个开口向上的半个凸包,所以我们可以先把直线汗斜率排序(凸包的边从左到右斜率递增),再将斜率最小的两条线入栈,然后依次处理每条线,如果其与栈顶元素的交点在上一个点的左边(凸包顶点横坐标递增),则将栈顶元素出栈 。

代码:

#include <cmath>
#include <stack>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const double eps = 1e-8;

const int maxn = 50005;

struct Point{
    Point(double x = 0, double y = 0){
        this->x = x;
        this->y = y;
    }
    double x, y;
};

struct Line{
    int id;
    double a, b;
};

int n;
Line line[maxn];

int sta[maxn], top;

bool cmp1(const Line a, const Line b){
    if(fabs(a.a - b.a) < eps)
        return a.b < b.b;
    return a.a < b.a;
}

bool cmp2(const int a, const int b){
    return line[a].id < line[b].id;
}

Point GetPoint(Line a, Line b)
{
    Point res;
    res.x = (b.b - a.b) / (a.a - b.a);
    res.y = res.x * a.a + a.b;
    return res;
}

void Insert(int pos)
{
    while(top){
        if(fabs(line[sta[top]].a - line[pos].a) < eps){
            top--;
        }else if(top > 1 && GetPoint(line[sta[top - 1]], line[pos]).x <= GetPoint(line[sta[top - 1]], line[sta[top]]).x){
            top--;
        }else{
            break;
        }
    }
    sta[++top] = pos;
}

int main()
{
    while(~scanf("%d", &n)){
        for(int i = 0; i < n; i++){
            line[i].id = i + 1;
            scanf("%lf%lf", &line[i].a, &line[i].b);
        }
        sort(line, line + n, cmp1);
        top = 0;
        for(int i = 0; i < n; i++){
            Insert(i);
        }
        sort(sta + 1, sta + top + 1, cmp2);
        for(int i = 1; i <= top; i++){
            printf("%d ", line[sta[i]].id);
        }
        printf("\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

achonor

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值