背包DP
tomjobs
别慌,慌也没用
展开
-
LeetCode 638. 大礼包(分组背包)
在LeetCode商店中, 有许多在售的物品。然而,也有一些大礼包,每个大礼包以优惠的价格捆绑销售一组物品。现给定每个物品的价格,每个大礼包包含物品的清单,以及待购物品清单。请输出确切完成待购清单的最低花费。每个大礼包的由一个数组中的一组数据描述,最后一个数字代表大礼包的价格,其他数字分别表示内含的其他种类物品的数量。任意大礼包可无限次购买。示例 1:输入: [2,5], [[3,0,5],[1,2,10]], [3,2]输出: 14解释:有A和B两种物品,价格分别为¥2和¥5。大礼包1原创 2021-02-01 19:02:25 · 495 阅读 · 3 评论 -
Sumsets POJ - 2229(完全背包,划分DP)
Farmer John commanded his cows to search for different sets of numbers that sum to a given number. The cows use only numbers that are an integer power of 2. Here are the possible sets of numbers that sum to 7:1+1+1+1+1+1+11+1+1+1+1+21+1+1+2+21+1+1+41原创 2020-12-25 21:11:08 · 195 阅读 · 0 评论 -
2020ICPC 南京 Monster Hunter(树形依赖背包)
好久没写树形DP手生疏了。题意:每个点权值为hp[x]+hp[v]hp[x]+hp[v]hp[x]+hp[v],其中vvv是xxx的儿子。你可以删掉mmm个点,求对于0≤m≤n0≤m≤n0≤m≤n的每个mmm能得到的最小权值和。思路:定义dp[i][j][0/1]dp[i][j][0/1]dp[i][j][0/1]代表点iii为根子树,删掉了jjj个点,iii是否被删能得到的最小权值和。转移就是按照基本的背包转移,不再赘述。但是注意一点,转移过程中其实隐含了dp[i][j][0/1]dp[i][原创 2020-12-25 17:17:28 · 688 阅读 · 1 评论 -
Codeforces 1459D - Glass Half Spilled
There are ???? glasses on the table numbered 1,…,????. The glass ???? can hold up to ???????? units of water, and currently contains ???????? units of water.You would like to choose ???? glasses and collect as much water in them as possible. To that effec原创 2020-12-20 17:23:56 · 490 阅读 · 0 评论 -
2020小米网络赛第二场 Knapsack(背包+贪心)
思路:题解写的是因为决策单调性,所以可以斜率优化blabla。看到网上题解写的很简洁(玄学),就是把价值重量比大的优先选,选到一定范围的时候再背包DP。但是因为本题的特殊性,物品重量很小,那么价值重量比大的直接选完全是可能的,感觉上不是正解,但是算是很有启发性。#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>#include<cmath>#i原创 2020-11-08 11:40:02 · 149 阅读 · 0 评论 -
Codeforces1437 C. Chef Monocarp(DP)
Chef Monocarp has just put ???? dishes into an oven. He knows that the ????-th dish has its optimal cooking time equal to ???????? minutes.At any positive integer minute ???? Monocarp can put no more than one dish out of the oven. If the ????-th dish is p原创 2020-10-30 09:36:16 · 232 阅读 · 0 评论 -
Codeforces1433 F. Zero Remainder Sum(DP)
You are given a matrix ???? of size ????×???? consisting of integers.You can choose no more than ⌊????2⌋ elements in each row. Your task is to choose these elements in such a way that their sum is divisible by ???? and this sum is the maximum.In other wo原创 2020-10-23 18:19:04 · 282 阅读 · 0 评论 -
牛客国庆day8 PACM Team(多重背包输出方案)
链接:https://ac.nowcoder.com/acm/contest/7865/D来源:牛客网题目描述Eddy was a contestant participating in ACM ICPC contests. ACM is short for Algorithm, Coding, Math. Since in the ACM contest, the most important knowledge is about algorithm, followed by coding(impl原创 2020-10-11 21:34:51 · 252 阅读 · 0 评论 -
Installing Apps Gym - 101623I(背包+贪心)
题意:下载app,每个app有下载容量和安装容量。内存必须满足下载容量才能下载,最后所占空间为安装容量。求c的内存最多可以安装多少个app。思路:比赛的时候wzf猜了个结论,就是按照 d-s排序从大到小遍历,然后按照普通背包跑然后过了。事后想想的话,其实可以从背包转移方程看出来:dp[j−num+a[i].s]=max(dp[j−num+a[i].s],dp[j−num]+1),num=max(a[i].s,a[i].d),num≤j≤cdp[j - num + a[i].s] =max(dp[原创 2020-10-10 00:09:02 · 324 阅读 · 0 评论 -
杭电多校第10场 hdu6883 Coin Game(背包)
Problem DescriptionThere are n machines in front of you, and each of them has a cute button on it.For the i-th machine, if you push the button on it, it will give you a coin valued at ai. If you push the button again, it will give you a coin valued at bi原创 2020-09-29 18:48:43 · 275 阅读 · 0 评论 -
牛客多校第八场 Enigmatic Partition(DP)
思路:只看懂了题解的写法,还有一种差分的神仙写法实在看不懂。因为每个数字是由三个连续数字构成,然后数目不确定,所以我们枚举第一个数字l,那么就知道这三个数字是l,l+1,l+2。我们枚举l的数目和l+1的数目,或者枚举l+1和l+2的数目。因为两个l+1可以变成一个l和一个l+2,所以只要枚举两个就可以了。然后又x个l,y个l+1的时候,就能得到(y-1)/2个变换情况。x个l+2,y个l+1同理。但是小数据的时候很慢,此时直接跑完全背包就好了。#include<iostream&g.原创 2020-08-04 00:19:02 · 250 阅读 · 0 评论 -
Jewel Thief Gym - 101002H(背包DP,决策单调性)
题意:n个物品,每个物品有重量和价值。求背包体积为1~m,这m种体积每种获得的最大价值。思路:第一道决策单调性DP。介绍:https://zhuanlan.zhihu.com/p/136022523本题的n的范围很大,突破点就在于每个物品的体积(最大只有300)。先按正常背包dp,设状态f[i][j]f[i][j]f[i][j],代表选了前iii个物品,体积为jjj时的最大价值。然后写出转移方程f[i][j]=max(f[i−1][j−a[i].x∗s]+a[i].v∗s)f[i][j]=m.原创 2020-07-10 11:57:21 · 470 阅读 · 0 评论 -
BZOJ4247: 挂饰(背包)
DescriptionJOI君有N个装在手机上的挂饰,编号为1…N。 JOI君可以将其中的一些装在手机上。JOI君的挂饰有一些与众不同——其中的一些挂饰附有可以挂其他挂件的挂钩。每个挂件要么直接挂在手机上,要么挂在其他挂件的挂钩上。直接挂在手机上的挂件最多有1个。此外,每个挂件有一个安装时会获得的喜悦值,用一个整数来表示。如果JOI君很讨厌某个挂饰,那么这个挂饰的喜悦值就是一个负数。JOI...原创 2019-08-24 20:03:54 · 189 阅读 · 0 评论 -
CF1341D. Nastya and Scoreboard(DP)
Denis, after buying flowers and sweets (you will learn about this story in the next task), went to a date with Nastya to ask her to become a couple. Now, they are sitting in the cafe and finally… Deni...原创 2020-04-26 17:56:12 · 371 阅读 · 0 评论 -
2019 ICPC银川区域赛 Girls Band Party(分组背包)
You are currently playing a game called “Garupa”. In an event of the game, you are trying to get more event points. You have nn cards, each with its own name, color, and power. When you play the game,...原创 2020-03-04 13:15:35 · 641 阅读 · 0 评论 -
背包问题输出方案 ACWING 12
以ACWING 12题为例https://www.acwing.com/problem/content/description/12/方案输出问题要求从结果遍历使用了4种方法输出本题要求的是输出背包方案,并要求方案字典序最小。因此要从后往前转移,并且等于的时候也要发生转移。方案1:记录pre[i][j]代表状态(i,j)是否发生了转移。那么一旦pre[i][j]为1,就代表选择了i,就...原创 2020-02-10 20:44:53 · 737 阅读 · 0 评论 -
CD UVA - 624(背包输出方案)
You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is onCDs. You need to have it on tapes so the problem to solve is: you have a tape N minutes long. How...原创 2020-02-10 19:44:53 · 182 阅读 · 0 评论 -
Bottles CodeForces - 730J(限制性背包)
Nick has n bottles of soda left after his birthday. Each bottle is described by two values: remaining amount of soda ai and bottle volume bi (ai ≤ bi).Nick has decided to pour all remaining soda into...原创 2020-02-10 13:40:29 · 492 阅读 · 1 评论 -
Fire CodeForces - 864E(背包变形,输出方案)
Polycarp is in really serious trouble — his house is on fire! It’s time to save the most valuable items. Polycarp estimated that it would take ti seconds to save i-th item. In addition, for each item,...原创 2020-02-09 14:41:45 · 337 阅读 · 0 评论 -
Economic Difficulties CodeForces - 1263F(DP)
An electrical grid in Berland palaces consists of 2 grids: main and reserve. Wires in palaces are made of expensive material, so selling some of them would be a good idea!Each grid (main and reserve...原创 2020-01-31 16:25:55 · 348 阅读 · 0 评论 -
Relocation POJ - 2923(状压dp+背包)
Emma and Eric are moving to their new house they bought after returning from their honeymoon. Fortunately, they have a few friends helping them relocate. To move the furniture, they only have two comp...原创 2020-01-13 14:37:32 · 305 阅读 · 0 评论 -
BZOJ1042[HAOI2008]硬币购物(容斥定理+完全背包)
题目描述硬币购物一共有4种硬币。面值分别为c1,c2,c3,c4。某人去商店买东西,去了tot次。每次带di枚ci硬币,买si的价值的东西。请问每次有多少种付款方法。输入格式第一行 c1,c2,c3,c4,tot 下面tot行 d1,d2,d3,d4,s输出格式每次的方法数输入输出样例输入 #1复制1 2 5 10 23 2 3 1 101000 2 2 2 900输出 #1...原创 2020-01-11 18:25:11 · 266 阅读 · 0 评论 -
AcWing 318. 划分大理石(多重背包二进制优化)
有价值分别为1…6的大理石各a[1…6]块,现要将它们分成两部分,使得两部分价值之和相等,问是否可以实现。其中大理石的总数不超过20000。输入格式输入包含多组数据!每组数据占一行,包含6个整数,表示a[1]~a[6]。当输入为0 0 0 0 0 0时表示输入结束,且该行无需考虑。输出格式每组数据输出一个结果,每个结果占一行。如果可以实现则输出“Can”,否则输出“Can’t”输...原创 2019-10-29 18:45:47 · 373 阅读 · 0 评论 -
P1759 通天之潜水(01背包记录路径,开结构体)
题目背景直达通天路·小A历险记第三篇题目描述在猴王的帮助下,小A终于走出了这篇荒山,却发现一条波涛汹涌的河拦在了自己的面前。河面上并没有船,但好在小A有n个潜水工具。由于他还要背重重的背包,所以他只能背m重的工具,又因为他的力气并不是无限的,河却很宽,所以他只能背有v阻力的工具。但是这条河下有非常重要的数据,所以他希望能够停留的时间最久。于是他找到了你,让你告诉他方案。输入格式三个数m,...原创 2019-08-22 19:21:08 · 171 阅读 · 0 评论 -
P1757 通天之分组背包(分组背包裸题)
题目背景直达通天路·小A历险记第二篇题目描述自01背包问世之后,小A对此深感兴趣。一天,小A去远游,却发现他的背包不同于01背包,他的物品大致可分为k组,每组中的物品相互冲突,现在,他想知道最大的利用价值是多少。输入格式两个数m,n,表示一共有n件物品,总重量为m接下来n行,每行3个数ai,bi,ci,表示物品的重量,利用价值,所属组数输出格式一个数,最大的利用价值输入输出样例...原创 2019-08-22 19:37:53 · 394 阅读 · 0 评论 -
Jin Ge Jin Qu hao UVA - 12563(背包DP)
(If you smiled when you see the title, this problem is for you _)For those who don’t know KTV, see: http://en.wikipedia.org/wiki/Karaoke_boxThere is one very popular song called Jin Ge Jin Qu(). It ...原创 2019-08-11 16:51:59 · 135 阅读 · 0 评论 -
Charm Bracelet POJ - 3624(01背包)
Bessie has gone to the mall’s jewelry store and spies a charm bracelet. Of course, she’d like to fill it with the best charms possible from the N (1 ≤ N ≤ 3,402) available charms. Each charm i in the ...原创 2019-08-10 22:12:08 · 141 阅读 · 0 评论 -
I.点进来吧,这里有你想要的(01背包)
Description住在云塘校区真是太可怜了,荒郊野外,方圆一公里都没有好吃的,一般都要地铁出去唯一幸运的是,西门中信和东门去德思勤还是有好东西吃的,比如汉堡王,金村漫漫暑训和寒假回家的时候根本就吃不到 QAQ汉堡王长这样:1.png 支持正版,盗版都不好吃,盗版茶颜难喝同理汉堡王有很多好吃的小食,最好吃的当然是超大一根的薯霸王 2.png香脆可口的王道椒香鸡腿3.png 和王道川蜀鸡翅...原创 2019-03-27 22:18:00 · 204 阅读 · 0 评论