BZOJ2809:[APIO2012]dispatching
浅谈左偏树: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的更多相关文章
- BZOJ2809:[Apio2012]dispatching——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=2809 题面复制于:https://www.luogu.org/problemnew/show/155 ...
- 【BZOJ2809】[Apio2012]dispatching 可并堆
[BZOJ2809][Apio2012]dispatching Description 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 M ...
- 【bzoj2809】[Apio2012]dispatching 左偏树
2016-05-31 15:56:57 题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2809 直观的思想是当领导力确定时,尽量选择薪水少的- ...
- 【bzoj2809】 Apio2012—dispatching
http://www.lydsy.com/JudgeOnline/problem.php?id=2809 (题目链接) 题意 给出一棵树,每个节点有两个权值${c}$,${L}$,分别代表花费和领导力 ...
- 【bzoj2809】[Apio2012]dispatching 贪心+可并堆
题目描述 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Master.除了 Master以外,每名忍者都有且仅有一个上级.为保密,同时增 ...
- 【BZOJ2809】[APIO2012] dispatching(左偏树例题)
点此看题面 大致题意: 有\(N\)名忍者,每名忍者有三个属性:上司\(B_i\),薪水\(C_i\)和领导力\(L_i\).你要选择一个忍者作为管理者,然后在所有被他管理的忍者中选择若干名忍者,使薪 ...
- 【bzoj2809】[Apio2012]dispatching (左偏树)
我们需要枚举根,然后从其子树内选尽量多的点,薪水不超过M,可是暴力复杂度不对.于是考虑自下而上合并树(开始每棵树内只有一个节点,就是自己) 每个树是一个堆,我们维护树的节点个数和薪水总和,合并时,不断 ...
- bzoj2809 [Apio2012]dispatching(左偏树)
[Apio2012]dispatching Description 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Master.除了 M ...
- 【APIO2012】【BZOJ2809】派遣dispatching
2809: [Apio2012]dispatching Time Limit: 10 Sec Memory Limit: 128 MB Submit: 1932 Solved: 967 [Submit ...
随机推荐
- 【BZOJ4128】Matrix BSGS+hash
[BZOJ4128]Matrix Description 给定矩阵A,B和模数p,求最小的x满足 A^x = B (mod p) Input 第一行两个整数n和p,表示矩阵的阶和模数,接下来一个n * ...
- C - Common Subsequence
C - Common Subsequence Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I ...
- nginx日志自动切分
#!/bin/bash NGINX_LOG_PATH=/data/nginx-/logs # 昨天 YESTERDAY=$(date -d "yesterday" +%Y-%m-% ...
- UnicodeEncodeError: 'latin-1' codec can't encode characters in position 0-3: ordinal not in range(256)
今天使用MySQLdb往MySQL插入中文数据遇到一个异常: UnicodeEncodeError: 'latin-1' codec can't encode characters in positi ...
- Python整型int、浮点float常用方法
#!/usr/bin/env python # -*- coding:utf-8 -*- # Python整型int.浮点float # abs(x) # 返回数字的绝对值,如abs(-10) 返回 ...
- java线程 同步临界区:thinking in java4 21.3.5
java线程 同步临界区:thinking in java4 21.3.5 thinking in java 4免费下载:http://download.csdn.net/detail/liangru ...
- mysql 大数据 查询方面的测试
---方法1: 直接使用数据库提供的SQL语句---语句样式: MySQL中,可用如下方法: SELECT * FROM 表名称 LIMIT M,N---适应场景: 适用于数据量较少的情况(元组百/千 ...
- 几款Java常用基础工具库
通用工具类(字符串.时间格式化.BeanUtils.IO) 1. commons-lang3库 1.1. org.apache.commons.lang3.StringUtils类 日常代码中,我们经 ...
- VS2015 下载 破解
Visual Studio Professional 2015简体中文版(专业版): http://download.microsoft.com/download/B/8/9/B898E46E-CBA ...
- next()和nextLine()的区别
众所周知,在Java中输入字符串有两种方法,就是next()和nextLine(),今天研究了一下其中的区别. 首先,nextLine()的输入是碰到回车就终止输入,而next()方法是碰到空格,回车 ...