bzoj1007 [HNOI2008]水平可见直线——单调栈

时间:2023-03-09 05:36:51
bzoj1007 [HNOI2008]水平可见直线——单调栈

题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1007

可以把直线按斜率从小到大排序,用单调栈维护,判断新直线与栈顶的交点和栈顶与它之前直线的交点的位置关系即可。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int const maxn=;
int n,top,ans[maxn];
struct N{
double a,b;
int bh;
}p[maxn],sta[maxn];
bool vis[];
bool cmp(N x,N y)
{
if(x.a!=y.a)return x.a<y.a;
else return x.b>y.b;
}
bool pd(N a)
{
N b=sta[top],c=sta[top-];
double x1=(c.b-b.b)/(b.a-c.a);
double x2=(c.b-a.b)/(a.a-c.a);
if(x2<=x1)return ;
else return ;
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%lf%lf",&p[i].a,&p[i].b),p[i].bh=i;
sort(p+,p+n+,cmp);
for(int i=;i<=n;i++)
{
if(p[i].a==p[i-].a)continue;
while(top>&&pd(p[i]))top--;
sta[++top]=p[i];ans[top]=p[i].bh;
}
sort(ans+,ans+top+);
for(int i=;i<=top;i++)
printf("%d ",ans[i]);
return ;
}