F. Cookies

链接:http://codeforces.com/contest/1099/problem/F

题意:

给你一棵树,树上有n个节点,每个节点上有ai块饼干,在这个节点上的每块饼干需要花费bi时间,有两个玩家,玩家一可以移动到当前点的子节点也可以申请游戏结束返回根节点并吃沿途的饼干,玩家二可以删除当前点到儿子节点的一条边,走路和吃饼干都消耗时间,会给出一个总时间,在总时间内尽可能的多吃饼干,问最多能吃多少个?

思路:

由于是玩家一先手,那么最开始的最大边则不会被删除,但之后路途的最大边都会被玩家二删除,所以我们对于当前点我们需要求:

f1.如果现在回头那么最多可以吃到多少饼干

f2.向下走最多可以吃到多少饼干

f3.向下走次多可以吃到多少饼干

当在根节点是取12最大值,其余情况取13最大值,用线段树维护剩余时间最多能取多少,

对单个饼干的时间建线段树,权值为饼干个数,查询时优先向左取,因为左边是花费时间少的,这样就可以取得剩余时间内能得到的最多的饼干。

实现代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid ll m = (l + r) >> 1 const ll M = 1e6+;
const ll Max = 1e6;
struct node{
ll to,w,next;
}e[M<<];
ll sum[M<<],num[M<<],a[M],tim[M],head[M],cnt;
void add(ll u,ll v,ll w){
e[++cnt].to = v;e[cnt].next = head[u];e[cnt].w = w;head[u] = cnt;
} void pushup(ll rt){
sum[rt] = sum[rt<<] + sum[rt<<|];
num[rt] = num[rt<<] + num[rt<<|];
} void update(ll p,ll c,ll l,ll r,ll rt){
if(l == r){
sum[rt] += p*c;
num[rt] += c;
return ;
}
mid;
if(p <= m) update(p,c,lson);
else update(p,c,rson);
pushup(rt);
} ll query(ll t,ll l,ll r,ll rt){
if(sum[rt] <= t) return num[rt];
if(l == r){
return t/l;
}
mid;
if(sum[rt<<] >= t) return query(t,lson);
else return num[rt<<] + query(t-sum[rt<<],rson);
} ll dfs(ll u,ll fa,ll t){
update(tim[u],a[u],,Max,);
ll f1 = query(t,,Max,);
ll f2 = ,f3 = ; for(ll i = head[u];i;i=e[i].next){
ll v = e[i].to;
if(v == fa) continue;
if(t <= e[i].w*) continue;
ll k = dfs(v,u,t-e[i].w*);
if(k > f2) f3 = f2,f2 = k;
else if(k > f3) f3 = k; }
update(tim[u],-a[u],,Max,);
if(u == ) return max(f1,f2);
else return max(f1,f3);
} int main()
{
ll n,T,x,y;
scanf("%lld%lld",&n,&T);
for(ll i = ;i <= n;i ++) scanf("%lld",&a[i]);
for(ll i = ;i <= n;i ++) scanf("%lld",&tim[i]);
for(ll i = ;i <= n;i ++){
scanf("%lld%lld",&x,&y);
add(i,x,y); add(x,i,y);
}
printf("%lld\n",dfs(,,T));
return ;
}

Codeforces Round #530 (Div. 2) F (树形dp+线段树)的更多相关文章

  1. Codeforces Round #587 (Div. 3) F Wi-Fi(线段树+dp)

    题意:给定一个字符串s 现在让你用最小的花费 覆盖所有区间 思路:dp[i]表示前i个全覆盖以后的花费 如果是0 我们只能直接加上当前位置的权值 否则 我们可以区间询问一下最小值 然后更新 #incl ...

  2. Codeforces Round #271 (Div. 2) F. Ant colony 线段树

    F. Ant colony time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  3. Codeforces Round #530 (Div. 2) F 线段树 + 树形dp(自下往上)

    https://codeforces.com/contest/1099/problem/F 题意 一颗n个节点的树上,每个点都有\(x[i]\)个饼干,然后在i节点上吃一个饼干的时间是\(t[i]\) ...

  4. Codeforces Round #530 (Div. 2)F Cookies (树形dp+线段树)

    题:https://codeforces.com/contest/1099/problem/F 题意:给定一个树,每个节点有俩个信息x和t,分别表示这个节点上的饼干个数和先手吃掉这个节点上一个饼干的的 ...

  5. Codeforces Round #530 (Div. 2) F - Cookies

    F - Cookies 思路:我们先考虑如何算出在每个节点结束最多能吃多少饼干, 这个dfs的时候用线段树维护一下就好了, 然后有个这个信息之后树上小dp一下就好啦. #include<bits ...

  6. Codeforces Round #426 (Div. 2) D. The Bakery 线段树优化DP

    D. The Bakery   Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought req ...

  7. Codeforces 671D. Roads in Yusland(树形DP+线段树)

    调了半天居然还能是线段树写错了,药丸 这题大概是类似一个树形DP的东西.设$dp[i]$为修完i这棵子树的最小代价,假设当前点为$x$,但是转移的时候我们不知道子节点到底有没有一条越过$x$的路.如果 ...

  8. Codeforces Round #244 (Div. 2) B. Prison Transfer 线段树rmq

    B. Prison Transfer Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/pro ...

  9. Codeforces Round #603 (Div. 2) E. Editor(线段树)

    链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...

随机推荐

  1. Docker镜像的修改和自定义

    一.docker镜像的更新 (1)启动镜像,写入一些文件或者更新软件 docker run -it 3afd47092a0e[root@44652ba46352 /]# ls (2)更新镜像 dock ...

  2. iOS原生实现二维码拉近放大

    http://www.cocoachina.com/ios/20180416/23033.html 2018-04-16 15:34 编辑: yyuuzhu 分类:iOS开发 来源:程序鹅 8 300 ...

  3. Java 学习使用常见的开源连接池

    目录 连接池介绍 自定义连接池 JDBC Tomcat Pool DBCP(DataBase Connection Pool) 使用配置文件来设置DBCP C3P0 Druid 连接池介绍 在说连接池 ...

  4. JavaScript中防止重复提交

    有这么一种情况: 页面有一个按钮,点击之后会触发Ajax请求,但是用户在点击之后,不知道是否点成功了,于是又点了一下,如果不加处理的话,就会进行两次Ajax请求,并且请求的数据都是一样的,对后端的程序 ...

  5. Ubuntu端口开放

    一.关于iptable的介绍 维基百科:https://zh.wikipedia.org/wiki/Iptables 注意:iptables的操作需要root权限 二.具体操作 sudo apt-ge ...

  6. Docker常规防止容器自动退出

    [root@server-crm /]# docker attach songheng [root@fc0a891e1861 /]# cat /bin/auto_service.sh #!/bin/s ...

  7. C#复习笔记(3)--C#2:解决C#1的问题(实现迭代器的捷径)

    实现迭代器的捷径 从这个题目上可以看到C#1实现一个迭代器模式的话是多么的痛苦,我自己也写过不下40行的代码来实现一个迭代器,C#中的迭代器模式是通过IEnumerable和他的泛型等价物IEnume ...

  8. leetcode:Roman to Integer and Integer to Roman

    2015-06-03 罗马数字以前接触过I到VIII比较多,直到遇见这个题目才知道更详细.阿拉伯数字和罗马数字之间的转换最重的是了解罗马数字的规则. 罗马数字规则:(总结) 1, 罗马数字共有7个,即 ...

  9. ArrayList性能短板深入分析

    ArrayList的数据结构主体是Object[]数组,数组对象在内存的位置是成块成块的. 1.对数组进行非尾部修改,会引发System.arrayCopy()行为.这就需要对后半部要移动的对象进行内 ...

  10. pycharm 安装

    pycharm 1.模板 file->setting->Editor->file and code template->python script->右上方 #!/usr ...