bzoj1007: [HNOI2008]水平可见直线
单调栈维护下凸包。
#include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> using namespace std; const double eps=1e-8; struct segment { double k,b;int id; }seg[510000],sta[510000];int top; bool cmp(segment sg1,segment sg2) { return (sg1.k!=sg2.k)?sg1.k<sg2.k:sg1.b>sg2.b; } bool cmd(segment sg1,segment sg2) { return sg1.id<sg2.id; } double jdx(segment sg1,segment sg2) { return (sg1.b-sg2.b)/(sg2.k-sg1.k); } int main() { int n; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%lf%lf",&seg[i].k,&seg[i].b), seg[i].id=i; sort(seg+1,seg+n+1,cmp); int tp=1; for(int i=2;i<=n;i++) if((seg[tp].k-seg[i].k)>eps)seg[++tp]=seg[i]; top=2; sta[1]=seg[1];sta[2]=seg[2]; for(int i=3;i<=n;i++) { while(top>1&&jdx(sta[top-1],sta[top])>=jdx(sta[top-1],seg[i]))top--; sta[++top]=seg[i]; } sort(sta+1,sta+top+1,cmd); for(int i=1;i<=top;i++)printf("%d ",sta[i].id); return 0; }
pain and happy in the cruel world.