Codeforces Round #530 (Div. 2) F (树形dp+线段树)
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+线段树)的更多相关文章
- Codeforces Round #587 (Div. 3) F Wi-Fi(线段树+dp)
题意:给定一个字符串s 现在让你用最小的花费 覆盖所有区间 思路:dp[i]表示前i个全覆盖以后的花费 如果是0 我们只能直接加上当前位置的权值 否则 我们可以区间询问一下最小值 然后更新 #incl ...
- 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 ...
- Codeforces Round #530 (Div. 2) F 线段树 + 树形dp(自下往上)
https://codeforces.com/contest/1099/problem/F 题意 一颗n个节点的树上,每个点都有\(x[i]\)个饼干,然后在i节点上吃一个饼干的时间是\(t[i]\) ...
- Codeforces Round #530 (Div. 2)F Cookies (树形dp+线段树)
题:https://codeforces.com/contest/1099/problem/F 题意:给定一个树,每个节点有俩个信息x和t,分别表示这个节点上的饼干个数和先手吃掉这个节点上一个饼干的的 ...
- Codeforces Round #530 (Div. 2) F - Cookies
F - Cookies 思路:我们先考虑如何算出在每个节点结束最多能吃多少饼干, 这个dfs的时候用线段树维护一下就好了, 然后有个这个信息之后树上小dp一下就好啦. #include<bits ...
- 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 ...
- Codeforces 671D. Roads in Yusland(树形DP+线段树)
调了半天居然还能是线段树写错了,药丸 这题大概是类似一个树形DP的东西.设$dp[i]$为修完i这棵子树的最小代价,假设当前点为$x$,但是转移的时候我们不知道子节点到底有没有一条越过$x$的路.如果 ...
- 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 ...
- Codeforces Round #603 (Div. 2) E. Editor(线段树)
链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...
随机推荐
- shell 小工具
1.打印进度条(待完善) #!/bin/sh printf -- 'Performing asynchronous action..'; DONE=; printf -- '............. ...
- Docker镜像的修改和自定义
一.docker镜像的更新 (1)启动镜像,写入一些文件或者更新软件 docker run -it 3afd47092a0e[root@44652ba46352 /]# ls (2)更新镜像 dock ...
- Feel Good POJ - 2796 (前缀和+单调栈)(详解)
Bill is developing a new mathematical theory for human emotions. His recent investigations are dedic ...
- mysql触发器,视图,游标
什么事触发器: 触发器是一中特殊的存储过程,主要是通过事件来触发而被执行的.它可以强化约束,来维护数据的完整性和一致性,可以跟踪数据库内的操作从而不允许未经许可的更新和变化.可以联级运算.如,某表上的 ...
- 使用Request+正则抓取猫眼电影(常见问题)
目前使用Request+正则表达式,爬取猫眼电影top100的例子很多,就不再具体阐述过程! 完整代码github:https://github.com/connordb/Top-100 总结一下,容 ...
- Django之Django终端打印SQL语句
Django之Django终端打印SQL语句 在Django项目中,settings.py文件中,在最后添加如下代码即可实现在Django终端打印SQL语句. LOGGING = { 'version ...
- Golang开发工具LiteIDE使用方法整理
安装 参考github的说明 添加GOPATH 创建workspace 创建新文件 运行程序 Liteide中运行程序有两种方式: FR(FileRun)是编译并运行单个文件,可以使用Shift + ...
- Python3练习题 001:4个数字求不重复的3位数
#Python练习题 001:4个数字求不重复的3位数#方法一import itertoolsres = [][res.append(i[0]*100 + i[1]*10 + i[2]) for i ...
- Azure系列2.1.10 —— CloudBlobClient
(小弟自学Azure,文中有不正确之处,请路过各位大神指正.) 网上azure的资料较少,尤其是API,全是英文的,中文资料更是少之又少.这次由于公司项目需要使用Azure,所以对Azure的一些学习 ...
- Jenkins系统上的时间不正确问题
很简单,点击系统管理,选择执行脚本命令: 打开 [系统管理]->[脚本命令行]运行下面的命令 System.setProperty('org.apache.commons.jelly.tags. ...