BZOJ2809(Apio2012)[dispatching]--左偏树

【链接】
bzoj2809

【题目大意】
给你一个n表示忍者的个数,和m表示薪水的总预算。对于每个忍者i,给出BI表示忍者i的上级,Ci表示忍者i的薪水,Li表示忍者i的领导力。

【解题报告】
这题其实对于每个忍者和他的下级和下下级以及下下下级与下*?级,可以将他们的整体抽象成一棵树,忍者就是树上的节点。所以这题就是水水的左偏树了。

#include<cstdio>
#include<cstring>
#include<algorithm> 
#define LL long long
using namespace std;
const int maxn=100005;
int n,m,p[maxn];
LL ans,sum[maxn];
struct wjd
{
    int x,v,p,id;
    bool operator < (const wjd &a)const{
        return x<a.x;
    } 
}a[maxn];
struct Ztree
{
    Ztree* son[2];
    int x,dis;
}tem[maxn],*ro[maxn],*Null=tem,*len=Null;
inline int Read()
{
    int res=0;
    char ch=getchar();
    while (ch<'0'||ch>'9') ch=getchar();
    while (ch>='0'&&ch<='9') res=res*10+ch-48,ch=getchar();
    return res;
}
void New(Ztree* &k,int x)
{
    k=++len; k->x=x; k->dis=0; k->son[0]=k->son[1]=Null;
}
Ztree* Join(Ztree* a,Ztree* b)
{
    if (a==Null) return b; if (b==Null) return a;
    if (a->x<b->x) swap(a,b);
    a->son[1]=Join(a->son[1],b);
    if (a->son[0]->dis<a->son[1]->dis) swap(a->son[0],a->son[1]);
    if (a->son[1]==Null) a->dis=0; else a->dis=a->son[1]->dis+1;
    return a; 
}
Ztree* Delete(Ztree* a)
{
    return Join(a->son[0],a->son[1]);
}
int main()
{
    freopen("2809.in","r",stdin);
    freopen("2809.out","w",stdout);
    n=Read(); m=Read(); ans=0;
    for (int i=1; i<=n; i++) a[i]=(wjd){Read(),Read(),Read(),i};
    sort(a+1,a+1+n); a[0]=(wjd){0,0,0,0};
    for (int i=0,x; i<=n; i++) x=a[i].id,New(ro[x],a[i].v),sum[x]=a[i].v,p[x]=1;
    for (int i=n; i>=1; i--)
    {
        int x=a[i].id;
        while (sum[x]>m) sum[x]-=ro[x]->x,p[x]--,ro[x]=Delete(ro[x]);
        ans=max(ans,(LL)a[i].p*p[x]);
        ro[a[i].x]=Join(ro[a[i].x],ro[x]); 
        sum[a[i].x]+=sum[x]; p[a[i].x]+=p[x]; 
    }
    printf("%lld",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值