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: 很容易看出此题贪心的思路: 只要在每个点的子树中贪心选取费用 ...
随机推荐
- VS code MacOS 环境搭建
环境:MacBook Pro 参考博客 为了动手开发AI代码,我需要安装一个VS code. 开始我以为是安装visual studio呢.我装过visual studio2017. VS code是 ...
- Daily Scrum6 11.10
今日任务: 徐钧鸿:codingcook的sql相关内容,并在进行复查张艺:继续用户管理部分代码黄可嵩:学习搜索的知识,继续进行搜索的移植和响应徐方宇:动态控件和页面间信息传递以及页面响应事件机制试验 ...
- No.0_Team C#
杜正远 略宅,喜昼伏夜出,却又喜游山玩水.平日多出现于宿舍食堂实验室,其他地方鲜有涉足.热爱编程与电脑游戏,渴望自己能写一款自己喜欢玩的电脑游戏,并以此为目标. 喜欢研究算法,但不喜欢硬件.正在入门软 ...
- 20145214 《网络对抗技术》 Web基础
20145214 <网络对抗技术> Web基础 1.实验后回答问题 (1)什么是表单 表单在网页中主要负责数据采集,提供了填写数据.选择数据,收集数据并提交给后台的功能 一个表单有三个基本 ...
- web06-PanduanLogin
电影网站:www.aikan66.com 项目网站:www.aikan66.com 游戏网站:www.aikan66.com 图片网站:www.aikan66.com 书籍网站:www.aikan66 ...
- 校友聊NABCD需求分析
校友聊 NABCD需求分析 N:内网用户流量不够使用 A:基于局域网进行通讯 B:通讯不花费外网流量 C:目前学校还没有使用 D:将软件放在校园网,可以下载使用
- Mevan(转)
Missing artifact com.oracle:ojdbc6:jar:11.2.0.1.0问题解决 ojdbc包pom.xml出错 置顶 2017年08月23日 10:55:25 阅读数:96 ...
- 【贪心算法】POJ-2393 简单贪心水题
一.题目 Description The cows have purchased a yogurt factory that makes world-famous Yucky Yogurt. Over ...
- PHP面试题一
http://www.viphper.com/?p=28 1.用PHP打印出前一天的时间格式是2006-5-10 22:21:21(2分) $a = date("Y-m-d H:i:s&qu ...
- Scrum 项目2.0 3.0
顺带 MY—HR 成员: 角色分配 学号 博客园 团队贡献分 丘惠敏 PM项目经理 201406114203 http://www.cnblogs.com/qiuhuimin/ 19 郭明茵 用户 2 ...