POJ 3624 01背包
初学DP,用贪心的思想想解题,可是想了一个多小时还是想不出。
//在max中的两个参数f[k], 和f[k-weight[i]]+value[i]都是表示在背包容量为k时的最大价值
//f[k]是这个意思,就不用说了。
//而f[k-weight[i]]+value[i]也表示背包容量为k时的最大价值是为什么呢?
//首先,f[k-weight[i]]表示的是背包容量为k-weight[i]的容量,也就是说f[k-weight[i]]
//表示的是容量还差weiht[i]才到k的价值,+walue[i]恰好弥补了差的这个价值。所以…… //如果你对f[k]=max(f[k],(f[k-weight[i]]+value[i]));这句话不是很清楚,
//下面的这句代码会对你有帮助
//cout<<"i="<<i+1<<" f["<<k<<"]="<<f[k]<<endl;
#include <iostream>
using namespace std;
int f[];
int main(){
int Num, TotalWeight, i, j, k, weight[], value[];
cin >> Num >> TotalWeight;
for(i = ; i < Num; ++i) cin >> weight[i] >> value[i];
for(i = ; i < Num; ++i){
for(k = TotalWeight; k >= weight[i]; --k){
f[k] = max(f[k], (f[k - weight[i]] + value[i]));
}
}
cout << f[TotalWeight] << endl;
return ;
}
POJ 3624 01背包的更多相关文章
- poj 2184 01背包变形【背包dp】
POJ 2184 Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14657 Accepte ...
- POJ 2184 01背包+负数处理
Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10200 Accepted: 3977 D ...
- poj 1837 01背包
Balance Time Limit: 1000 MS Memory Limit: 30000 KB 64-bit integer IO format: %I64d , %I64u Java clas ...
- POJ 3628 01背包 OR 状压
思路: 1.01背包 先找到所有奶牛身高和与B的差. 然后做一次01背包即可 01背包的容积和价格就是奶牛们身高. 最后差值一减输出结果就大功告成啦! 2. 搜索 这思路很明了吧... 搜索的确可以过 ...
- Proud Merchants(POJ 3466 01背包+排序)
Proud Merchants Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) ...
- POJ之01背包系列
poj3624 Charm Bracelet 模板题 没有要求填满,所以初始化为0就行 #include<cstdio> #include<iostream> using na ...
- POJ 3624 Charm Bracelet(01背包裸题)
Charm Bracelet Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 38909 Accepted: 16862 ...
- POJ 3624 Charm Bracelet (01背包)
题目链接:http://poj.org/problem?id=3624 Bessie has gone to the mall's jewelry store and spies a charm br ...
- POJ 3624 Charm Bracelet(0-1背包模板)
http://poj.org/problem?id=3624 题意:给出物品的重量和价值,在重量一定的情况下价值尽可能的大. 思路:经典0-1背包.直接套用模板. #include<iostre ...
随机推荐
- [LeetCode]题解:005-Longest Palindromic Substring优化
题目来源和题意分析: 详情请看我的博客:http://www.cnblogs.com/chruny/p/4791078.html 题目思路: 我上一篇博客解决这个问题的时间复杂度是最坏情况是(O(n^ ...
- Anatomy of a Program in Memory
Memory management is the heart of operating systems; it is crucial for both programming and system a ...
- STL deque详解
英文原文:http://www.codeproject.com/Articles/5425/An-In-Depth-Study-of-the-STL-Deque-Container 绪言 这篇文章深入 ...
- android下文件下载
public static void downFile(final String url){ new Thread(){ public void run(){ FileOutputStream os= ...
- leetcode第一刷_Path Sum II
在更新上面一道题的时候我就想,是不是另一道打印路径的,果不其然啊. 这样的题非经常见的,做法也非常easy,我是用一个引用的vector来存,满足条件之后直接压入结果集中,当然也能够用数组之类的,都一 ...
- poj2492 A Bug's Life【基础种类并查集】
转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4298148.html ---by 墨染之樱花 题目链接:http://poj.org/pr ...
- c#关于EXCEL导入数据库的做法
以下例子转载:互联网 先在类中定义一个方法名为ExecleDs的方法,用于将Excel表里的数据填充到DataSet中,代码如下 public DataSet ExecleDs(string file ...
- linux c: 静态库和动态库的生成和使用
场景: main函数需要两个接口,一个求和函数,一个打印函数. int sum(int i, int j); 求两个int数字的和. void show(int i, char* name); 打印i ...
- Centos6.4安装JDK
链接地址:http://www.iteye.com/topic/1130311 1.先看看OpenJDK的安装包 $ rpm -qa |grep javatzdata-java-2013b-1.el6 ...
- MongoDB Linux下的安装和启动(转)
1. 下载MongoDB,此处下载的版本是:mongodb-linux-i686-1.8.1.tgz.tar. http://fastdl.mongodb.org/linux/mongodb-linu ...