首页 > 代码库 > [BZOJ 2809][Apio2012]dispatching(左偏树)

[BZOJ 2809][Apio2012]dispatching(左偏树)

Description

在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿。在这个帮派里,有一名忍者被称之为 Master。除了 Master以外,每名忍者都有且仅有一个上级。为保密,同时增强忍者们的领导力,所有与他们工作相关的指令总是由上级发送给他的直接下属,而不允许通过其他的方式发送。现在你要招募一批忍者,并把它们派遣给顾客。你需要为每个被派遣的忍者 支付一定的薪水,同时使得支付的薪水总额不超过你的预算。另外,为了发送指令,你需要选择一名忍者作为管理者,要求这个管理者可以向所有被派遣的忍者 发送指令,在发送指令时,任何忍者(不管是否被派遣)都可以作为消息的传递 人。管理者自己可以被派遣,也可以不被派遣。当然,如果管理者没有被排遣,就不需要支付管理者的薪水。你的目标是在预算内使顾客的满意度最大。这里定义顾客的满意度为派遣的忍者总数乘以管理者的领导力水平,其中每个忍者的领导力水平也是一定的。写一个程序,给定每一个忍者 i的上级 Bi,薪水Ci,领导力L i,以及支付给忍者们的薪水总预算 M,输出在预算内满足上述要求时顾客满意度的最大值。

Solution

可并堆

枚举管理者,所能排遣的忍者在它的子树中,如果薪水超出预算就把最大的元素弹出

不断向上合并

#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#define MAXN 100005typedef long long LL;using namespace std;int n,m,b[MAXN],c[MAXN],l[MAXN],tot=0,root[MAXN];LL ans=0;struct Node{    int lch,rch,w,d,sz,sum;}heap[MAXN];void update(int x){    heap[x].d=heap[heap[x].rch].d+1;    heap[x].sz=heap[heap[x].lch].sz+heap[heap[x].rch].sz+1;    heap[x].sum=heap[heap[x].lch].sum+heap[heap[x].rch].sum+heap[x].w;}int merge(int x,int y){    if(!x||!y)return x+y;    if(heap[x].w<heap[y].w)swap(x,y);    heap[x].rch=merge(heap[x].rch,y);    if(heap[heap[x].lch].d<heap[heap[x].rch].d)swap(heap[x].lch,heap[x].rch);    update(x);    return x;}int insert(int w){    heap[++tot].d=0,heap[tot].sz=1;    heap[tot].lch=heap[tot].rch=0;    heap[tot].sum=heap[tot].w=w;    return tot;}void pop(int &x){    x=merge(heap[x].lch,heap[x].rch);}int main(){    scanf("%d%d",&n,&m);    for(int i=1;i<=n;i++)    {        scanf("%d%d%d",&b[i],&c[i],&l[i]);        root[i]=insert(c[i]);        while(heap[root[i]].sum>m)pop(root[i]);    }    for(int i=n;i>0;i--)    {        ans=max(ans,1LL*heap[root[i]].sz*l[i]);        root[b[i]]=merge(root[i],root[b[i]]);        while(heap[root[b[i]]].sum>m)pop(root[b[i]]);    }    printf("%lld",ans);    return 0;}

 

[BZOJ 2809][Apio2012]dispatching(左偏树)