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: 很容易看出此题贪心的思路: 只要在每个点的子树中贪心选取费用 ...
随机推荐
- OGG FOR BIGDATA 安装(修正)
参考:http://docs.oracle.com/goldengate/bd1221/gg-bd/GADBD/toc.htm 一.环境介绍 源:centos6.5 oracl e 11.20.4 ...
- 微信公众号开发笔记1(nodejs开发)
本篇记录了微信公众号开发的一些笔记 一.微信服务器与我们服务器的交流 微信开发者拥有自己的服务器,在我们服务器上可以与微信服务器进行交流.既然可以交流,那就必定需要前提条件(微信认证),也就是说,只有 ...
- Daily Scrum9 11.13
昨天的任务已完成. 今日任务: 姓名 今日任务 时长 徐钧鸿 测试SQL包里的代码 2h 张艺 继续搭建还没搭建完的框架 修复bug 2h 黄可嵩 继续进行搜索响应编写 2h 徐方宇 搭建框架 修改b ...
- Sprint2
进展:主要进行了在安卓手机端进行APP开发的资料及有关学习的视频的查找等.了解也学习了这些资料还有技术.第一个任务完成了一半. 燃尽图:
- Sprint计划表
Sprint会议计划 一.Sprint 需求 准备环节:小组成员利用周六周日在网上查阅Android开发的教程,练习开发一些简单的小程序,具备一定的开发能力,在电脑上搭建Android开发环境,做好 ...
- BZOJ 3132(上帝造题的七分钟-树状数组求和+2D逆求和数组)
3132: 上帝造题的七分钟 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 46 Solved: 18[Submit][Status][Discus ...
- ASP.NET MVC 3.0 参考源码索引
http://www.projky.com/asp.netmvc/3.0/Microsoft/Internal/Web/Utils/CommonResources.cs.htmlhttp://www. ...
- Internet History, Technology and Security (Week 3)
Week 3 History: The Web Makes it Easy to Use Welcome to week 3! This is our fourth and final week of ...
- C语言以字符形式读写文件
一.字符读取函数 fgetc (一).函数介绍 fgetc 是 file get char 的缩写,意思是从指定的文件中读取一个字符.函数原型为: int fgetc(FILE* fp) fp 为文件 ...
- [转帖]go 的goroutine 以及 channel 的简介.
进程,线程的概念在操作系统的书上已经有详细的介绍.进程是内存资源管理和cpu调度的执行单元.为了有效利用多核处理器的优势,将进程进一步细分,允许一个进程里存在多个线程,这多个线程还是共享同一片内存空间 ...