BZOJ1007【HNOI2008】水平可见直线

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1007


【分析】

先按直线的斜率排好序。斜率相同的直线只有截距最大的那个可见。

然后记录一个栈,以及一个数组D[i]记录位于栈中的第i条直线与栈中第i-1条直线交点的横坐标。(以斜率从小到大排序为例,那么栈中第i条直线可见部分仅在x>D[i]处)

如果当前加入的直线与栈顶直线的交点横坐标小于D[top](即当前加入直线覆盖了栈顶直线的可见部分),则栈顶直线不可见,如此依次弹出栈顶直线,再加入当前直线。


【代码】

#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn=50010;
const double eps=1e-10;
 
struct Line
{
    double a,b;
    int ID;
    bool operator < (const Line &p) const {return a<p.a || (abs(a-p.a)<eps && b>p.b);}
}L[maxn];
typedef Line Node;
double GetX(Line x,Line y) {return (y.b-x.b)/(x.a-y.a);}
 
int Q[maxn],top;
int I[maxn];
double D[maxn];
int n;
 
int main()
{
    scanf("%d",&n);
    for (int i=0;i<n;i++) scanf("%lf%lf",&L[i].a,&L[i].b),L[i].ID=i+1;
    sort(L,L+n);
    Q[top]=0;D[top]=-1e80;
    for (int i=1;i<n;i++)
    {
        if (abs(L[i].a-L[Q[top]].a)<eps) continue;
        D[++top]=GetX(L[i],L[Q[top-1]]);
        while (D[top]-D[top-1]<eps) D[--top]=GetX(L[i],L[Q[top-1]]);
        Q[top]=i;
    }
    for (int i=0;i<=top;i++) I[L[Q[i]].ID]=1;
    for (int i=1;i<=n;i++) if (I[i]) printf("%d ",i);
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值