枚举树上的每个结点做管理者, 贪心地取其子树中薪水较低的, 算出这个结点为管理者的满意度, 更新答案. 用平衡树+启发式合并, 时间复杂度为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. javascriptDOM编程艺术_学习笔记_知识点 动态创建标记

    传统技术:document.write 和 innerHTML 深入剖析DOM方法:createElement.createTextNode.appendChild 和 insertBefore   ...

  2. android五种布局模式

    Android布局是应用界面开发的重要一环,在Android中,共有五种布局方式,分别是:LinearLayout (线性布局),FrameLayout(框架布局),AbsoluteLayout(绝对 ...

  3. [LeetCode]题解(python):001-Two-Sum

    题目来源: https://leetcode.com/problems/two-sum/ 题意分析: 这道题目是输入一个数组和target,要在一个数组中找到两个数字,其和为target,从小到大输出 ...

  4. python自学笔记(九)python练习题

    1. 已知字符串 a = "aAsmr3idd4bgs7Dlsf9eAF",要求如下 1.1 请将a字符串的大写改为小写,小写改为大写 print a.swapcase() 1.2 ...

  5. verilog中读取文件中的字符串_modelsim高级仿真

    今天给个程序大家玩玩.因为今天遇到一个问题,就是要向UART发送指令,指令非常多,都是字符串.一直copy 函数 UART ("COMM_1");  UART ("COM ...

  6. 代码收藏 JS实现页内查找定位功能

    前部分为IE下搜索方法 用TextRange来实现 后部分为firefox.chrome下搜索方法 var nextIndex = 0; var searchValue = ''; var input ...

  7. 1005 - Rooks(规律)

    1005 - Rooks   PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 32 MB A rook is ...

  8. AndroidStudio 0.2.x 引入多模块Eclipse项目

    !!!!太他妈的累人了!整整折腾了两天!!!!!!! 不知从那个版本开始ImportModule... 从AndroidStudio的File菜单中消失了,在0.2之前的版本作为library的模块可 ...

  9. C# MyNewQueue 消息队列

    C# using System; using System.Messaging; using System.Drawing; using System.IO; namespace MyProject ...

  10. C++之对象组合

    #include<stdio.h>//初始化列表 提供了对成员变量初始化的方式//Constructor        class M      {       private:      ...