题目链接

题目

题目描述

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

题解

知识点:背包dp。

分组背包套01背包,因为每组物品不能枚举否则超时。

要先开一个临时空间给分组背包内的01背包,因为组内物品是一同考虑的,不能影响一个组的值,最后和原数组没有买游戏机的比较取最大值。

剩下的看代码和01背包差不多。

时间复杂度 \(O(nvg)\)

空间复杂度 \(O(v)\)

代码

#include <bits/stdc++.h>

using namespace std;

int dp[100007], tmp[100007];

int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, v;
cin >> n >> v;
for (int i = 1, p, g;i <= n;i++) {///组
cin >> p >> g;
for (int k = p;k <= v;k++) tmp[k] = dp[k - p];///买游戏机
for (int j = 1, gp, pv;j <= g;j++) { ///游戏
cin >> gp >> pv;
for (int k = v;k >= p + gp;k--)///因为买了游戏机,价格不能小于游戏机加这个游戏
tmp[k] = max(tmp[k], tmp[k - gp] + pv);///在第i组滚动
}
for (int k = p;k <= v;k++) dp[k] = max(dp[k], tmp[k]);///和不买游戏机比
}
cout << dp[v] << '\n';
return 0;
}

NC24870 [USACO 2009 Dec G]Video Game Troubles的更多相关文章

  1. NC24866 [USACO 2009 Dec S]Music Notes

    NC24866 [USACO 2009 Dec S]Music Notes 题目 题目描述 FJ is going to teach his cows how to play a song. The ...

  2. BZOJ1775[USACO 2009 Dec Gold 3.Video Game Troubles]——DP

    题目描述 输入 * 第1行: 两个由空格隔开的整数: N和V * 第2到第N+1行: 第i+1行表示第i种游戏平台的价格和可以在这种游戏平台上面运行的游 戏.包含: P_i, G_i还有G_i对由空格 ...

  3. BZOJ1774[USACO 2009 Dec Gold 2.Cow Toll Paths]——floyd

    题目描述 跟所有人一样,农夫约翰以着宁教我负天下牛,休叫天下牛负我的伟大精神,日日夜夜苦思生 财之道.为了发财,他设置了一系列的规章制度,使得任何一只奶牛在农场中的道路行走,都 要向农夫约翰上交过路费 ...

  4. USACO 2009 Dec cow toll paths 过路费-floyd

    这道题首先要明确一点,那就是当你从一个点走到自己时,也是需要花费这个点点权值的费用.这个点卡了我两次QWQ 然后我比较喜欢分两步搞: 首先,我们利用floyd的一个性质:就是在更新其他点之间的路线时要 ...

  5. Usaco 滑雪比赛 Bobsledding, 2009 Dec(dp)

    Description 滑雪比赛bobsled 贝西参加了一场高山急速滑雪比赛,滑道总长度为 L.出发时,她的初速度为 1,贝西可以加速 或减速,每过 1 米,她能将速度增加 1.减少 1 或保持不变 ...

  6. USACO翻译:USACO 2013 DEC Silver三题

    USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...

  7. USACO 2009 Open 干草塔 Tower of Hay

    USACO 2009 Open 干草塔 Tower of Hay Description 为了调整电灯亮度,贝西要用干草包堆出一座塔,然后爬到牛棚顶去把灯泡换掉.干草 包会从传送带上运来,共会出现N包 ...

  8. USACO翻译:USACO 2014 DEC Silver三题

    USACO 2014 DEC SILVER 一.题目概览 中文题目名称 回程 马拉松 奶牛慢跑 英文题目名称 piggyback marathon cowjog 可执行文件名 piggyback ma ...

  9. [USACO 2017 Dec Gold] Tutorial

    Link: USACO 2017 Dec Gold 传送门 A: 为了保证复杂度明显是从终结点往回退 结果一开始全在想优化建边$dfs$……其实可以不用建边直接$multiset$找可行边跑$bfs$ ...

  10. USACO 2009 Feb 股票市场 Stock Market

    USACO 2009 Feb 股票市场 Stock Market Description 尽管奶牛们天生谨慎,她们仍然在住房抵押信贷市场中大受打击,现在她们准备在股市 上碰碰运气.贝西开挂了,她知道S ...

随机推荐

  1. SpringBoot01:HelloWorld!

    回顾Spring Spring是一个开源框架,2003年兴起的一个轻量级的Java开发框架. Spring是为了解决企业级应用开发的复杂性而创建的,简化开发. Spring是怎样简化Java开发的呢? ...

  2. 各种数据库shell一键登录的方法

    各种数据库shell一键登录的方法 一个小场景 想通过shell 直接执行一些内容. 然后发现通过shell就可以. 所以这里总结一下 1. Oracle export ORACLE_HOME=/u0 ...

  3. 【转帖】【性能提升神器】STRAIGHT_JOIN

    今天给大家下另一个性能提升神器-STRAIGHT_JOIN,在数据量大的联表查询中灵活运用的话,能大大缩短查询时间. 首先来解释下STRAIGHT_JOIN到底是用做什么的: STRAIGHT_JOI ...

  4. [转帖]ELKStack入门篇(二)之Nginx、Tomcat、Java日志收集以及TCP收集日志使用

    https://www.cnblogs.com/linuxk/p/9273160.html 1.收集Nginx的json格式日志 1.1.Nginx安装   1.2.配置logstash [root@ ...

  5. [转帖]Dapper,大规模分布式系统的跟踪系统

    http://bigbully.github.io/Dapper-translation/ 作者:Benjamin H. Sigelman, Luiz Andr´e Barroso, Mike Bur ...

  6. [转帖]华为毕昇 JDK 8u292、11.0.11 发布!

    https://baijiahao.baidu.com/s?id=1705499834793298544&wfr=spider&for=pc 2021 年 6 月 30 日,毕昇 JD ...

  7. PHP GC回收机制详解

    前言 GC的全称是Garbage Collection也就是垃圾回收的意思,在PHP中,是使用引用计数和回收周期来自动管理内存对象的,当一个对象被设置为NULL,或者没有任何指针指向时,他就会变成垃圾 ...

  8. (数据科学学习手札76)基于Python的拐点检测——以新冠肺炎疫情数据为例

    本文对应代码.数据及文献资料已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes,对代码不感兴趣的朋友可以直接跳至2.2 探 ...

  9. Walrus 0.5发布:重构交互流程,打造开箱即用的部署体验

    开源应用管理平台 Walrus 0.5 已于近日正式发布! Walrus 0.4 引入了全新应用模型,极大程度减少了重复的配置工作,并为研发团队屏蔽了云原生及基础设施的复杂度.Walrus 0.5 在 ...

  10. C/C++ 反汇编:针对加减乘除的还原

    算术运算通常是指,加减乘除四则运算,而计算机中的四则运算与数学中的有所不同,同样是实现算术运算,高级语言与汇编语言的实现思路完全不同,往往一个简单的减法运算,都要几条指令的配合才能得出计算结果,而为了 ...