链接:https://ac.nowcoder.com/acm/contest/1077/B
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
Farmer John’s cows love their video games! FJ noticed that after playing these games that his cows produced much more milk than usual, surely because contented cows make more milk.
The cows disagree, though, on which is the best game console. One cow wanted to buy the Xbox 360 to play Halo 3; another wanted to buy the Nintendo Wii to play Super Smash Brothers Brawl; a third wanted to play Metal Gear Solid 4 on the PlayStation 3. FJ wants to purchase the set of game consoles (no more than one each) and games (no more than one each – and within the constraints of a given budget) that helps his cows produce the most milk and thus nourish the most children.
FJ researched N (1 <= N <= 50) consoles, each with a console price Pi (1 <= Pi <= 1000) and a number of console-specific games Gi (1 <= Gi <= 10). A cow must, of course, own a console before she can buy any game that is specific to that console. Each individual game has a game price GPj (1 <= GPj price <= 100) and a production value (1 <= PVj <= 1,000,000), which indicates how much milk a cow will produce after playing the game. Lastly, Farmer John has a budget V (1 <= V <= 100,000) which is the maximum amount of money he can spend. Help him maximize the sum of the production values of the games he buys.
Consider one dataset with N=3 consoles and a V=$800 budget. The first console costs $300 and has 2 games with cost $30 and $25 and production values as shown:
Game # Cost Production Value
1 $30 50
2 $25 80

The second console costs $600 and has only 1 game:
Game # Cost Production Value
1 $50 130

The third console costs $400 and has 3 games:
Game # Cost Production Value
1 $40 70
2 $30 40
3 $35 60

Farmer John should buy consoles 1 and 3, game 2 for console 1, and games 1 and 3 for console 3 to maximize his expected production at 210:
Production Value
Budget: $800
Console 1 -$300
Game 2 -$25 80
Console 3 -$400
Game 1 -$40 70
Game 3 -$35 60
-------------------------------------------
Total: 0 (>= 0) 210
输入描述:

  • Line 1: Two space-separated integers: N and V

  • Lines 2…N+1: Line i+1 describes the price of and the games ?available for console i; it contains: Pi, Gi, and Gi pairs of space-separated integers GPj, PVj
    输出描述:

  • Line 1: The maximum production value that Farmer John can get with his budget.
    示例1
    输入
    复制

    3 800
    300 2 30 50 25 80
    600 1 50 130
    400 3 40 70 30 40 35 60

输出
复制

210

/*
有依赖的背包:
先把一组看成一个整体,对每组做01背包
然后再对每组里面的物品做01背包

*/
代码1:

#include <bits/stdc++.h>using namespace std;
const int MAXN = 1e5+5;
int dp[52][MAXN];
int main()
{int N,W;cin>>N>>W;int wi,n,w,v;for(int i = 1; i <= N; i++){cin>>wi>>n;for(int j = wi; j <= W; j++)//假设要选这组物品,买这个盒子,获得收益为0{dp[i][j] = dp[i-1][j-wi] + 0;}while(n--){cin>>w>>v;for(int j = W; j>= w+wi; j--)//此时买了盒子,里面的东西可以随便选{dp[i][j] = max(dp[i][j],dp[i][j-w]+v);}}for(int j = 0; j <= W; j++) //也可以不选这组物品{dp[i][j] = max(dp[i][j],dp[i-1][j]);}}cout<<dp[N][W]<<endl;return 0;
}

代码2:
滚动数组空间优化,AC_code:

#include <bits/stdc++.h>using namespace std;
const int MAXN = 1e5+5;
int dp[2][MAXN];
int main()
{int N,W;cin>>N>>W;int wi,n,w,v;for(int i = 1; i <= N; i++){cin>>wi>>n;for(int j = wi; j <= W; j++)//假设要选这组物品{dp[i&1][j] = dp[(i-1)&1][j-wi] + 0;}while(n--){cin>>w>>v;for(int j = W; j>= w+wi; j--) //因为买了这个盒子,里面的东西可以随便选{dp[i&1][j] = max(dp[i&1][j],dp[i&1][j-w]+v);}}for(int j = 0; j <= W; j++) //也可以不选这组物品{dp[i&1][j] = max(dp[i&1][j],dp[(i-1)&1][j]);}}cout<<dp[N&1][W]<<endl;return 0;
}

Video Game Troubles(有依赖的背包)相关推荐

  1. 2165: 黄金矿工(有依赖的背包转化为分组背包)

    Description Input 3 10 1 1 1 1 2 2 2 2 1 3 15 9 Output 3 HINT -----sample2------ 1 1 13 1 2 2 2 2 1 ...

  2. ssl1056-金明的预算方案【dp之有依赖的背包】

    这道题卡了挺久的QAQ现在才搞定 Description 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:"你的房间需要购 ...

  3. 【洛谷P2967】【USACO 2009 Dec】电子游戏 Video Game Troubles

    问题描述 约翰的奶牛们玩游戏成瘾!本来约翰是想把她们拖去电击治疗的,后来他发现奶牛们在生产了更多的牛奶,也就开始支持她们了. 但是,奶牛在选择游戏平台上的分歧很大:有的奶牛想买一台 Xbox 360来 ...

  4. BNUOJ 4140 Video Game Troubles

    动态规划问题.方程有很多种.还是NOWCO上面的那个感觉好理解一些. 把每组游戏机提供的游戏,看成是一件物品,对每组内的物品进行01背包,也就是拿和不拿的决策.然后对于n组游戏机,每组游戏机和其提供的 ...

  5. [USACO09DEC]视频游戏的麻烦Video Game Troubles(DP)

    https://www.luogu.org/problem/P2967 https://ac.nowcoder.com/acm/contest/1077/B 题目描述 Farmer John's co ...

  6. AWS KVS(Kinesis Video Streams)之WebRTC依赖项

    KVS WebRTC依赖于openssl.srtp.usrsctp.kvspic.我们挨个来编译,如果我们需要往嵌入式设备中移植,需要移植这些依赖项,因此在编译移植前,我们需要得到源码. 先下载所有代 ...

  7. 洛谷——1064金明的预算方案————有依赖的背包

    题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:"你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过N元钱 ...

  8. hdu4044 依赖背包变形 好题!

    由于不是求最大的可拦截的HP值,而是要将最小值最大化,那么就需要分配每个子树用的钱数以达到最小值最大化 第一步解决如何分配钱使得结点u的子树中用了j元钱后可以拦截的HP最大,这就是变形的分组(依赖)背 ...

  9. 浅说——九讲背包之01背包

    所谓九讲,也就是: 0/1背包 0/1背包降维 完全背包 多重背包(二进制优化) 混合背包 二维费用背包 分组背包 有依赖的背包 背包的方案总数\背包的具体方案路径 0/1背包: [问题描述](经典) ...

最新文章

  1. 没学过编程可以自学python吗-完全没学过编程的人学习 Python前应该掌握些什么?...
  2. NAS服务器局域网内IPad、手机、电视盒子等联网播放
  3. spring mvc 返回html 乱码,解决springmvc使用ResponseBody注解返回json中文乱码问题
  4. python+OpenCV图像处理(二)图像像素的访问、通道的合并与分离
  5. Intel 64/x86_64/IA-32/x86处理器 - 指令格式(6) - 8086/16位指令位移量字节/立即数字节
  6. RTT学习笔记3-时钟定时器管理
  7. ORA-28009:connection as SYS should be as SYSDBA OR SYSOPER
  8. 古风手机壁纸,国潮的你不可错过!
  9. java intent 传递集合对象_Android系列之Intent传递对象的几种实例方法
  10. 猜数游戏c语言编程while,【游戏编程】猜数字游戏(C语言)
  11. 华为云盘里面的照片怎么导出来_华为手机误删照片,怎么恢复?别急!只需点击这里...
  12. 路由器当交换机用的设置方法
  13. 人人都是产品经理总结 第三章1
  14. 深度学习的显卡对比评测:2080ti vs 3090 vs A100
  15. mysql root password_MYSQL安装时解决要输入current root password的方案
  16. CocosCreator之Button按钮
  17. Linux下编写和加载 .ko 文件(驱动模块文件)
  18. 差分机:尼古拉的雪耻
  19. 天猫忌惮京东开放平台壮大 欲借“二选一”形成垄断
  20. 坐标系统与投影变换及在ARCGIS中的应用

热门文章

  1. 下面不是python合法标识符_哪个不是python合法标识符
  2. Linux 端口占用解决
  3. 解决Linux下启动Tomcat遇到Neither the JAVA_HOME nor the JRE_HOME environment variable is defined...
  4. mysql开启慢查询
  5. ubuntu安装jdk,ubuntu设置java环境变量
  6. 再见xx网盘!4 行命令搭建属于你的私人网盘!
  7. 我用 Python 帮朋友做了张图,结果
  8. 刷新纪录 | 74.7 秒训练完 ImageNet!2048 GPU 暴力出奇迹
  9. 从全职高手开始的系统_《全职高手》让杨洋“去油”,却不是他的“救命稻草”...
  10. python获取当时文件目录_python获取文件目录