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 ...
随机推荐
- 【BZOJ3672】[Noi2014]购票 树分治+斜率优化
[BZOJ3672][Noi2014]购票 Description 今年夏天,NOI在SZ市迎来了她30周岁的生日.来自全国 n 个城市的OIer们都会从各地出发,到SZ市参加这次盛会. ...
- Gone Fishing(贪心)
Gone Fishing John is going on a fising trip. He has h hours available (1 ≤ h ≤ 16), and there are n ...
- EasyDSS流媒体服务器灵活地帮助用户实现摄像机RTSP转RTMP直播功能
简要描述 今天突然接到国内某上市公司同事打来的技术咨询电话,经过简单的沟通,大概所描述的需求是: 1.目前现场有非常多的摄像机资源需要接入: 2.需要将摄像机的RTSP流转成RTMP流接入到微信小程序 ...
- META-INF中的INF的意思
1 META是元的意思,比如meta data,元数据. 2 什么是meta data 元数据就是描述其它数据的数据,比如web page中的meta data,包括关键字,对该网页的描述等等. 3 ...
- DNN 数据的表示
- centos 下安装pdo_pgsql 只需一个命令_______yum install php56w-pgsql
[root@localhost ~]# yum install php56w-pgsql Loaded plugins: fastestmirror, langpacks Repository pgd ...
- 读paper:Deep Convolutional Neural Network using Triplets of Faces, Deep Ensemble, andScore-level Fusion for Face Recognition
今天给大家带来一篇来自CVPR 2017关于人脸识别的文章. 文章题目:Deep Convolutional Neural Network using Triplets of Faces, Deep ...
- spring mvc入门教程 转载自【http://elf8848.iteye.com/blog/875830】
目录 一.前言二.spring mvc 核心类与接口三.spring mvc 核心流程图 四.spring mvc DispatcherServlet说明 五.spring mvc 父子上下文的说明 ...
- 图片加载Picasso
https://github.com/square/picasso 基本用法 // 基本用法 // 普通加载图片 Picasso.with(PicassoActivity.this) .load(&q ...
- c# 文件IO操作 StreamReader StreamWriter Split 使用
StreamWriter(String,Boolean) 若要追加数据到该文件中,则为 true:若要覆盖该文件,则为 false. 如果指定的文件不存在,该参数无效,且构造函数将创建一个新文件. 例 ...