bzoj 2809
2809: [Apio2012]dispatching
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 4519 Solved: 2329
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
0 3 3
1 3 5
2 2 2
1 2 4
2 3 1
Sample Output
HINT
如果我们选择编号为 1的忍者作为管理者并且派遣第三个和第四个忍者,薪水总和为 4,没有超过总预算 4。因为派遣了 2 个忍者并且管理者的领导力为 3,
用户的满意度为 2 ,是可以得到的用户满意度的最大值。
Source
代码:
//对于每一个子树显然是选花费小的节点合算,维护一个大根堆,并且当堆的花费和大于m时删掉堆根。
//斜堆。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int MAXN=;
int n,m,tot,cnt,head[MAXN],C[MAXN],L[MAXN],node[MAXN][],root[MAXN],key[MAXN];
ll sum[MAXN],size[MAXN],ans;
struct Edge { int to,next; }edge[MAXN<<];
void init()
{
tot=cnt=;
ans=;
memset(head,-,sizeof(head));
memset(node,,sizeof(node));
}
void addedge(int x,int y)
{
edge[tot].to=y;edge[tot].next=head[x];
head[x]=tot++;
}
int mmeg(int x,int y)
{
if(x==) return y;
if(y==) return x;
if(key[x]<key[y]) swap(x,y);
node[x][]=mmeg(node[x][],y);
swap(node[x][],node[x][]);
return x;
}
int ttop(int x) { return key[x]; }
int ppop(int x) { return mmeg(node[x][],node[x][]); }
void dfs(int x)
{
root[x]=++cnt;
size[x]=;
key[cnt]=sum[x]=C[x];
for(int i=head[x];i!=-;i=edge[i].next){
int y=edge[i].to;
dfs(y);
size[x]+=size[y];
sum[x]+=sum[y];
root[x]=mmeg(root[x],root[y]);
}
while(sum[x]>m){
sum[x]-=ttop(root[x]);
root[x]=ppop(root[x]);
size[x]--;
}
ans=max(ans,size[x]*L[x]);
}
int main()
{
init();
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
int x;
scanf("%d%d%d",&x,&C[i],&L[i]);
addedge(x,i);
}
dfs();
printf("%lld\n",ans);
return ;
}
//左偏树。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int MAXN=;
int n,m,tot,cnt,head[MAXN],node[MAXN][],key[MAXN],root[MAXN],d[MAXN],C[MAXN],L[MAXN];
ll size[MAXN],sum[MAXN],ans;
struct Edge { int to,next; }edge[MAXN<<];
void init()
{
tot=cnt=;
ans=;
memset(head,-,sizeof(head));
memset(node,,sizeof(node));
memset(d,-,sizeof(d));
}
void addedge(int x,int y)
{
edge[tot].to=y;edge[tot].next=head[x];
head[x]=tot++;
}
int mmeg(int x,int y)
{
if(x==) return y;
if(y==) return x;
if(key[x]<key[y]) swap(x,y);
node[x][]=mmeg(node[x][],y);
if(d[node[x][]]>d[node[x][]]) swap(node[x][],node[x][]);
d[x]=d[node[x][]]+;
return x;
}
int ttop(int x) { return key[x]; }
int ppop(int x) { return mmeg(node[x][],node[x][]); }
void dfs(int x)
{
size[x]=;
root[x]=++cnt;
d[cnt]=;
sum[x]=key[cnt]=C[x];
for(int i=head[x];i!=-;i=edge[i].next){
int y=edge[i].to;
dfs(y);
size[x]+=size[y];
sum[x]+=sum[y];
root[x]=mmeg(root[x],root[y]);
}
while(sum[x]>m){
sum[x]-=ttop(root[x]);
root[x]=ppop(root[x]);
size[x]--;
}
ans=max(ans,size[x]*L[x]);
}
int main()
{
init();
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
int x;
scanf("%d%d%d",&x,&C[i],&L[i]);
addedge(x,i);
}
dfs();
printf("%lld\n",ans);
return ;
}
bzoj 2809的更多相关文章
- BZOJ 2809: [Apio2012]dispatching( 平衡树 + 启发式合并 )
枚举树上的每个结点做管理者, 贪心地取其子树中薪水较低的, 算出这个结点为管理者的满意度, 更新答案. 用平衡树+启发式合并, 时间复杂度为O(N log²N) ------------------- ...
- BZOJ 2809 APIO2012 dispatching Treap+启示式合并 / 可并堆
题目大意:给定一棵树,选定一棵子树中的一些点,薪水和不能超过m,求点的数量*子树根节点的领导能力的最大值 考虑对于每一个节点,我们维护一种数据结构,在当中贪心寻找薪金小的雇佣. 每一个节点暴力重建一定 ...
- BZOJ 2809: [Apio2012]dispatching(左偏树)
http://www.lydsy.com/JudgeOnline/problem.php?id=2809 题意: 思路:最简单的想法就是枚举管理者,在其子树中从薪水低的开始选起,但是每个节点都这样处理 ...
- bzoj 2809 可并堆维护子树信息
对于每个节点,要在其子树中选尽量多的节点,并且节点的权值和小于一个定值. 建立大根堆,每个节点从儿子节点合并,并弹出最大值直到和满足要求. /***************************** ...
- bzoj 2809: [Apio2012]dispatching -- 可并堆
2809: [Apio2012]dispatching Time Limit: 10 Sec Memory Limit: 128 MB Description 在一个忍者的帮派里,一些忍者们被选中派 ...
- bzoj 2809 左偏树\平衡树启发式合并
首先我们对于一颗树,要选取最多的节点使得代价和不超过m,那么我们可以对于每一个节点维护一个平衡树,平衡树维护代价以及代价的和,那么我们可以在logn的时间内求出这个子树最多选取的节点数,然后对于一个节 ...
- 【BZOJ 2809】2809: [Apio2012]dispatching (左偏树)
2809: [Apio2012]dispatching Description 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Maste ...
- BZOJ 2809 [Apio2012]dispatching(斜堆+树形DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2809 [题目大意] 给出一棵树,求出每个点有个权值,和一个乘算值,请选取一棵子树, 并 ...
- [BZOJ 2809] Dispatching
Link:https://www.lydsy.com/JudgeOnline/problem.php?id=2809 Algorithm: 很容易看出此题贪心的思路: 只要在每个点的子树中贪心选取费用 ...
随机推荐
- 通过NPM快速发布你的NodeJS模块(组件包)
1.更新 NPM - [ npm install -g npm | 该步骤可选:最好使用新版本] 楼主当前版本号 2.6.1 ,如果更新报错,可以尝试 国内淘宝镜像 $ npm -v 2.6.1 // ...
- Classifier
1.视频:https://morvanzhou.github.io/tutorials/machine-learning/keras/2-2-classifier/ 2.敲了代码,但是运行结果不懂,明 ...
- DFS--障碍在指定时间会消失
哈利被困在了一个魔法花园里.魔法花园是一个 N*M 的矩形,在其中有着许多植物, 这些植物会在时刻 K 的倍数消失. 哈利每单位时间都会选择上.下.左.右四 个方向的其中一个进行移动. #includ ...
- 浏览器播放rtmp流
我是利用flash插件实现的,需要以下几个文件: flowplayer-3.2.8.min.js flowplayer-3.2.18.swf flowplayer.rtmp-3.2.8.swf flo ...
- 巧妙使用CSS媒体查询(Media Queries)和JavaScript判断浏览器设备类型的好方法
有无数的理由要求我们在任何时候都应该知道用户是使用的什么设备浏览我们的网站——宽屏,普通屏,平板,手机?知道这些特征,我们web应用的CSS和JavaScript才能同步做相应的操作.在给Mozill ...
- JSPatch 原理
原理 JSPatch用iOS内置的JavaScriptCore.framework作为JS引擎,但没有用它JSExport的特性进行JS-OC函 数互调,而是通过Objective-C Runtime ...
- Current request is not a multipart request
1. 文件上传需要在form表单中添加<form enctype="multipart/form-data"> 2. SpringMVC默认是关闭fileupload功 ...
- 项目上线,php的错误信息必须不让其在页面中显示给客户,
对于PHP开发者来 说,一旦某个产品投入使用,应该立即将 display_errors选项关闭,以免因为这些错误所透露的路径.数据库连接.数据表等信息而遭到黑客攻击.但是,任何一个产品在投入使用后,都 ...
- Mysql 定位执行效率低的sql 语句
一.通过MySQL慢查询日志定位执行效率低的SQL语句. MySQL通过慢查询日志定位那些执行效率较低的SQL 语句,用--log-slow-queries[=file_name]选项启动时,mysq ...
- dotnet core sdk 2.1 在centos下的安装
1. 安装微软的仓库 rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm 2. 修改仓库 ...