P3194 [HNOI2008]水平可见直线

传送门

我们把所有的直线按斜率从小到大排序,然后用单调栈维护
发现,如果当前直线与\(st[top-1]\)直线的交点的横坐标大于等于与\(st[top]\)的交点的横坐标,当前直线可以覆盖掉\(st[top]\)
这个可以画图理解一下

//minamoto
#include<bits/stdc++.h>
#define rint register int
using namespace std;
#define getc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[1<<21],*p1=buf,*p2=buf;
int read(){
    int res,f=1;char ch;
    while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
    for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
    return res*f;
}
const int N=50005;
struct node{
	int a,b,id;
	inline bool operator <(const node &q)const{return a==q.a?b>q.b:a<q.a;}
}p[N];int n,st[N],top;
inline double solve(int i,int j){return (double)(p[i].b-p[j].b)/(p[j].a-p[i].a);}
int main(){
//	freopen("testdata.in","r",stdin);
	n=read();for(rint i=1;i<=n;++i)p[i].a=read(),p[i].b=read(),p[i].id=i;
	sort(p+1,p+1+n),st[top=0]=1;
	for(rint i=2;i<=n;++i){
		if(p[i].a==p[i-1].a)continue;
		while(top&&solve(st[top],i)<=solve(st[top-1],i))--top;
		st[++top]=i;
	}for(rint i=0;i<=top;++i)st[i]=p[st[i]].id;
	sort(st,st+1+top);for(rint i=0;i<=top;++i)printf("%d ",st[i]);return 0;
}
posted @ 2018-11-20 16:06  bztMinamoto  阅读(201)  评论(0编辑  收藏  举报
Live2D