2012金华现场赛 E - IT Companies [HDU - 4446]

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/z591826160/article/details/83267859

E - IT Companies [HDU - 4446]

题面

在这里插入图片描述

思路

考虑 min ( k c [ k ] = = 0 ) \min(k|c[k]==0) 的情况,必然它是目前序列里最大的公司,否则在 [ 1 , k 1 ] [1,k-1] 中必然有 c [ i ] = 0 c[i]=0 ,因此只需将 k k 放入维护的从大到小顺序的答案队列中,更新 [ 1 , k 1 ] [1,k-1] c [ i ] c[i] 的值
考虑 m i n ( c [ i ] ) > 0 min(c[i])>0 ,说明最大值不在公司中,而在branch中,而在 m i n ( c [ i ] ) = 0 ) min(c[i])=0) 的操作下,越先出现的branch其值一定越大,因此还需维护一个branch的从大到小顺序的答案队列,并跟新 [ k + 1 , n ] [k+1,n] 的值
而出现 m i n ( c [ i ] ) < 0 min(c[i])<0 的情况或者在2条件下branch队列为空即为不可能情况
当两个队列大小为2*n时结束

代码

const int maxn = 5e5+7;
const int inf  = 0x3f3f3f3f;
const int mod  = 1e9 + 7;
const int none = -1;
const LL linf  = (LL)inf << 32 | inf;

int tree[maxn],num[maxn],lazy[maxn];
queue<int>ans,bran;
stack<int>stk;
void up(int rt){
    if(tree[rt<<1]<=tree[rt<<1|1]){
        tree[rt]=tree[rt<<1];
        num[rt]=num[rt<<1];
    } else{
        tree[rt]=tree[rt<<1|1];
        num[rt]=num[rt<<1|1];
    }
}
void down(int rt){
    if(lazy[rt]!=0){
        lazy[rt<<1]+=lazy[rt];
        lazy[rt<<1|1]+=lazy[rt];
        tree[rt<<1]+=lazy[rt];
        tree[rt<<1|1]+=lazy[rt];
    }
    lazy[rt]=0;
}
void build(int l,int r,int rt){
    if(l==r){
        rd(tree[rt]);
        num[rt]=l;
        return ;
    }
    __m;
    build(lson);
    build(rson);
    up(rt);
}
void update(int l,int r,int rt,int L,int R,int add){
    if(L>R)return;
    if(l>=L&&r<=R){
        tree[rt]+=add;
        lazy[rt]+=add;
        return ;
    }
    down(rt);
    __m;
    if(mid>=L)update(lson,L,R,add);
    if(mid<R)update(rson,L,R,add);
    up(rt);
}
int main(){
    int n;
    while(rd(n),n){
        clr(lazy);
        while(!ans.empty())ans.pop();
        while(!bran.empty())bran.pop();
        while(!stk.empty())stk.pop();
        build(1,n,1);
        bool ok=true;
        while(ans.size()+bran.size()<2*n){
            if(tree[1]==0){
                ans.push(num[1]);
                bran.push(-num[1]);
                int now=num[1];
                update(1,n,1,1,now-1,-1);
                update(1,n,1,now,now,inf);
            } else if(tree[1]>0){
                if(bran.empty()){
                    ok=false;
                    break;
                }
                int now=bran.front();
                bran.pop();
                ans.push(now);
                update(1,n,1,-now+1,n,-1);
            } else {
                ok=false;
                break;
            }
        }
        if(!ok){
            puts("Impossible");
            continue;
        } 
        while(!ans.empty()){
            stk.push(ans.front());
            ans.pop();
        }
        while(!bran.empty()){
            stk.push(bran.front());
            bran.pop();
        }
        while(!stk.empty()){
            printf("%d ",stk.top());stk.pop();
        }
        puts("");
    }
}

猜你喜欢

转载自blog.csdn.net/z591826160/article/details/83267859