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. ...
随机推荐
- 【分类问题中模型的性能度量(二)】超强整理,超详细解析,一文彻底搞懂ROC、AUC
文章目录 1.背景 2.ROC曲线 2.1 ROC名称溯源(选看) 2.2 ROC曲线的绘制 3.AUC(Area Under ROC Curve) 3.1 AUC来历 3.2 AUC几何意义 3.3 ...
- [题解] LuoguP4841 [集训队作业2013]城市规划
Description 求\(n\)个点无重边.无自环.带标号的无向联通图个数,对\(1004535809\)(\(479 \times 2^{21} + 1\))取模.\(n \le 130000\ ...
- linux部分环境搭建
连接数据库: mysql -hrm-uf6b23117l3s5t69zjo.mysql.rds.aliyuncs.com -uwswl -pwswl@2019 显示用户: show databases ...
- PAT Advanced 1143 Lowest Common Ancestor (30) [二叉查找树 LCA]
题目 The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both ...
- Tensorflow学习教程------普通神经网络对mnist数据集分类
首先是不含隐层的神经网络, 输入层是784个神经元 输出层是10个神经元 代码如下 #coding:utf-8 import tensorflow as tf from tensorflow.exam ...
- 洛谷 P5664 Emiya 家今天的饭(84分)
题目传送门 解题思路: 对于每一个列c,f[i][j][k]表示到第i行,第c列选了j个,其它列一共选了k个,然后我们读题意发现只要j>k,那就一定是不合法的,然后统计所有方案,减去所有不合法方 ...
- 洛谷 P5661 公交换乘(队列)
题目传送门 解题思路: 暴力模拟. AC代码: #include<iostream> #include<cstdio> #include<queue> using ...
- 经典线段树 UVALive 3938/UVA 1400
题意:就是相当于动规里面的求最大连续子串,不同的是,这里需要读入一个区间x,y,输出的区间 a,b 且x<=a<=b<=y,使得a b的连续子串最长,而且询问次数达到了10的五次方. ...
- java实现接口导出csv文件
Tomxin7 Simple, Interesting | 简单,有趣 业务介绍 项目要求从数据库中查询出相关数据后,通过表格展示给用户,如果用户需要,可以点击导出按钮,导出数据为csv格式. 开发环 ...
- HTML 回到顶部 浮动
回到顶部 <div id="FloatDIV" style="position: absolute; top: 0px; z-index: 9999; backgr ...