[bzoj1007] [HNOI2008]水平可见直线

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

Solution

用栈维护下凸壳即可。

#include<bits/stdc++.h>
using namespace std;

void read(int &x) {
    x=0;int f=1;char ch=getchar();
    for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
    for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
}

#define write(x) printf("%d\n",x)

const int maxn = 2e5+10;

struct line {int k,b,id;}l[maxn],sta[maxn];

int cmp1(line a,line b) {return a.k==b.k?a.b<b.b:a.k<b.k;}

int cmp2(line a,line b) {return a.id<b.id;}

double inter(line a,line b) {return (double)(a.b-b.b)/(b.k-a.k);}

int main() {
    int n;read(n);
    for(int i=1;i<=n;i++) read(l[i].k),read(l[i].b),l[i].id=i;
    sort(l+1,l+n+1,cmp1);int top=0;
    for(int i=1;i<=n;i++) {
        while(top) {
            if(sta[top].k==l[i].k) top--;
            else if(top>1&&inter(l[i],sta[top-1])<=inter(sta[top],sta[top-1])) top--;
            else break;	
        }sta[++top]=l[i];
    }
    sort(sta+1,sta+top+1,cmp2);
    for(int i=1;i<=top;i++) printf("%d ",sta[i].id);puts("");
    return 0;
}
posted @ 2019-02-03 12:54  Hyscere  阅读(90)  评论(0编辑  收藏  举报