Given n straight lines, find out which lines can be seen from top down
Bare questions in semi-planialization
First, sort all straight lines by slope and import them to the stack in sequence.
If the intersection of a straight line and stack top is on the left of the intersection of the stack top line and the next line on the stack top, the stack top is deleted.
If the slope of multiple lines is the same, insert only the line with the maximum intercept.
Record the answer output.
#include<cmath>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define M 50500using namespace std;typedef pair<double,double> point;struct line{double k,b;int pos;bool operator < (const line &y) const{if( fabs(k-y.k) < 1e-7 )return b < y.b;return k < y.k;}}lines[M];int n,top;line *stack[M];bool ans[M];point Cross(const line &l1,const line &l2){double x=(l1.b-l2.b)/(l2.k-l1.k);double y=l1.k*x+l1.b;return point(x,y);}void Insert(line &l){while(top>1){point p1=Cross(l,*stack[top]);point p2=Cross(*stack[top],*stack[top-1]);if( p1.first-p2.first<1e-7 )stack[top--]=0x0;elsebreak;}stack[++top]=&l;}int main(){int i;cin>>n;for(i=1;i<=n;i++)scanf("%lf%lf",&lines[i].k,&lines[i].b),lines[i].pos=i;sort(lines+1,lines+n+1);for(i=1;i<=n;i++)if( i==n || fabs(lines[i].k-lines[i+1].k)>1e-7 )Insert(lines[i]);while(top)ans[stack[top--]->pos]=1;for(i=1;i<=n;i++)if(ans[i])printf("%d ",i);}
Bzoj 1007 hnoi2008 horizontal visibility straight half plane intersection