AC日记——dispatching bzoj 2809

2809: [Apio2012]dispatching

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 3290  Solved: 1740
[Submit][Status][Discuss]

Description

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


 

1  ≤N ≤ 100,000 忍者的个数;
1  ≤M ≤ 1,000,000,000 薪水总预算; 
 
0  ≤Bi < i  忍者的上级的编号;
1  ≤Ci ≤ M                     忍者的薪水;
1  ≤Li ≤ 1,000,000,000             忍者的领导力水平。
 
 

Input

从标准输入读入数据。
 
第一行包含两个整数 N M,其中 N表示忍者的个数,M表示薪水的总预算。
 
接下来 N行描述忍者们的上级、薪水以及领导力。其中的第 i 行包含三个整 Bi , C i , L i分别表示第i个忍者的上级,薪水以及领导力。Master满足B i = 0并且每一个忍者的老板的编号一定小于自己的编号 Bi < i


 

 

Output

输出一个数,表示在预算内顾客的满意度的最大值。
 
 

Sample Input


5 4
0 3 3
1 3 5
2 2 2
1 2 4
2 3 1

Sample Output

6

HINT



如果我们选择编号为 1的忍者作为管理者并且派遣第三个和第四个忍者,薪水总和为 4,没有超过总预算                         4。因为派遣了                              2   个忍者并且管理者的领导力为      3,

用户的满意度为 2      ,是可以得到的用户满意度的最大值。

Source

思路:

  这题不仅仅是恶心,,简直是个,,,唉,不说了。。

  主席树+dfs序;

  最后输出的ans是每个忍者可以派遣的忍者的个数*这个忍者的领导力的最大值;

  我们思考主席树;

  如何用主席树呢?

  首先用dfs序跋整个树便利,记录每个点的开始的标号和最后的标号;

  然后,我们开始往主席树里加点;

  主席树的叶节点是排序离散后的薪水;

  主席树维护两个元素,忍者的数量和花费;

  然后,我们每次查询就可以了;

  细节,,,

  要用long long;

  查询到末节点的时候,返回值是min(当前预算/当前单个忍者花费,当前忍者数量),不然40分;

来,上代码:

#include <cstdio>
#include <iostream>
#include <algorithm>

#define LL long long 
#define maxn 100005

using namespace std;
 
struct TreeNodeType {
    LL l,r,dis,sum;
};
struct TreeNodeType tree[maxn*25];
 
struct EdgeType {
    LL to,next;
};
struct EdgeType edge[maxn<<1];
 
LL if_z,n,m,head[maxn],cost[maxn],lead[maxn];
LL cnt,hash[maxn],root[maxn],tot,start[maxn],end[maxn];
LL ans=0,size;
 
char Cget;
 
inline void read_int(LL &now)
{
    if_z=1,Cget=getchar(),now=0;
    while(Cget>'9'||Cget<'0')
    {
        if(Cget=='-') if_z=-1;
        Cget=getchar();
    }
    while(Cget>='0'&&Cget<='9')
    {
        now=now*10+Cget-'0';
        Cget=getchar();
    }
    now*=if_z;
}
 
inline void edge_add(LL from,LL to)
{
    cnt++;
    edge[cnt].to=to;
    edge[cnt].next=head[from];
    head[from]=cnt;
}
 
void tree_build(LL now,LL l,LL r)
{
    if(l==r) return ;
    LL mid=(l+r)>>1;
    tree[now].l=++tot;
    tree_build(tree[now].l,l,mid);
    tree[now].r=++tot;
    tree_build(tree[now].r,mid+1,r);
}
 
void tree_add(LL pre,LL now,LL pos,LL l,LL r)
{
    tree[now].dis=tree[pre].dis+1;
    tree[now].sum=tree[pre].sum+hash[pos];
    if(l==r) return ;
    LL mid=(l+r)>>1;
    if(pos<=mid)
    {
        tree[now].l=++tot;
        tree_add(tree[pre].l,tree[now].l,pos,l,mid);
        tree[now].r=tree[pre].r;
    }
    else
    {
        tree[now].r=++tot;
        tree_add(tree[pre].r,tree[now].r,pos,mid+1,r);
        tree[now].l=tree[pre].l;
    }
}
 
void Search(LL now,LL fa)
{
    start[now]=cnt++,root[cnt]=++tot;
    LL pos=lower_bound(hash+1,hash+size+1,cost[now])-hash;
    tree_add(root[cnt-1],root[cnt],pos,1,size);
    for(LL i=head[now];i;i=edge[i].next)
    {
        if(edge[i].to==fa) continue;
        Search(edge[i].to,now);
    }
    end[now]=cnt;
}
 
LL tree_query(LL pre,LL now,LL pos,LL l,LL r)
{
    if(l==r) return min(pos/hash[l],tree[now].dis-tree[pre].dis);
    LL pos_=tree[tree[now].l].dis-tree[tree[pre].l].dis;
    LL mid=(l+r)>>1,dis_=tree[tree[now].l].sum-tree[tree[pre].l].sum;
    if(pos<=dis_) return tree_query(tree[pre].l,tree[now].l,pos,l,mid);
    else return tree_query(tree[pre].r,tree[now].r,pos-dis_,mid+1,r)+pos_;
}
 
int main()
{
    read_int(n),read_int(m);
    LL bi,master;
    for(LL i=1;i<=n;i++)
    {
        read_int(bi),read_int(cost[i]),read_int(lead[i]);
        edge_add(bi,i),edge_add(i,bi),hash[i]=cost[i];
        if(bi==0) master=i;
    }
    sort(hash+1,hash+n+1);
    size=unique(hash+1,hash+n+1)-hash-1;
    root[0]=++tot;
    tree_build(root[0],1,size);
    cnt=0,Search(master,0);
    for(LL i=1;i<=n;i++)
    {
        LL pos=tree_query(root[start[i]],root[end[i]],m,1,size);
        ans=max(ans,lead[i]*pos);
    }
    cout<<ans<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/IUUUUUUUskyyy/p/6411407.html