枚举树上的每个结点做管理者, 贪心地取其子树中薪水较低的, 算出这个结点为管理者的满意度, 更新答案. 用平衡树+启发式合并, 时间复杂度为O(N log²N)

---------------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cstdlib>
 
using namespace std;
 
const int maxn = 100009;
 
typedef long long ll;
 
struct Node {
Node *ch[2];
int v, s, r; ll sum;
void upd() {
s = ch[0]->s + ch[1]->s + 1;
sum = ch[0]->sum + ch[1]->sum + v;
}
} pool[maxn], *null, *root[maxn];
 
queue<Node*> q;
int N, M, w[maxn], Rt;
ll ans = 0;
 
void init() {
null = pool;
null->ch[0] = null->ch[1] = null;
null->s = null->v = null->sum = 0;
for(int i = 1; i < maxn; i++) q.push(pool + i);
}
 
Node* newNode(int v) {
Node* t = q.front(); q.pop();
t->ch[0] = t->ch[1] = null;
t->v = v; t->s = 1; t->r = rand();
return t;
}
 
void Rotate(Node* &t, int d) {
Node* o = t->ch[d ^ 1];
t->ch[d ^ 1] = o->ch[d];
o->ch[d] = t;
t->upd(); o->upd();
t = o;
}
 
void Insert(Node* &t, int v) {
if(t == null)
t = newNode(v);
else {
int d = (t->v < v);
Insert(t->ch[d], v);
if(t->ch[d]->r > t->r) Rotate(t, d ^ 1);
}
t->upd();
}
 
void Delete(Node* &t, int v) {
int d = (v != t->v ? (t->v < v) : -1);
if(d == -1) {
if(t->ch[0] != null && t->ch[1] != null) {
int h = (t->ch[0]->r < t->ch[0]->r);
Rotate(t, h); Delete(t->ch[h], v);
} else {
q.push(t);
t = (t->ch[0] != null ? t->ch[0] : t->ch[1]);
}
} else 
Delete(t->ch[d], v);
if(t != null) t->upd();
}
 
int cal(int x) {
int cnt = 0; ll tot = M;
for(Node* t = root[x]; t != null; ) {
if(t->ch[0]->sum + t->v <= tot) {
tot -= t->ch[0]->sum + t->v;
cnt += t->ch[0]->s + 1;
t = t->ch[1];
} else
t = t->ch[0];
}
return cnt;
}
 
struct edge {
int to;
edge* next;
} E[maxn << 1], *pt = E, *head[maxn];
 
void addedge(int u, int v) {
pt->to = v; pt->next = head[u]; head[u] = pt++;
}
 
Node* con(Node* a, Node* b) {
if(a->s < b->s) swap(a, b);
while(b->s) {
Insert(a, b->v);
Delete(b, b->v);
}
return a;
}
 
void dfs(int x) {
for(edge* e = head[x]; e; e = e->next) {
dfs(e->to);
root[x] = con(root[x], root[e->to]);
}
ans = max(ans, ll(w[x]) * cal(x));
}
 
int main() {
init();
scanf("%d%d", &N, &M);
for(int i = 0; i < N; i++) {
int p, v;
scanf("%d%d%d", &p, &v, w + i);
root[i] = newNode(v);
if(--p < 0) 
Rt = i;
else
addedge(p, i);
}
dfs(Rt);
printf("%lld\n", ans);
return 0;
}

---------------------------------------------------------------------------------

2809: [Apio2012]dispatching

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 2026  Solved: 1031
[Submit][Status][Discuss]

Description

在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿。在这个帮派里,有一名忍者被称之为 Master。除了 Master以外,每名忍者都有且仅有一个上级。为保密,同时增强忍者们的领导力,所有与他们工作相关的指令总是由上级发送给他的直接下属,而不允许通过其他的方式发送。现在你要招募一批忍者,并把它们派遣给顾客。你需要为每个被派遣的忍者 支付一定的薪水,同时使得支付的薪水总额不超过你的预算。另外,为了发送指令,你需要选择一名忍者作为管理者,要求这个管理者可以向所有被派遣的忍者 发送指令,在发送指令时,任何忍者(不管是否被派遣)都可以作为消息的传递 人。管理者自己可以被派遣,也可以不被派遣。当然,如果管理者没有被排遣,就不需要支付管理者的薪水。你的目标是在预算内使顾客的满意度最大。这里定义顾客的满意度为派遣的忍者总数乘以管理者的领导力水平,其中每个忍者的领导力水平也是一定的。写一个程序,给定每一个忍者 i的上级 Bi,薪水Ci,领导力L i,以及支付给忍者们的薪水总预算 M,输出在预算内满足上述要求时顾客满意度的最大值。
1  ≤N ≤ 100,000 忍者的个数;
1  ≤M ≤ 1,000,000,000 薪水总预算; 
 
0  ≤Bi < i  忍者的上级的编号;
1  ≤Ci ≤ M                     忍者的薪水;
1  ≤Li ≤ 1,000,000,000             忍者的领导力水平。
 
 

Input

从标准输入读入数据。
 
第一行包含两个整数 N和 M,其中 N表示忍者的个数,M表示薪水的总预算。
 
接下来 N行描述忍者们的上级、薪水以及领导力。其中的第 i 行包含三个整 Bi , C i , L i分别表示第i个忍者的上级,薪水以及领导力。Master满足B i = 0,并且每一个忍者的老板的编号一定小于自己的编号 Bi < i。

Output

输出一个数,表示在预算内顾客的满意度的最大值。
 
 

Sample Input

5 4
0 3 3
1 3 5
2 2 2
1 2 4
2 3 1

Sample Output

6

HINT

如果我们选择编号为 1的忍者作为管理者并且派遣第三个和第四个忍者,薪水总和为 4,没有超过总预算                         4。因为派遣了                              2   个忍者并且管理者的领导力为      3,

用户的满意度为 2      ,是可以得到的用户满意度的最大值。

Source

BZOJ 2809: [Apio2012]dispatching( 平衡树 + 启发式合并 )的更多相关文章

  1. bzoj 2809: [Apio2012]dispatching -- 可并堆

    2809: [Apio2012]dispatching Time Limit: 10 Sec  Memory Limit: 128 MB Description 在一个忍者的帮派里,一些忍者们被选中派 ...

  2. BZOJ 2809 APIO2012 dispatching Treap+启示式合并 / 可并堆

    题目大意:给定一棵树,选定一棵子树中的一些点,薪水和不能超过m,求点的数量*子树根节点的领导能力的最大值 考虑对于每一个节点,我们维护一种数据结构,在当中贪心寻找薪金小的雇佣. 每一个节点暴力重建一定 ...

  3. BZOJ 2809: [Apio2012]dispatching(左偏树)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2809 题意: 思路:最简单的想法就是枚举管理者,在其子树中从薪水低的开始选起,但是每个节点都这样处理 ...

  4. BZOJ 2809 [Apio2012]dispatching(斜堆+树形DP)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2809 [题目大意] 给出一棵树,求出每个点有个权值,和一个乘算值,请选取一棵子树, 并 ...

  5. bzoj 2809: [Apio2012]dispatching

    #include<cstdio> #include<algorithm> #define M 1000005 using namespace std; long long an ...

  6. BZOJ 2809: [Apio2012]dispatching [斜堆]

    题意:主席树做法见上一题 我曾发过誓再也不写左偏树(期末考试前一天下午5个小时没写出棘手的操作) 于是我来写斜堆啦 从叶子往根合并,维护斜堆就行了 题目连拓扑序都给你了... 说一下斜堆的操作: 合并 ...

  7. BZOJ 2809: [Apio2012]dispatching(可并堆 左偏树板题)

    这道题只要读懂题目一切好说. 给出nnn个点的一棵树,每一个点有一个费用vvv和一个领导力aaa,给出费用上限mmm.求下面这个式子的最大值ax∗∣S∣ ( S⊂x的子树, ∑iv[i]≤m )\la ...

  8. BZOJ 2809: [Apio2012]dispatching [主席树 DFS序]

    传送门 题意:查询树上根节点值*子树中权值和$\le m$的最大数量 最大值是多少 求$DFS$序,然后变成区间中和$\le m$最多有几个元素,建主席树,然后权值线段树上二分就行了 $WA$:又把边 ...

  9. bzoj 2809: [Apio2012]dispatching【dfs序+主席树】

    可并堆就可以,但是想复健一下主席树. 考虑枚举管理者,然后选忍者的时候在子树中贪心的从小到大选.做成dfs序就是选区间内和小于等于k的最多点.可以用主席树,查询的时候在主席树上二分即可 这里注意,为了 ...

随机推荐

  1. BZOJ 1452: [JSOI2009]Count(二维BIT)

    为每一个权值开一个二维树状数组. ------------------------------------------------------------------------- #include& ...

  2. ASP.NET jQuery 随笔 显示RadioButtonList成员选中的内容和值

    通过jQuery来获取RadioButtonList成员内容. <%@ Page Language="C#" AutoEventWireup="true" ...

  3. 驯服你的Windows Server 2003

    虽然通过一些技巧可以让Windows Server 2003更符合我们的使用习惯,但对我等菜鸟来说,操作还是有相当的难度,有没有更简单.更省事的驯服它的办法呢?有,那就是使用Windows Serve ...

  4. access数据库 top语句失效解决方法

    使用查询语句  select   top 1  *  from  News  order  by  [PublicTime] desc   就不一定管用了,如果News表里面的PublicTime字段 ...

  5. DB2 权限控制

    http://blog.csdn.net/liujinwei2005/article/details/8606983 http://www.ibm.com/developerworks/cn/data ...

  6. podoWeb极品流氓软件

    用firefox做页面网络性能测试, 打开网页就会发现页面不停的去请求googleapi.com上的jquery Or swfobject.js,页面就此卡住,一直在等待类似这两个请求的返回,知道Ab ...

  7. flex正则表达式

    正则表达式是一种通用的标准,大部分计算机语言都支持正则表达式,包括as3,这里收集了一些常用的正则表达式语句,大家用到的时候就不用自己写了 ^\d+$ //匹配非负整数(正整数 + 0) ^[0-9] ...

  8. HDU-4866-Shooting(函数式线段树)

    Problem Description In the shooting game, the player can choose to stand in the position of [1, X] t ...

  9. python 之 yield表达式

    如果在某个函数中包含了yield, 这意味着这个函数已经是一个Generator, 它的执行 会和其他普通的函数有很多不同. 比如: def   h(): print    'To   be  bra ...

  10. js数组练习

    //查找数组对象中 age 大于 18 对象 function filterAdult(arr) { return arr.filter(function(item, index, array) { ...