您的位置:首页 > 理论基础 > 数据结构算法

【左偏堆】bzoj2809派遣

2017-03-14 21:57 453 查看
【问题描述】

在一个忍者的帮派里,些们被选中遣给在一个忍者的帮派里,些们被选中遣给顾客,然后依据自己的工作顾客,然后依据自己的工作获取报偿。

在这个帮派里,有一名忍者被称之为MasterMaster。除了。除了MasterMaster以外,每名忍者以外,每名忍者都有且仅一个上级。为保密。为保密,同时增强,同时增强忍者们的领导力,所有与他们工作相关的指令总是由上级发送给他的直接下属,而不允许通过其他的方式发送。

现在你要招募一批忍者,并把它们派遣给顾客。你需要为每个被派遣的忍者支付一定的薪水,同时使得支付的薪水总额不超过你的预算。另外,为了发送指令,你需要选择一名忍者作为管理者,要求这个管理者可以向所有被派遣的忍者发送指令,在发送指令时,任何忍者(不管是否被派遣)都可以作为消息的传递人。管理者自己可以被派遣,也可以不被派遣。当然,如果管理者没有被派遣,你就不需要支付管理者的薪水。

你的目标是在预算内使顾客的满意度最大。这里定义顾客的满意度为派遣的忍者总数乘以管理者的领导力,其中每个忍者的领导力也是一定的。

写一个程序,给定每一个忍者i的上级Bi,薪水Ci,领导力Li,以及支付给忍者们的薪水总预算M,输出在预算内满足上述要求时顾客满意度的最大值。

【问题分析】

题目大意是给定一棵树,每个点有花费和点权,在不超过给定花费的前提下选择一颗子树内的若干点使得根节点点权与选择点的数量乘积最大。

思路:很明显的贪心问题,对于一颗子树内的点肯定是选择花费最小的若干个,所以可以对每一个节点维护一个大根堆表示子树内点的花费,然后更新堆,更新答案即可。

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N=100001;
struct zk {int to,next;}edge
;
struct zz {int key,lson,rson,deep;}heap
;
struct gg {int cost,root,able; long long sum,size;}tree
;
int n,m,cnt,cnt_heap; long long ans;
int first
;
inline int readin()
{
int x=0,f=1; char ch=getchar();
while(ch>'9'||ch<'0') {if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();}
return x*f;
}
void add(int a,int b)
{
edge[++cnt].to=b; edge[cnt].next=first[a];
first[a]=cnt; return;
}
void read()
{
int i,x;
n=readin(); m=readin();
for (i=1;i<=n;i++)
{
x=readin(); add(x,i);
tree[i].cost=readin();
tree[i].able=readin();
}
return;
}
int build(int k)
{
heap[++cnt_heap].key=k;
heap[cnt_heap].lson=heap[cnt_heap].rson=heap[cnt_heap].deep=0;
return cnt_heap;
}
int merge(int x,int y)
{
if (!x) return y;
if (!y) return x;
if (heap[x].key<heap[y].key)
swap(x,y);
heap[x].rson=merge(heap[x].rson,y);
if (heap[heap[x].lson].deep<heap[heap[x].rson].deep)
swap(heap[x].lson,heap[x].rson);
heap[x].deep=heap[heap[x].rson].deep+1;
return x;
}
void dfs(int k)
{
int i;
tree[k].root=build(tree[k].cost);
tree[k].sum=tree[k].cost; tree[k].size=1;
for (i=first[k];i;i=edge[i].next)
{
dfs(edge[i].to);
tree[k].sum+=tree[edge[i].to].sum;
tree[k].size+=tree[edge[i].to].size;
tree[k].root=merge(tree[k].root,tree[edge[i].to].root);
}
while(tree[k].sum>m)
{
tree[k].sum-=heap[tree[k].root].key;
tree[k].root=merge(heap[tree[k].root].lson,heap[tree[k].root].rson);
tree[k].size--;
}
ans=max(ans,tree[k].size*tree[k].able);
return;
}
int main()
{
read();
dfs(1);
printf("%lld\n",ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据结构 apio bzoj