BZOJ1007【HNOI2008】水平可见直线

来源:互联网 发布:淘宝店铺索引在哪里 编辑:程序博客网 时间:2024/05/17 01:37

题目链接: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 0