bzoj1007: [HNOI2008]水平可见直线 单调栈维护凸壳

在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条直线两两不重合.求出所有可见的直线.

很明显最后的结果应该是一个斜率递增的结果,那么我们先按斜率排序,然后用单调栈维护,如果要加入的线i和last-1的交点在i和last的左侧,就证明last这条线已经完全被覆盖了,那么从栈中删除,直接维护下去就得到 了结果,注意一下斜率相同的情况

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define cd complex<double>
#define ull unsigned long long
#define base 1000000000000000000
#define fio ios::sync_with_stdio(false);cin.tie(0)

using namespace std;

const double g=10.0,eps=1e-12;
const int N=500000+10,maxn=100000+10,inf=0x3f3f3f3f,INF=0x3f3f3f3f3f3f3f3f;

struct line{
    double k,b;
    int id;
    bool operator<(const line &rhs)const{
        if(k!=rhs.k)return k<rhs.k;
        return b<rhs.b;
    }
}l[N];
bool cmp(int a,int b)
{
    return l[a].id<l[b].id;
}
int q[N];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%lf%lf",&l[i].k,&l[i].b);
        l[i].id=i+1;
    }
    sort(l,l+n);
//    for(int i=0;i<n;i++)printf("%f %f\n",l[i].k,l[i].b);
    int head=1,last=1;q[head]=0;
    for(int i=1;i<n;i++)
    {
        if(head<=last&&l[q[last]].k==l[i].k)last--;
        while(head<last)
        {
            double x=(l[i].b-l[q[last-1]].b)/(l[q[last-1]].k-l[i].k);
            double y=l[i].k*x+l[i].b;
            double x1=(l[q[last]].b-l[q[last-1]].b)/(l[q[last-1]].k-l[q[last]].k);
            double y1=l[q[last]].k*x+l[q[last]].b;
//            printf("%f %f %f %f\n",l[i].k,l[i].b,l[q[last-1]].k,l[q[last-1]].b);
            if(x<=x1)last--;
            else break;
        }
        q[++last]=i;
//        for(int j=head;j<=last;j++)printf("%d ",q[j]);
//        puts("+++");
    }
    sort(q+head,q+last+1,cmp);
    for(int i=head;i<=last;i++)printf("%d ",l[q[i]].id);
    return 0;
}
/********************
7
-1 0
1 0
0 0
0 -1
0 -2
-1 -1
1 -1
********************/
View Code

 

posted @ 2018-04-13 21:49  walfy  阅读(155)  评论(0编辑  收藏  举报