HDU3449_Consumer
这个是一个背包的变形题,很值得仔细体味
大致题意:
这个比普通背包多一个限制:再选每一类物品之前必须要先购买一个篮子来装,篮子有一定的价格,其他就和背包是一样的了
思路:
为了能够体现篮子的价值,我们可以多加一维表示当前买第i个篮子和不买的情况
dp[i][j][0]表示用j个金币且第i个物品篮子没有买的前提下能获得最多的价值
dp[i][j][1]表示用j个金币且第i个物品篮子已经买的前提下能获得最多的价值
那么状态建好了,下面试着进行状态转移
dp[i][j][0] = max(dp[i-1][j][0], dp[i-1][j][1]);
dp[i][j][1]的状态转移稍微复杂一点,因为第i类物品有好几个,这个时候我们就要对i类每一个物品进行01选择
设当前物品价格p价值v
dp[i][j][1] = max(dp[i][j][1], dp[i][j-c-p][0]+v, dp[i][j-p][1] + v);
哈哈多么漂亮机智的移到
Description
of these stuff, he has to buy the box beforehand). Each kind of stuff has its own value. Now FJ only has an amount of W dollars for shopping, he intends to get the highest value with the money.
Input
mi (1<=mi<=10 the number goods ith box can carry), and mi pairs of numbers, the price cj (1<=cj<=100), the value vj(1<=vj<=1000000)
Output
Sample Input
3 800
300 2 30 50 25 80
600 1 50 130
400 3 40 70 30 40 35 60
Sample Output
210
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<stack>
#include<map>
#include<queue>
#include<vector> using namespace std;
const int maxn = 1e5+100;
const int INF = 0x7f7f7f7f;
#define pr(x) cout << #x << " = " << x << " ";
#define prln(x) cout << #x << " = " << x <<endl;
#define ll long long
int max(int a,int b,int c){
return max(max(a,b),c);
}
int dp[maxn][2], p, c, v, num;
int main(){
#ifdef LOCAL
freopen("C:\\Users\\User Soft\\Desktop\\in.txt","r",stdin);
//freopen("C:\\Users\\User Soft\\Desktop\\out.txt","w",stdout);
#endif
int n, m, k;
while(scanf("%d%d", &n, &m)==2){
memset(dp,0,sizeof dp);
for(int i = 1; i <= n; ++i) {
scanf("%d%d", &p, &num);
for(int j = 0; j <=m; ++j){
dp[j][0] = max(dp[j][0], dp[j][1]);
if(j < p) dp[j][1] = -INF;
else dp[j][1] = 0;
}
for(int j = 1; j <= num; ++j) {
scanf("%d%d",&c, &v);
for(int j = m; j >= 0; --j) {
if(j >= p + c)
dp[j][1] = max(dp[j-c-p][0] + v, dp[j-c][1] + v, dp[j][1]);
}
}
}
cout << max(dp[m][1], dp[m][0]) << "\n";
}
return 0;
}
HDU3449_Consumer的更多相关文章
随机推荐
- git-ssh-keygen
ssh-keygen 先看本地是否已经有了密钥 cd ~/.ssh 该文件夹下会包含两个文件 id_rsa --私钥 id_rsa.pub --公钥 如果没有这两个文件的话就需要重新生成(有的话使用一 ...
- 【学习总结】java数据结构和算法-第二章-数据结构和算法概述
总目录链接 [学习总结]尚硅谷2019java数据结构和算法 github:javaDSA 目录 数据结构和算法的关系 几个实际编程中的问题 线性结构和非线性结构 数据结构和算法的关系 几个实际编程中 ...
- k3 cloud中出现合计和汇总以后没有显示出来,合价要新增一行以后才出现值
解决办法:找到对应字段,把及时触发值更新事件打上勾
- 1.ireport基本使用
1. 2.
- R语言控制流
一般来说R语言是自上而下执行的,但是遇到特殊情况可能用到循环执行某些语句,这时候条件运算和循环就能派上用场了.
- 灰常牛逼的命令行备忘录 navi
灰常牛逼的命令行备忘录 navi 1. navi命令简介 1.1 navi命令简介 命令行是非常高效的工具,但一个很常见的现象是,很多命令行过一段时间就容易忘.举个栗子,如果我们常用 git 命令行管 ...
- [react-native]react-native填坑笔记
填坑笔记 开始入坑RN,从最开始的学起难免有不少乱七八糟的问题,记录在这里. 1. 8081端口占用问题 按照官网教程搭建开发环境并按照下面代码运行时候有报错,显示8081端口的问题 react-na ...
- webdriver原理
WebDriver 安装C/S构架设计的: 代码(客户端)--->驱动(解析代码)----->浏览器(服务端) 代码通过http请求发给浏览器驱动,驱动解析代码把他们发给浏览器,浏览器执行 ...
- python3 变量格式化转换成字符串
num=3 str='I am %f years old' % (num) print(str) 输出 I am 3.000000 years old
- JavaScript之ECMAScript
JavaScript脚本语言, 运行在浏览器上,无需编译, 轻量级的语言. 功能:让页面有执行逻辑的功能, 可以产生一些动态的效果 JavaScript = ECMAScript + BOM + DO ...