bzoj2809 [Apio2012]dispatching——左偏树(可并堆)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2809
思路有点暴力和贪心,就是 dfs 枚举每个点作为管理者;
当然它的子树中派遣出去的忍者越多越好,只要不超过预算;
所以需要能够合并子树情况的、能反映最大值节点的数据结构,也就是左偏树(可并堆);
所以对于每个点维护一个大根左偏树,当子树内总和超过预算时,就去掉大根堆堆顶,这样最优;
自己的第一棵左偏树!没想到相当简单呢。
左偏树的论文:https://wenku.baidu.com/view/029c886d1eb91a37f1115ce5.html
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
int const maxn=1e5+;
int n,m,head[maxn],ct,mas;
ll ans;
struct N{
int to,next;
N(int t=,int n=):to(t),next(n) {}
}edge[maxn<<];
struct T{
int ls,rs,siz,val,led,fa,dis;
ll sum;
}t[maxn];
int rd()
{
int ret=;char ch=getchar();
while(ch<''||ch>'')ch=getchar();
while(ch>=''&&ch<='')ret=ret*+ch-'',ch=getchar();
return ret;
}
void add(int x,int y)
{
edge[++ct]=N(y,head[x]);head[x]=ct;
edge[++ct]=N(x,head[y]);head[y]=ct;
}
int merge(int x,int y)
{
if(!x)return y;
if(!y)return x;
if(t[x].val<t[y].val)swap(x,y);
t[x].rs=merge(t[x].rs,y);
int ls=t[x].ls,rs=t[x].rs;
t[x].sum=t[ls].sum+t[rs].sum+t[x].val;
t[x].siz=t[ls].siz+t[rs].siz+;
if(t[ls].dis<t[rs].dis)swap(t[x].ls,t[x].rs);//不是swap(ls,rs)
if(t[x].rs)t[x].dis=t[t[x].rs].dis+;//
else t[x].dis=;
return x;
}
int dfs(int x)
{
int rt=x;
for(int i=head[x];i;i=edge[i].next)
{
int u=edge[i].to;
if(u==t[x].fa)continue;
rt=merge(rt,dfs(u));
}
// while(t[x].sum>m)
// {
// t[x].sum-=t[x].val;
// t[x].siz--;
// x=merge(ls,rs);
// }
while(t[rt].sum>m)rt=merge(t[rt].ls,t[rt].rs);
ans=max(ans,(ll)t[rt].siz*t[x].led);//不是 t[rt].led ,因为是选 x 作为管理者
return rt;
}
int main()
{
n=rd();m=rd();
for(int i=;i<=n;i++)
{
t[i].fa=rd(); t[i].val=rd(); t[i].led=rd();
t[i].siz=; t[i].sum=t[i].val;
add(t[i].fa,i);
if(t[i].fa==)mas=i;
}
dfs(mas);
printf("%lld",ans);
return ;
}
bzoj2809 [Apio2012]dispatching——左偏树(可并堆)的更多相关文章
- bzoj2809 [Apio2012]dispatching(左偏树)
[Apio2012]dispatching Description 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Master.除了 M ...
- 【bzoj2809】[Apio2012]dispatching 左偏树
2016-05-31 15:56:57 题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2809 直观的思想是当领导力确定时,尽量选择薪水少的- ...
- [Apio2012]dispatching 左偏树
题目描述 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Master.除了 Master以外,每名忍者都有且仅有一个上级.为保密,同时增 ...
- [Apio2012]dispatching 左偏树做法
http://codevs.cn/problem/1763/ 维护子树大根堆,当子树薪水和>m时,删除最贵的点 #include<cstdio> #include<iostre ...
- [note]左偏树(可并堆)
左偏树(可并堆)https://www.luogu.org/problemnew/show/P3377 题目描述 一开始有N个小根堆,每个堆包含且仅包含一个数.接下来需要支持两种操作: 操作1: 1 ...
- APIO2012 派遣dispatching | 左偏树
题目链接:戳我 就是尽可能地选取排名小的,加起来就可以了.然后我们考虑利用一个大根堆,一个一个合并,如果超过派遣的钱,我们就把费用最大的那个忍者丢出队列. 左偏树,作为一个十分优秀的可并堆,我们这道题 ...
- BZOJ2809 dispatching(左偏树)
在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Master.除了 Master以外,每名忍者都有且仅有一个上级.为保密,同时增强忍者们的 ...
- [APIO2012]派遣 左偏树
P1552 [APIO2012]派遣 题面 考虑枚举每个节点作为管理者,计算所获得的满意程度以更新答案.对于每个节点的计算,贪心,维护一个大根堆,每次弹出薪水最大的人.这里注意,一旦一个人被弹出,那么 ...
- 洛谷P1552 [APIO2012] 派遣 [左偏树,树形DP]
题目传送门 忍者 Description 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Master.除了 Master以外,每名忍者都 ...
随机推荐
- POJ 1159 字符串匹配问题
题目大意: 问至少添加几个字符才能保证这个字符串是个回文串 一开始想也想不到字符串匹配上,因为是找回文串,我们可以把已给字符串逆向得到一个新的字符串,然后比较两者得到最大匹配长度,最后总长度减去最大匹 ...
- HDU 3609 二分图多重匹配
Escape Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Subm ...
- HDU 5695 Gym Class
拓扑排序. #include<cstdio> #include <iostream> #include<cstring> #include<cmath> ...
- poj——1274 The Perfect Stall
poj——1274 The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25709 A ...
- idea2019设置智能提示忽略大小写
2019的设置和2018的不太一样,话不多说,直接上干货.setting --> Editor --> General --> Code Completion 直接把这个选项前面的勾 ...
- vs npm设置淘宝npm
VS2017自带的npm会去国外的镜像下载文件, 奇慢无比, 还是马云家淘宝的镜像适合国内用户. 淘宝npm镜像地址: https://registry.npm.taobao.org VS中使用淘宝 ...
- Hadoop-08-Hive本地独立式安装
1.安装mysql sudo apt-get install mysql-server mysql-client 2.使用root账户登录mysql数据库,新建存放hive元数据的数据库.如果叫hiv ...
- WEKA简单介绍与资源汇总
简单介绍 Weka是一个开源的数据挖掘软件,里面集成了很多经典的机器学习算法,在高校和科研机构中受到了广泛的应用. 具体的简单介绍和简单的使用请參考文档:<使用Weka进行数据挖掘>. 学 ...
- webstorm 6.0 注册码
User Name: EMBRACE License Key: ===== LICENSE BEGIN ===== 24718-12042010 00001h6wzKLpfo3gmjJ8xoTPw ...
- 小胖说事35-----Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer posi
2011-06-11 15:19:17.167 ***[930:707] *** Terminating app due to uncaught exception 'CALayerInvalidGe ...