【0-1 背包模板】 poj 3624
先看个未经优化的二维空间dp:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <memory.h>
using namespace std;
const int maxn1=;
const int maxn2=;
int dp[maxn2][maxn2];
//int dp[maxn2];
int w[maxn1],v[maxn2];
int m,n;
int max(int x,int y)
{
return x>y?x:y;
}
int main()
{
freopen("in.txt","r",stdin);
while(~scanf("%d%d",&n,&m))
{
for(int i=;i<=n;i++)
cin >> w[i] >> v[i];
}
memset(dp,,sizeof(dp));
int j;
for(int i=;i<=n;i++)
{
for(j=;j<=m;j++)
{
if(j>=w[i])
dp[i][j]=max(dp[i-][j],dp[i-][j-w[i]]+v[i]);
else
dp[i][j] = dp[i-][j];
}
}
cout << dp[n][m] << endl;
return ;
}
改进:
1。二维优化到一维
2。倒写
#include <iostream>
#include <cstdio>
#include <cmath>
#include <memory.h>
using namespace std;
const int maxn1=;
const int maxn2=;
int dp[maxn2],w[maxn1],v[maxn2];
int m,n;
int max(int a, int b)
{
if(a>b) return a ;
else return b ;
}
int main()
{
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
while(~scanf("%d%d",&n,&m))
{
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)
cin >> w[i] >> v[i];
int j;
for(int i=;i<=n;i++)
{
for(j=m;j>=w[i];j--)
{
dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
cout << j << " j "<< dp[j]<< endl;
}
cout <<endl;
}
cout << dp[m] << endl;
}
return ;
}
给一组数据便于理解:
6 j 4
5 j 4
4 j 4
3 j 4
2 j 4
1 j 4
6 j 10
5 j 10
4 j 10
3 j 10
2 j 6
6 j 22
5 j 18
4 j 16
3 j 12
6 j 23
5 j 19
4 j 16
3 j 12
2 j 7
23
最后给出模板:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <memory.h>
using namespace std; const int maxn1=;
const int maxn2=;
int dp[maxn2],w[maxn1],v[maxn1];
int m,n; int max(int a, int b)
{
if(a>b) return a ;
else return b ;
} int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d%d",&n,&m))
{
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)
cin >> w[i] >> v[i];
int j;
for(int i=;i<=n;i++)
for(j=m;j>=w[i];j--)
dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
cout << dp[m] << endl;
}
return ;
}
【0-1 背包模板】 poj 3624的更多相关文章
- 01背包模板、全然背包 and 多重背包(模板)
转载请注明出处:http://blog.csdn.net/u012860063 贴一个自觉得解说不错的链接:http://www.cppblog.com/tanky-woo/archive/2010/ ...
- 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(DP 01背包)
POJ.3624 Charm Bracelet(DP 01背包) 题意分析 裸01背包 代码总览 #include <iostream> #include <cstdio> # ...
- POJ 1636 Prison rearrangement DFS+0/1背包
题目链接: id=1636">POJ 1636 Prison rearrangement Prison rearrangement Time Limit: 3000MS Memor ...
- POJ 1745 【0/1 背包】
题目链接:http://poj.org/problem?id=1745 Divisibility Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- 01背包问题:Charm Bracelet (POJ 3624)(外加一个常数的优化)
Charm Bracelet POJ 3624 就是一道典型的01背包问题: #include<iostream> #include<stdio.h> #include& ...
- poj 3624 Charm Bracelet 01背包问题
题目链接:poj 3624 这是最基础的背包问题,特点是:每种物品仅有一件,可以选择放或不放. 用子问题定义状态:即F [i, v]表示前i件物品恰放入一个容量为v 的背包可以 ...
- 解题报告:hdu2602 Bone collector 01背包模板
2017-09-03 15:42:20 writer:pprp 01背包裸题,直接用一维阵列的做法就可以了 /* @theme: 01 背包问题 - 一维阵列 hdu 2602 @writer:ppr ...
- 【洛谷P2722 USACO】 总分 01背包模板
P2722 总分 Score Inflation 题目背景 学生在我们USACO的竞赛中的得分越多我们越高兴. 我们试着设计我们的竞赛以便人们能尽可能的多得分,这需要你的帮助 题目描述 我们可以从几个 ...
- P2722 总分 Score Inflation (完全背包模板)
题目传送门:P2722 总分 Score Inflation 题目描述 我们可以从几个种类中选取竞赛的题目,这里的一个"种类"是指一个竞赛题目的集合,解决集合中的题目需要相同多的时 ...
随机推荐
- ggplot2 theme相关设置—文本调整
在geom设置和scale设置之后,要想把图画的漂亮,theme设置是比不可少的 在theme 设置中element_text()是一项很重要的内容 element_text(family = NUL ...
- Union、Union All、Except、InterSect的区别
UNION: 将多个「结果集 (result set)」的「行 (row)」合并,作为单个结果集返回,并移除重复的行.若有重复的行,只留下一个. UNION ALL: 将多个「结果集 (result ...
- Linux(power服务器)中kettle(2)
Hadoop集群硬件环境 4台机器 ip地址 172.16.1.131 172.16.1.132 172.16.1.133 172.16.1.134 每台内存16G 8核cpu 直接使用报错:
- C语言 - 预编译
1.#ifdef 实现 与 或#if (defined SIMULATION) && (defined _FMM_LOG)#endif #if (!defined A) || (!de ...
- 查询全国的省市信息<option>拼接
//修改页面获取省市信息 function getProvinceUSER_AREA1(){ $.ajax({ type:'post', url:'<%=path%>/user/findU ...
- great C++ socket library
NETLINK: http://netlinksockets.sourceforge.net/index.html
- APP测试--功能测试
1.1 了解需求 这一点,不但是功能测试,是所有测试都需要的第1步.通过需求文档,与产品经理的沟通,与开发的沟通,用户的使用习惯等各方法,了解APP的需求. 1.2 编写测试用例 当然之前可能是测试计 ...
- px和em,rem的区别
任意浏览器的默认字体高都是16px. px: 像素(Pixel) , 计算机屏幕上的一个点.固定大小:不方便维护: em:相对于当前对象内 (父元素) 文本的字体尺寸.如当前对行内文本的字体尺寸未被人 ...
- JavaScript(8)——JSON
JSON 啊呀呀,终于写到了JSON了.莫名的开心,虽然还是被说进度慢,不过,我不管,我就是开心.恩,好好学习,好好加油.(这是一段毫无逻辑的话,我也不知道说啥了) JSON是JavaScript的一 ...
- 将dom4j格式化为标准的xml字符串
StringWriter writer = new StringWriter(); OutputFormat format = OutputFormat.createPrettyPrint(); fo ...