AC日记——dispatching bzoj 2809

2809: [Apio2012]dispatching

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 3290  Solved: 1740
[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

思路:

  这题不仅仅是恶心,,简直是个,,,唉,不说了。。

  主席树+dfs序;

  最后输出的ans是每个忍者可以派遣的忍者的个数*这个忍者的领导力的最大值;

  我们思考主席树;

  如何用主席树呢?

  首先用dfs序跋整个树便利,记录每个点的开始的标号和最后的标号;

  然后,我们开始往主席树里加点;

  主席树的叶节点是排序离散后的薪水;

  主席树维护两个元素,忍者的数量和花费;

  然后,我们每次查询就可以了;

  细节,,,

  要用long long;

  查询到末节点的时候,返回值是min(当前预算/当前单个忍者花费,当前忍者数量),不然40分;

来,上代码:

#include <cstdio>
#include <iostream>
#include <algorithm>

#define LL long long
#define maxn 100005

using namespace std;

struct TreeNodeType {
    LL l,r,dis,sum;
};
struct TreeNodeType tree[maxn*25];

struct EdgeType {
    LL to,next;
};
struct EdgeType edge[maxn<<1];

LL if_z,n,m,head[maxn],cost[maxn],lead[maxn];
LL cnt,hash[maxn],root[maxn],tot,start[maxn],end[maxn];
LL ans=0,size;

char Cget;

inline void read_int(LL &now)
{
    if_z=1,Cget=getchar(),now=0;
    while(Cget>‘9‘||Cget<‘0‘)
    {
        if(Cget==‘-‘) if_z=-1;
        Cget=getchar();
    }
    while(Cget>=‘0‘&&Cget<=‘9‘)
    {
        now=now*10+Cget-‘0‘;
        Cget=getchar();
    }
    now*=if_z;
}

inline void edge_add(LL from,LL to)
{
    cnt++;
    edge[cnt].to=to;
    edge[cnt].next=head[from];
    head[from]=cnt;
}

void tree_build(LL now,LL l,LL r)
{
    if(l==r) return ;
    LL mid=(l+r)>>1;
    tree[now].l=++tot;
    tree_build(tree[now].l,l,mid);
    tree[now].r=++tot;
    tree_build(tree[now].r,mid+1,r);
}

void tree_add(LL pre,LL now,LL pos,LL l,LL r)
{
    tree[now].dis=tree[pre].dis+1;
    tree[now].sum=tree[pre].sum+hash[pos];
    if(l==r) return ;
    LL mid=(l+r)>>1;
    if(pos<=mid)
    {
        tree[now].l=++tot;
        tree_add(tree[pre].l,tree[now].l,pos,l,mid);
        tree[now].r=tree[pre].r;
    }
    else
    {
        tree[now].r=++tot;
        tree_add(tree[pre].r,tree[now].r,pos,mid+1,r);
        tree[now].l=tree[pre].l;
    }
}

void Search(LL now,LL fa)
{
    start[now]=cnt++,root[cnt]=++tot;
    LL pos=lower_bound(hash+1,hash+size+1,cost[now])-hash;
    tree_add(root[cnt-1],root[cnt],pos,1,size);
    for(LL i=head[now];i;i=edge[i].next)
    {
        if(edge[i].to==fa) continue;
        Search(edge[i].to,now);
    }
    end[now]=cnt;
}

LL tree_query(LL pre,LL now,LL pos,LL l,LL r)
{
    if(l==r) return min(pos/hash[l],tree[now].dis-tree[pre].dis);
    LL pos_=tree[tree[now].l].dis-tree[tree[pre].l].dis;
    LL mid=(l+r)>>1,dis_=tree[tree[now].l].sum-tree[tree[pre].l].sum;
    if(pos<=dis_) return tree_query(tree[pre].l,tree[now].l,pos,l,mid);
    else return tree_query(tree[pre].r,tree[now].r,pos-dis_,mid+1,r)+pos_;
}

int main()
{
    read_int(n),read_int(m);
    LL bi,master;
    for(LL i=1;i<=n;i++)
    {
        read_int(bi),read_int(cost[i]),read_int(lead[i]);
        edge_add(bi,i),edge_add(i,bi),hash[i]=cost[i];
        if(bi==0) master=i;
    }
    sort(hash+1,hash+n+1);
    size=unique(hash+1,hash+n+1)-hash-1;
    root[0]=++tot;
    tree_build(root[0],1,size);
    cnt=0,Search(master,0);
    for(LL i=1;i<=n;i++)
    {
        LL pos=tree_query(root[start[i]],root[end[i]],m,1,size);
        ans=max(ans,lead[i]*pos);
    }
    cout<<ans<<endl;
    return 0;
}
时间: 2024-07-31 14:33:08

AC日记——dispatching bzoj 2809的相关文章

AC日记——旅游 bzoj 2157

2157 思路: LCT: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 400005 #define INF 0x3f3f3f3f int n,val[maxn],sum[maxn],Max[maxn],Min[maxn],ch[maxn][2]; int rev[maxn],flag[maxn],f[maxn],top,st[maxn],m,cur[maxn],id; inline void in(int &a

AC日记——Aragorn&#39;s Story HDU 3966

Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10510    Accepted Submission(s): 2766 Problem Description Our protagonist is the handsome human prince Aragorn comes from The Lor

【BZOJ 2809】2809: [Apio2012]dispatching (左偏树)

2809: [Apio2012]dispatching Description 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Master.除了 Master以外,每名忍者都有且仅有一个上级.为保密,同时增强忍者们的领导力,所有与他们工作相关的指令总是由上级发送给他的直接下属,而不允许通过其他的方式发送.现在你要招募一批忍者,并把它们派遣给顾客.你需要为每个被派遣的忍者 支付一定的薪水,同时使得支付的薪水总额不超过你的预算.另外,为

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

题目大意:给定一棵树,选定一棵子树中的一些点,薪水和不能超过m,求点的数量*子树根节点的领导能力的最大值 考虑对于每一个节点,我们维护一种数据结构,在当中贪心寻找薪金小的雇佣. 每一个节点暴力重建一定不行.我们考虑可并数据结构.每一个节点将子节点的信息直接合并就可以 能够用启示式合并的Treap.也能够用可并堆 今天特意去学了这玩应0.0 先写了左偏树 然后又写了下随机堆-- 后者速度上更快一些 只是建议从左偏树開始学起 总之平衡树常数各种大就是了0.0 Treap+启示式合并 #include

AC日记——[Noi2011]阿狸的打字机 bzoj 2434

2434 思路: 构建ac自动机: 抽离fail树: 根据字符串建立主席树: 在线处理询问: 询问x在y中出现多少次,等同于y有多少字母的fail能走到x: 1a,hahahahah: 代码: #include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn

AC日记——[HEOI2012]旅行问题 bzoj 2746

2746 思路: 建立ac自动机,然后把fail树抽出来: 然后在fail树上走lca(神奇): 代码: #include <cstdio> #include <vector> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn 1000005 #define mod 1000000007LL struct Tr

BZOJ 2809 APIO2012 dispatching Treap+启发式合并 / 可并堆

题目大意:给定一棵树,选定一棵子树中的一些点,薪水和不能超过m,求点的数量*子树根节点的领导能力的最大值 考虑对于每个节点,我们维护一种数据结构,在其中贪心寻找薪金小的雇佣. 每个节点暴力重建一定不行,我们考虑可并数据结构,每个节点将子节点的信息直接合并即可 可以用启发式合并的Treap,也可以用可并堆 今天特意去学了这玩应0.0 先写了左偏树 然后又写了下随机堆-- 后者速度上更快一些 不过建议从左偏树开始学起 总之平衡树常数各种大就是了0.0 Treap+启发式合并 #include<cst

AC日记——[ZJOI2012]网络 bzoj 2816

2816 思路: 多个LCT: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 10005 #define ll long long int val[maxn]; struct LinkCutTreeType { int f[maxn],Max[maxn],ch[maxn][2],rev[maxn],sta[maxn],top,cnt[maxn]; void updata(int now) { Max[now]=va

AC日记——[Scoi2010]序列操作 bzoj 1858

1858 思路: 恶心: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 100005 struct TreeNodeType { int l,r,l1,r1,m1,l0,r0,m0,mid,size,cnt1,cnt0; bool f1,f0,ff; inline void turn(){swap(l1,l0),swap(r1,r0),swap(m1,m0),swap(cnt1,cnt0);} inline voi