浅谈左偏树:https://www.cnblogs.com/AKMer/p/10246635.html

题目传送门:https://lydsy.com/JudgeOnline/problem.php?id=2809

对于每个子树维护一棵满足大根堆性质的左偏树,可以通过当前节点和子树的左偏树合并得来,如果总权值超过\(m\)就不断\(pop\)堆顶,每个点只会被\(pop\)一次。满足大根堆性质是贪心,权值小的忍者越多我就可以雇佣越多人。然后用这颗子树的根的领导能力乘以左偏树结点个数来更新答案就行了。

时间复杂度:\(O(nlogn)\)

空间复杂度:\(O(n)\)

代码如下:

#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll; const int maxn=1e5+5; ll ans;
int n,m,tot;
int c[maxn],l[maxn];
int now[maxn],pre[maxn],to[maxn];
int son[maxn][2],dist[maxn],siz[maxn],val[maxn]; int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
} void add(int a,int b) {
pre[++tot]=now[a];
now[a]=tot,to[tot]=b;
} void update(int u) {
siz[u]=siz[son[u][0]]+1+siz[son[u][1]];
val[u]=val[son[u][0]]+c[u]+val[son[u][1]];
dist[u]=dist[son[u][1]]+1;
} int merge(int a,int b) {
if(!a||!b)return a+b;
if(c[a]<c[b])swap(a,b);
son[a][1]=merge(son[a][1],b);
if(dist[son[a][1]]>dist[son[a][0]])
swap(son[a][1],son[a][0]);
update(a);return a;
} int pop(int u) {
int tmp=merge(son[u][1],son[u][0]);
son[u][0]=son[u][1];
return tmp;
} int dfs(int fa,int u) {
int tmp=u;siz[u]=1,val[u]=c[u];
for(int p=now[u],v=to[p];p;p=pre[p],v=to[p]) {
tmp=merge(tmp,dfs(u,v));
while(val[tmp]>m)tmp=pop(tmp);
}
ans=max(ans,1ll*siz[tmp]*l[u]);
return tmp;
} int main() {
n=read(),m=read(),dist[0]=-1;
for(int i=1;i<=n;i++) {
int FA=read();add(FA,i);
c[i]=read(),l[i]=read();
}
dfs(0,1);
printf("%lld\n",ans);
return 0;
}

BZOJ2809:[APIO2012]dispatching的更多相关文章

  1. BZOJ2809:[Apio2012]dispatching——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=2809 题面复制于:https://www.luogu.org/problemnew/show/155 ...

  2. 【BZOJ2809】[Apio2012]dispatching 可并堆

    [BZOJ2809][Apio2012]dispatching Description 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 M ...

  3. 【bzoj2809】[Apio2012]dispatching 左偏树

    2016-05-31  15:56:57 题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2809 直观的思想是当领导力确定时,尽量选择薪水少的- ...

  4. 【bzoj2809】 Apio2012—dispatching

    http://www.lydsy.com/JudgeOnline/problem.php?id=2809 (题目链接) 题意 给出一棵树,每个节点有两个权值${c}$,${L}$,分别代表花费和领导力 ...

  5. 【bzoj2809】[Apio2012]dispatching 贪心+可并堆

    题目描述 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Master.除了 Master以外,每名忍者都有且仅有一个上级.为保密,同时增 ...

  6. 【BZOJ2809】[APIO2012] dispatching(左偏树例题)

    点此看题面 大致题意: 有\(N\)名忍者,每名忍者有三个属性:上司\(B_i\),薪水\(C_i\)和领导力\(L_i\).你要选择一个忍者作为管理者,然后在所有被他管理的忍者中选择若干名忍者,使薪 ...

  7. 【bzoj2809】[Apio2012]dispatching (左偏树)

    我们需要枚举根,然后从其子树内选尽量多的点,薪水不超过M,可是暴力复杂度不对.于是考虑自下而上合并树(开始每棵树内只有一个节点,就是自己) 每个树是一个堆,我们维护树的节点个数和薪水总和,合并时,不断 ...

  8. bzoj2809 [Apio2012]dispatching(左偏树)

    [Apio2012]dispatching Description 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Master.除了 M ...

  9. 【APIO2012】【BZOJ2809】派遣dispatching

    2809: [Apio2012]dispatching Time Limit: 10 Sec Memory Limit: 128 MB Submit: 1932 Solved: 967 [Submit ...

随机推荐

  1. lua 元表是个啥?

    function readOnly(t) local proxy = {} local mt = { __index = t, __newindex = function(t,k,v) error(& ...

  2. Lock和Condition

    1 什么是可重入锁 可重入锁是说一个线程在已经获取了该锁的情况下,还可以再次获取该锁. 主要的应用场景: 可重入锁指的是在一个线程中可以多次获取同一把锁,比如:一个线程在执行一个带锁的方法,该方法中又 ...

  3. php字符串操作: 去掉UTF-16的空格

    $s = json_encode($s); $s = str_replace('\u00a0','',$s); $s = str_replace('\u3000','',$s); $s = str_r ...

  4. R中常用数据挖掘算法包

    数据挖掘主要分为4类,即预测.分类.聚类和关联,根据不同的挖掘目的选择相应的算法.下面对R语言中常用的数据挖掘包做一个汇总: 连续因变量的预测: stats包 lm函数,实现多元线性回归 stats包 ...

  5. Python socket TCPServer Demo

    #coding:utf-8 from SocketServer import TCPServer,BaseRequestHandler import traceback class MyBaseReq ...

  6. linux shell 字符串操作(长度,查找,替换)

    感谢原创,文章很有帮助. 转自:http://www.cnblogs.com/chengmo/archive/2010/10/02/1841355.html 一.判断读取字符串值 表达式 含义 ${v ...

  7. spring mvc入门教程 转载自【http://elf8848.iteye.com/blog/875830】

    目录  一.前言二.spring mvc 核心类与接口三.spring mvc 核心流程图 四.spring mvc DispatcherServlet说明 五.spring mvc 父子上下文的说明 ...

  8. Kindeditor 函数用途

    1.loadScript  加载文件   2.updateState 更新工具条状态   afterCreate在dom加载的时候执行,dom加载完之前执行的 K.ready dom加载完之后执行   ...

  9. linux基本使用

    (待完善,想到哪就写到哪,目前内容大幅度参考中文man手册) 最重要的命令(man) 在 Linux 下遇到问题,最重要的是要自己寻求帮助, google是个好东西 man 是 Linux 的帮助手册 ...

  10. 【转载】Dom4j的使用(全而好的文章)

    Dom4j的使用(全而好的文章) Dom4j 使用简介 作者:冰云 icecloud(AT)sina.com 时间:2003.12.15   版权声明: 本文由冰云完成,首发于CSDN,未经许可,不得 ...