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. ...
随机推荐
- 深入理解class
一, class和自定义类型的区别: 1,类声明不会被提升. 2,类声明的代码自动运行在严格模式. 3,类的所有方法都是不可枚举的,而自定的方法必须使用Object.defineProperty来设置 ...
- 原生js完成打地鼠小游戏
:这是首页,有简单模式和地狱模式两种模式进行选择 这是选择完模式之后的游戏界面:30秒一局游戏倒计时,每打中一只老鼠加一分,没砸中减一分,没砸不加不减 首先准备几张图片 html代码: <!-- ...
- JS高级学习笔记(6)- 事件循环
参考文章:深入理解JS引擎的执行机制 JavaScript 异步.栈.事件循环.任务队列 我的笔记:ES系列之Promise async 和 await Event Loop 前提 js ...
- [题解] Luogu P4721 【模板】分治 FFT
分治FFT的板子为什么要求逆呢 传送门 这个想法有点\(cdq\)啊,就是考虑分治,在算一段区间的时候,我们把他分成两个一样的区间,然后先做左区间的,算完过后把左区间和\(g\)卷积一下,这样就可以算 ...
- Centos7.4系统 httpd模式搭建文件服务器
环境:服务环境:centos7.4 说明:搭建Apache文件服务器,下载路径为/opt/ymyg(下载路径根据实际需要自己定义) 步骤: 1.安装httpd服务 [root@localhost ...
- 冒泡排序_python
def popdata(ls): for i in range(len(ls)): for j in range(i+1,len(ls)): if ls[i]>ls[j]: # tmp=ls[i ...
- 18 12 23 html 基本学习
一个html的基本结构如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...
- 12 react 基础 的 css 过渡动画 及 动画效果 及 使用 react-transition-group 实现动画
一. 过渡动画 # index.js import React from 'react';import ReactDOM from 'react-dom';import App from './app ...
- 强大的代码生成器——T4模板
T4 Editor工具下载地址 tangible T4 Editor 2.5.0 plus modeling tools for VS 2019 https://marketplace.visuals ...
- 期末项目之 Json文件
Github上的json文件: https://raw.githubusercontent.com/color-me/first/master/b