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 ...
随机推荐
- ElasticSearch聚合
前言 说完了ES的索引与检索,接着再介绍一个ES高级功能API – 聚合(Aggregations),聚合功能为ES注入了统计分析的血统,使用户在面对大数据提取统计指标时变得游刃有余.同样的工作,你在 ...
- Ubuntu18.04安装netstat
一.简介 Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Memb ...
- 封装day.js
封装day.js import dayjs from 'dayjs' import 'dayjs/locale/zh-cn' import relativeTime from 'dayjs/plugi ...
- Linux查看和注销用户(User)
Linux如何注销其他用户?_Linux教程_Linux公社-Linux系统门户网站https://www.linuxidc.com/Linux/2012-07/64939.htm linux注销指定 ...
- Notepad++快捷使用
用Notepad++写代码,要是有一些重复的代码想copy一下有木有简单的方法呢,确实还是有的不过也不算太好用.主要是应用键盘上的 Home 键 和 End 键.鼠标光标停留在一行的某处,按 Home ...
- MySQL设计SQL语句优化规范
原文:http://bbs.landingbj.com/t-0-240751-1.html 1. 使用mysql explain 对sql执行效率进行检测 ,explain显示了mysql如何使用索引 ...
- 二、npm scripts
一.执行原理 安装npm 包,会将其package.json bin 字段添加到node_modules bin 里面,创建对应的.cmd文件,因此: 例如: "scripts": ...
- MyBatis映射文件3(参数处理Map)
参数命名 POJO 如果多个参数,正好是业务逻辑的数据模型,那么我们就可以直接传入POJO,这样#{}中就可以直接使用属性名 Map 如果多个参数不是业务逻辑的数据模型,没有对应的POJO,为了方便, ...
- liunx 运维知识二部分
Windows下的目录和Linux系统下的目录有什么区别? Windows目录下的文件一般都是分区(C盘,D盘...),C盘下面有什么目录,目录下面还有其他目录加上文件. Linux系统目录结构一切都 ...
- MySQL 字段内容区分大小写
数据由Oracle 迁入MySQL ,由于之前Oracle区分大小写,MySQL的配置使用了默认配置,导致一些数据导入失败,有的唯一键报错,冲突. 将测试过程记录在下面. 数据库版本:MySQL 5. ...