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

1007: [HNOI2008]水平可见直线

Time Limit: 1 Sec  Memory Limit: 162 MB
Submit: 5932  Solved: 2254
[Submit][Status][Discuss]

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
 
 
 
【题解】
算法比较直观,先按斜率排序,再将最小的两条线入栈,然后依次处理每条线,如果其与栈顶元素的交点在上一个点的左边,则将栈顶元素出栈。
这样为什么对呢?因为对任意一个开口向上的半凸包,从左到右依次观察每条边和每个顶点,发现其斜率不断增大,顶点的横坐标也不断增大。
注意对斜率相同的直线的处理。
 
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<ctime>
 6 #include<algorithm>
 7 #include<cstdlib>
 8 using namespace std;
 9 #define eps 1e-8
10 struct node{double a,b;int id;}l[50010],st[50010];
11 int n,top,ans[50010];
12 inline int read()
13 {
14     int x=0,f=1;  char ch=getchar();
15     while(!isdigit(ch))  {if(ch=='-')  f=-1;  ch=getchar();}
16     while(isdigit(ch))  {x=x*10+ch-'0';  ch=getchar();}
17     return x*f;
18 }
19 bool cmp(node x,node y)//斜率相同则按纵截距排序,否则按斜率排序
20 {
21     if(fabs(x.a-y.a)<eps)  return x.b<y.b;
22     else return x.a<y.a;
23 }
24 double gets(node x,node y)  {return (y.b-x.b)/(x.a-y.a);}
25 void insert(node x)
26 {
27     while(top)
28     {
29         if(fabs(x.a-st[top].a)<eps)  top--;     //斜率相同将top弹出
30         else if(gets(x,st[top])<=gets(st[top],st[top-1])&&top>1)  top--;   //交点在左边将top弹出
31         else break;
32     }
33     st[++top]=x;
34 }
35 void work()
36 {
37     for(int i=1;i<=n;i++)  insert(l[i]);
38     for(int i=1;i<=top;i++)  ans[st[i].id]=1;
39     for(int i=1;i<=n;i++)  if(ans[i])  printf("%d ",i);
40 }
41 int main()
42 {
43     n=read();
44     for(int i=1;i<=n;i++)  {scanf("%lf%lf",&l[i].a,&l[i].b);  l[i].id=i;}
45     sort(l+1,l+n+1,cmp);
46     work();
47     return 0;
48 }
 
 
原文地址:https://www.cnblogs.com/chty/p/5852747.html