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. ...
随机推荐
- 一本通1166 求f(x,n)
[题目描述] 已知 计算x=4.2,n=1以及x=2.5,n=15时f的值. [输入] 输入x和n. [输出] 函数值,保留两位小数. [输入样例] 4.2 10 [输出样例] 3.68 1.看见这种 ...
- 【数据结构】二叉树的遍历(前、中、后序及层次遍历)及leetcode107题python实现
文章目录 二叉树及遍历 二叉树概念 二叉树的遍历及python实现 二叉树的遍历 python实现 leetcode107题python实现 题目描述 python实现 二叉树及遍历 二叉树概念 二叉 ...
- Swift之分割视图控制器-UISplitViewController
Swift之分割视图控制器-UISplitViewController UISplitViewController这种控制器只能用于iPad,它可以在iPad屏幕中显示两个不同的场景:在横向模式下,左 ...
- spring boot集成mybatis(3) - mybatis generator 配置
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
- react动态生成列表
在组件的render函数中遍历数组menus[]并且要return返回虚拟Dom对象. render() { return createPortal( this.state.visible & ...
- DES与MD5加密
using System; using System.Data; using System.Configuration; using System.Web; using System.Security ...
- Servlet基础(java,idea)
IDEA的Tomcat配置Web的项目创建以及Servlet简单运行 登录测试 用到jdbc的知识 在Customer的DAO层实现类中实现检查用户登录方法 package impl; import ...
- Python logging模块 控制台、文件输出
步骤 导入logging模块 设置level(此处是DEBUG) 添加文件handler和流handler import logging logger=logging.getLogger(__name ...
- 2020/2/4 PHP代码审计之会话认证漏洞
0x00 会话认证漏洞简介 会话认证是个非常大的话题,涉及各种协议和框架,如cookie.session.sso.oauth.openid等. 而其中最常使用的是Cookie和Session,他们都能 ...
- promise核心技术 1 实例对象/函数对象
一个程序员要在看到代码的语法同时判断数据类型 知道语法是基础 基础才能延伸功能 //一行代码 a()[0]() // a() 首先推断出a是一个函数 //a()[0] 判断a函数的返回值是一个数组 ...