Bzoj 1007 hnoi2008 horizontal visibility straight half plane intersection

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.