BZOJ 2809: [Apio2012]dispatching( 平衡树 + 启发式合并 )

枚举树上的每个结点做管理者, 贪心地取其子树中薪水较低的, 算出这个结点为管理者的满意度, 更新答案. 用平衡树+启发式合并, 时间复杂度为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 ", 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

原文地址:https://www.cnblogs.com/JSZX11556/p/4871497.html