BZOJ1007水平可见直线 (凸包)

来源:互联网 发布:域名ip隐藏 编辑:程序博客网 时间:2024/05/18 02:24

Description

  在xoy直角坐标平面上有n条直线L1,L2,...Ln,若在y值为正无穷大处往下看,能见到Li的某个子线段,则称Li为可见的,否则Li为被覆盖的.

  例如,对于直线:L1:y=x; L2:y=-x; L3:y=0

  则L1和L2是可见的,L3是被覆盖的.

  给出n条直线,表示成y=Ax+B的形式(|A|,|B|<=500000),且n条直线两两不重合.求出所有可见的直线.

Input

  第一行为N(0 < N < 50000),接下来的N行输入Ai,Bi

Output

  从小到大输出可见直线的编号,两两中间用空格隔开,最后一个数字后面也必须有个空格

Sample Input

3
-1 0
1 0
0 0

Sample Output

1 2

HINT

Source

Solution

  按斜率从小到大给直线排序,维护一个下凸壳

  要把新加的线与凸壳的交点以右的直线删掉,因为新加的线一定在它与它之前的线组成的凸壳中

#include<iostream>#include<cstdio>#include<cmath>#include<cstdlib>#include<algorithm>#define eps 1e-8using namespace std;struct data{double a,b;int n;}l[50001];data st[50001];bool ans[50001];int top,n;inline bool cmp(data a,data b){if(fabs(a.a-b.a)<eps)return a.b<b.b;else return a.a<b.a;}double crossx(data x1,data x2){return (x2.b-x1.b)/(x1.a-x2.a);}void insert(data a){while(top){if(fabs(st[top].a-a.a)<eps)top--;else if(top>1&&crossx(a,st[top-1])<=crossx(st[top],st[top-1]))top--;else break;}st[++top]=a;}void work(){for(int i=1;i<=n;i++)insert(l[i]);    for(int i=1;i<=top;i++)ans[st[i].n]=1;    for(int i=1;i<=n;i++)       if(ans[i])printf("%d ",i);}int main(){scanf("%d",&n);for(int i=1;i<=n;i++){    scanf("%lf%lf",&l[i].a,&l[i].b);    l[i].n=i;    }    sort(l+1,l+n+1,cmp);    work();return 0;}