传送门 - > \(CF816E\) Karen and Supermarket

题意翻译

在回家的路上,凯伦决定到超市停下来买一些杂货。 她需要买很多东西,但因为她是学生,所以她的预算仍然很有限。

事实上,她只花了b美元。

超市出售N种商品。第i件商品可以以ci美元的价格购买。当然,每件商品只能买一次。

最近,超市一直在努力促销。凯伦作为一个忠实的客户,收到了n张优惠券。

如果凯伦购买i次商品,她可以用i次优惠券降低di美元。 当然,不买对应的商品,优惠券不能使用。

然而,对于优惠券有一个规则。对于所有i>=2,为了使用i张优惠券,凯伦必须也使用第xi张优惠券 (这可能意味着使用更多优惠券来满足需求。)

凯伦想知道。她能在不超过预算B的情况下购买的最大商品数量是多少?

感谢@luv_letters 提供的翻译

题目描述

On the way home, Karen decided to stop by the supermarket to buy some groceries.

She needs to buy a lot of goods, but since she is a student her budget is still quite limited. In fact, she can only spend up to bb dollars.

The supermarket sells nn goods. The ii -th good can be bought for c_{i}ci​ dollars. Of course, each good can only be bought once.

Lately, the supermarket has been trying to increase its business. Karen, being a loyal customer, was given nncoupons. If Karen purchases the ii -th good, she can use the ii -th coupon to decrease its price by d_{i}di​ . Of course, a coupon cannot be used without buying the corresponding good.

There is, however, a constraint with the coupons. For all i>=2i>=2 , in order to use the ii -th coupon, Karen must also use the x_{i}xi​ -th coupon (which may mean using even more coupons to satisfy the requirement for that coupon).

Karen wants to know the following. What is the maximum number of goods she can buy, without exceeding her budget bb ?

输入输出格式

输入格式:

The first line of input contains two integers nn and bb ( 1<=n<=50001<=n<=5000 , 1<=b<=10^{9}1<=b<=109 ), the number of goods in the store and the amount of money Karen has, respectively.

The next nn lines describe the items. Specifically:

  • The ii -th line among these starts with two integers, c_{i}ci​ and d_{i}di​ ( 1<=d_{i}<c_{i}<=10^{9}1<=di​<ci​<=109 ), the price of the ii -th good and the discount when using the coupon for the ii -th good, respectively.
  • If i>=2i>=2 , this is followed by another integer, x_{i}xi​ ( 1<=x_{i}<i1<=xi​<i ), denoting that the x_{i}xi​ -th coupon must also be used before this coupon can be used.

输出格式:

Output a single integer on a line by itself, the number of different goods Karen can buy, without exceeding her budget.

输入输出样例

输入样例#1:

6 16

10 9

10 5 1

12 2 1

20 18 3

10 2 3

2 1 5

输出样例#1:

4

输入样例#2:

5 10

3 1

3 1 1

3 1 2

3 1 3

3 1 4

输出样例#2:

5

说明

In the first test case, Karen can purchase the following 44 items:

  • Use the first coupon to buy the first item for 10-9=110−9=1 dollar.
  • Use the third coupon to buy the third item for 12-2=1012−2=10 dollars.
  • Use the fourth coupon to buy the fourth item for 20-18=220−18=2 dollars.
  • Buy the sixth item for 22 dollars.

The total cost of these goods is 1515 , which falls within her budget. Note, for example, that she cannot use the coupon on the sixth item, because then she should have also used the fifth coupon to buy the fifth item, which she did not do here.

In the second test case, Karen has enough money to use all the coupons and purchase everything.

题解

一眼看出树形dp,但是又不知道怎么打,所以考场打了个top序dp后来被证明是错的

知道了是树形dp后怎么定义状态呢?我们看到题目中有一句话

为了使用i张优惠券,凯伦必须也使用第xi张优惠券

很多人可能会这么定义dp数组,\(dp[i][j][0]\)表示第i个节点已经选了j个,本节点不选的最小花费,\(dp[i][j][1]\)就是本节点选的最小花费

可是这样并不能保证本节点选了就一定可以优惠,也就是说是有后效性的

所以我们转换一下思路,\(dp[i][j][0]\)表示第\(i\)个节点已经选了j个本节点无优惠的最小花费,同理知道\(dp[i][j][1]\)的定义

那么就很好转移了,如下

\[dp[u][j+k][1]=min(dp[u][j+k][1],dp[u][j][1]+dp[son[u]][k][0])
\]

\[dp[u][j+k][1]=min(dp[u][j+k][1],dp[u][j][1]+dp[son[u]][k][1])
\]

\[dp[u][j+k][0]=min(dp[u][j+k][0],dp[u][j][0]+dp[son[u]][k][0])
\]

设\(w[i]\)为\(i\)物品的原价,\(v[i]\)为优惠后的价格

至于初始值,\(dp[u][1][0]=w[u],dp[u][1][1]=v[u],dp[u][0][0]=0\)

注意:这道题不能处理出叶子节点的个数,再\(dp\),会T,要一边dfs一边dp

#include<bits/stdc++.h>
#define Max(a,b) (a)>(b)?(a):(b)
#define Min(a,b) (a)<(b)?(a):(b)
#define in(i) (i=read())
using namespace std;
int read() {
int ans=0,f=1; char i=getchar();
while(i<'0' || i>'9') {if(i=='-') f=-1; i=getchar();}
while(i>='0' && i<='9') {ans=(ans<<1)+(ans<<3)+(i^48); i=getchar();}
return ans*f;
}
int n,s,cnt;
struct node {
int v,w;
}a[5010];
struct edge {
int to,next;
}e[10010];
int head[5010],son[5010];
int dp[5010][5010][2];
void add(int a,int b) {
e[++cnt].to=b;
e[cnt].next=head[a];
head[a]=cnt;
}
void dfs(int u) {
son[u]=1; dp[u][1][0]=a[u].w;
dp[u][1][1]=a[u].v; dp[u][0][0]=0;
for(int i=head[u];i;i=e[i].next) {
int to=e[i].to; dfs(to);
for(int j=son[u];j>=0;--j)//倒序枚举,0/1背包正常枚举方式
for(int k=0;k<=son[to];++k) {
dp[u][j+k][0]=Min(dp[u][j+k][0],dp[u][j][0]+dp[to][k][0]);
dp[u][j+k][1]=Min(dp[u][j+k][1],dp[u][j][1]+dp[to][k][1]);
dp[u][j+k][1]=Min(dp[u][j+k][1],dp[u][j][1]+dp[to][k][0]);
}
son[u]+=son[to];
}
}
int main()
{
//freopen("shopping.in","r",stdin);
//freopen("shopping.out","w",stdout);
in(n); in(s);
memset(dp,0x3f,sizeof(dp));
in(a[1].w); in(a[1].v);
a[1].v=a[1].w-a[1].v;
for(int i=2;i<=n;++i) {
in(a[i].w); in(a[i].v);
a[i].v=a[i].w-a[i].v;
int fa; in(fa); add(fa,i);
} dfs(1);
for(int i=n;i>=0;--i)
if(dp[1][i][0]<=s || dp[1][i][1]<=s)
printf("%d\n",i),exit(0);
}

博主蒟蒻,随意转载.但必须附上原文链接

http://www.cnblogs.com/real-l/

[CF816E] Karen and Supermarket1 [树形dp]的更多相关文章

  1. Codeforces 815C Karen and Supermarket 树形dp

    Karen and Supermarket 感觉就是很普通的树形dp. dp[ i ][ 0 ][ u ]表示在 i 这棵子树中选择 u 个且 i 不用优惠券的最小花费. dp[ i ][ 1 ][ ...

  2. CF815C Karen and Supermarket [树形DP]

    题目传送门 Karen and Supermarket On the way home, Karen decided to stop by the supermarket to buy some gr ...

  3. 816E. Karen and Supermarket 树形DP

    LINK 题意:给出n个商品,除第一个商品外,所有商品可以选择使用优惠券,但要求其前驱商品已被购买,问消费k以下能买几个不同的商品 思路:题意很明显就是树形DP.对于一个商品有三种选择,买且使用优惠券 ...

  4. Codeforces Round #419 (Div. 1) C. Karen and Supermarket 树形DP

    C. Karen and Supermarket     On the way home, Karen decided to stop by the supermarket to buy some g ...

  5. Codeforces Round #419 (Div. 2) E. Karen and Supermarket(树形dp)

    http://codeforces.com/contest/816/problem/E 题意: 去超市买东西,共有m块钱,每件商品有优惠卷可用,前提是xi商品的优惠券被用.问最多能买多少件商品? 思路 ...

  6. codeforces 816 E. Karen and Supermarket(树形dp)

    题目链接:http://codeforces.com/contest/816/problem/E 题意:有n件商品,每件有价格ci,优惠券di,对于i>=2,使用di的条件为:xi的优惠券需要被 ...

  7. CodeForces 816E Karen and Supermarket ——(树形DP)

    题意:有n件商品,每件商品都最多只能被买一次,且有一个原价和一个如果使用优惠券以后可以减少的价格,同时,除了第一件商品以外每件商品都有一个xi属性,表示买这个商品时如果要使用优惠券必须已经使用了xi的 ...

  8. poj3417 LCA + 树形dp

    Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4478   Accepted: 1292 Descripti ...

  9. COGS 2532. [HZOI 2016]树之美 树形dp

    可以发现这道题的数据范围有些奇怪,为毛n辣么大,而k只有10 我们从树形dp的角度来考虑这个问题. 如果我们设f[x][k]表示与x距离为k的点的数量,那么我们可以O(1)回答一个询问 可是这样的话d ...

随机推荐

  1. python函数(2017-8-2)

    1. def 函数名(形式参数) 函数体 return "123" 函数执行了return之后就不再执行下面的代码 2. 默认形参实参的位置一一对应 如果要调整位置,指定形参名字 ...

  2. Grok Debugger本地安装(转载)

    原文链接:http://fengwan.blog.51cto.com/508652/1758845 最近在使用ELK对日志进行集中管理,因为涉及到日志的规则经常要用到http://grokdebug. ...

  3. WPF中,如何将绑定源设置到单件实例

    原文:WPF中,如何将绑定源设置到单件实例  WPF中,如何将绑定源设置到单件实例                                       周银辉 大概两个月前,曾有位朋友问我:如 ...

  4. python内置模块[sys,os,os.path,stat]

    python内置模块[sys,os,os.path,stat] 内置模块是python自带功能,在使用内置模块时,需要遵循 先导入在 使用 一.sys 对象 描述 sys.argv 命令行参数获取,返 ...

  5. 【廖雪峰老师python教程】——filter/sorted

    filter Python内建的filter()函数用于过滤序列. 和map()类似,filter()也接收一个函数和一个序列.和map()不同的是,filter()把传入的函数依次作用于每个元素,然 ...

  6. python 基础篇 14 程程器表达式 内置函数

    昨日内容回顾    可迭代对象:        内部含有__iter__方法的就是可迭代对象.        可迭代对象不能取值,因为内部不含有__next__方法.     可迭代对象 ---> ...

  7. Laxcus大数据分布计算演示实例

    Laxcus大数据管理系统提供了基于Diffuse/Converge分布算法的计算能力.算法的具体介绍详见<Laxcus:大数据处理系统>一文.本图展示了在集群环境下的随机数产生.排序.显 ...

  8. PAT——乙级1036:跟奥巴马一起编程 &乙级1027:打印沙漏 (有坑)

    乙级1036 1036 跟奥巴马一起编程 (15 point(s)) 美国总统奥巴马不仅呼吁所有人都学习编程,甚至以身作则编写代码,成为美国历史上首位编写计算机代码的总统.2014 年底,为庆祝“计算 ...

  9. java的命名空间

    这个package  me.gacl.websocket相当于.net中的namespace命名空间. import  相当于.net中的using,引用命名空间:

  10. LTE QOS

    http://wenku.baidu.com/link?url=ziFIkdKaC7MU2RY-bTOp2bt87WFPw5_02bqmYs5W6w4ktOfPHEcWesK1U2T7YiyXjVSM ...