Codeforces Round #530 (Div. 2)F Cookies (树形dp+线段树)
题:https://codeforces.com/contest/1099/problem/F
题意:给定一个树,每个节点有俩个信息x和t,分别表示这个节点上的饼干个数和先手吃掉这个节点上一个饼干的的时间。然后有先手和后手俩个人。
◉先手可以这么操作:在规定总时间T到达某个节点然后一定要返回根节点1,期间可以选择吃掉某些节点上的某些饼干(前提是保证剩下的时间能够回到根节点);
◉后手可以这么操作:在先手到达的位置和这个位置的孩子之间的连边选择一条让先手吃得更多的边摧毁掉,也可以跳过这个过程;
问:在给定的总时间T内,先手最多能吃到多少的饼干。
分析:对于每个节点都有3个选择,我们记为dp1,dp2,dp3;
dp1:从当前位置上去能吃到的最多的饼干;
dp2:从当前位置沿孩子下去能吃到的最多的饼干数;(这一步只在当前位置为根节点的时候起作用,因为是先手,所以可以直接选最大)
dp3:从当前位置沿孩子下去能吃到的次多的饼干数;(因为后手会阻止先手吃最大的饼干数,所以就选次大)
dfs处理这棵树,回溯的时候做dp2,dp3的计算即可;
然后饼干数的最大和次大我们把节点信息的x和t,全部转化为时间消耗,x*t对应吃了x个饼干,然后二分出答案即可。
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define lson root<<1,l,midd
#define rson root<<1|1,midd+1,r
typedef long long ll;
const int M=1e6+;
ll x[M],t[M];
struct node{
int u;
ll w;
};
vector<node>g[M];
struct Node{
ll ti,num;
}tree[M<<];
int n;
void update(ll num,ll ti,ll root,ll l,ll r){
if(l==r){
tree[root].ti+=num*ti;
tree[root].num+=num;
return ;
}
int midd=(l+r)>>;
if(ti<=midd)
update(num,ti,lson);
else
update(num,ti,rson);
tree[root].ti=tree[root<<].ti+tree[root<<|].ti;
tree[root].num=tree[root<<].num+tree[root<<|].num;
}
ll query(ll nowti,ll root,ll l,ll r){
if(tree[root].ti<=nowti)
return tree[root].num;
if(l==r)
return nowti/l;
int midd=(l+r)>>;
if(tree[root<<].ti>=nowti)
return query(nowti,lson);
else
return query(nowti-tree[root<<].ti,rson)+tree[root<<].num;
}
ll dfs(int u,ll nowt){
//cout<<u<<endl;
update(x[u],t[u],,,);
ll dp1=query(nowt,,,);
ll dp2=,dp3=;
for(int i=;i<g[u].size();i++){
node v=g[u][i];
if(nowt<=*v.w)
continue;
ll temp=dfs(v.u,nowt-2ll*v.w);
if(temp>dp2)
dp3=dp2,dp2=temp;
else if(dp3<temp)
dp3=temp;
}
update(-x[u],t[u],,,);
if(u==)///根节点,先走,所以不用考虑次大
return max(dp1,dp2);
else
return max(dp1,dp3);
}
int main(){
ll T;
scanf("%d%I64d",&n,&T);
for(int i=;i<=n;i++)
scanf("%I64d",&x[i]);
for(int i=;i<=n;i++)
scanf("%I64d",&t[i]);
for(int i=;i<=n;i++){
ll w;
int u;
scanf("%d%I64d",&u,&w);
g[u].pb(node{i,w});
}
printf("%I64d",dfs(,T));
return ;
}
Codeforces Round #530 (Div. 2)F Cookies (树形dp+线段树)的更多相关文章
- Codeforces Round #530 (Div. 2) F - Cookies
F - Cookies 思路:我们先考虑如何算出在每个节点结束最多能吃多少饼干, 这个dfs的时候用线段树维护一下就好了, 然后有个这个信息之后树上小dp一下就好啦. #include<bits ...
- Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树)
Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树) 题目链接 题意 给定一个nm的矩阵,每行取2k的矩阵,求总 ...
- Codeforces Round #530 (Div. 2) F (树形dp+线段树)
F. Cookies 链接:http://codeforces.com/contest/1099/problem/F 题意: 给你一棵树,树上有n个节点,每个节点上有ai块饼干,在这个节点上的每块饼干 ...
- Codeforces Round #530 (Div. 2) F 线段树 + 树形dp(自下往上)
https://codeforces.com/contest/1099/problem/F 题意 一颗n个节点的树上,每个点都有\(x[i]\)个饼干,然后在i节点上吃一个饼干的时间是\(t[i]\) ...
- Codeforces Round #277 (Div. 2)D(树形DP计数类)
D. Valid Sets time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)
题目链接:http://codeforces.com/contest/610/problem/D 就是给你宽度为1的n个线段,然你求总共有多少单位的长度. 相当于用线段树求面积并,只不过宽为1,注意y ...
- Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)
题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...
- Codeforces Round #590 (Div. 3) D. Distinct Characters Queries(线段树, 位运算)
链接: https://codeforces.com/contest/1234/problem/D 题意: You are given a string s consisting of lowerca ...
- Codeforces Round #292 (Div. 1) C. Drazil and Park 线段树
C. Drazil and Park 题目连接: http://codeforces.com/contest/516/problem/C Description Drazil is a monkey. ...
随机推荐
- 【pwnable.kr】leg
pwnable从入门到放弃第八题. Download : http://pwnable.kr/bin/leg.cDownload : http://pwnable.kr/bin/leg.asm ssh ...
- BZOJ:2242: [SDOI2011]计算器
题解:BSGS 问题:map空间 BSGS判无解 a%p!=0 0与最小非负整数 有区别 函数传参类型转换int->long long long long ->int; 费马小定理充分必要 ...
- Vulkan 开发学习资料汇总
开发资料汇总 1.API Reference 2.Vulkan Spec 有详细说明的pdf 文章 1.知乎Vulkan-高性能渲染 2.Life of a triangle - NVIDIA's l ...
- 【STM32H7教程】第48章 STM32H7的FMC总线应用之是32路高速IO扩展
完整教程下载地址:http://www.armbbs.cn/forum.php?mod=viewthread&tid=86980 第48章 STM32H7的FMC总线应用之是32路 ...
- DES与MD5加密
using System; using System.Data; using System.Configuration; using System.Web; using System.Security ...
- 一天一个设计模式——Composite组合模式
一.模式说明 能够使容器与内容物具有一致性,创造出递归结构的模式就是Composite组合模式. 举个例子:计算机中的文件系统中有文件和文件夹的概念,我们知道,文件夹可以包含文件,也可以包含子文件夹, ...
- redis(六)---- 简单延迟队列
延迟队列的应用场景也很常见,例如:session的超时过期.自动取消未付款订单等等.redis中有一种数据结构叫做zset,即有序集合.元素类型为String类型,且元素具有唯一性不能重复,每个元素可 ...
- 基于图灵api的Python机器人
一.注册图灵机器人 先注册并登录图灵机器人官网: 点击创建机器人 复制机器人的key 二.搭建Python机器人 Python版本:3.6 注意替换第三行代码的apikey import reques ...
- C基础 带你手写 redis ae 事件驱动模型
引言 - 整体认识 redis ae 事件驱动模型, 网上聊得很多. 但当你仔细看完一篇又一篇之后, 可能你看的很舒服, 但对于 作者为什么要这么写, 出发点, 好处, 缺点 ... 可能还是好模糊, ...
- go语言使用
设置 package control 在 Preferences->Package Setting->Package Control->Settings - User 中加入 cha ...