先看个未经优化的二维空间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的更多相关文章

  1. 01背包模板、全然背包 and 多重背包(模板)

    转载请注明出处:http://blog.csdn.net/u012860063 贴一个自觉得解说不错的链接:http://www.cppblog.com/tanky-woo/archive/2010/ ...

  2. 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 ...

  3. POJ.3624 Charm Bracelet(DP 01背包)

    POJ.3624 Charm Bracelet(DP 01背包) 题意分析 裸01背包 代码总览 #include <iostream> #include <cstdio> # ...

  4. POJ 1636 Prison rearrangement DFS+0/1背包

    题目链接: id=1636">POJ 1636 Prison rearrangement Prison rearrangement Time Limit: 3000MS   Memor ...

  5. POJ 1745 【0/1 背包】

    题目链接:http://poj.org/problem?id=1745 Divisibility Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

  6. 01背包问题:Charm Bracelet (POJ 3624)(外加一个常数的优化)

    Charm Bracelet    POJ 3624 就是一道典型的01背包问题: #include<iostream> #include<stdio.h> #include& ...

  7. poj 3624 Charm Bracelet 01背包问题

    题目链接:poj 3624 这是最基础的背包问题,特点是:每种物品仅有一件,可以选择放或不放.             用子问题定义状态:即F [i, v]表示前i件物品恰放入一个容量为v 的背包可以 ...

  8. 解题报告:hdu2602 Bone collector 01背包模板

    2017-09-03 15:42:20 writer:pprp 01背包裸题,直接用一维阵列的做法就可以了 /* @theme: 01 背包问题 - 一维阵列 hdu 2602 @writer:ppr ...

  9. 【洛谷P2722 USACO】 总分 01背包模板

    P2722 总分 Score Inflation 题目背景 学生在我们USACO的竞赛中的得分越多我们越高兴. 我们试着设计我们的竞赛以便人们能尽可能的多得分,这需要你的帮助 题目描述 我们可以从几个 ...

  10. P2722 总分 Score Inflation (完全背包模板)

    题目传送门:P2722 总分 Score Inflation 题目描述 我们可以从几个种类中选取竞赛的题目,这里的一个"种类"是指一个竞赛题目的集合,解决集合中的题目需要相同多的时 ...

随机推荐

  1. HDU 1008 u Calculate e

    Problem Description A simple mathematical formula for e is where n is allowed to go to infinity. Thi ...

  2. ajaxFileUpload 报这错jQuery.handleError is not a function

    今天刚打个一个技术群,里面有人问标题上的问题,嘿,我恰好遇过,现在大家至少也在用jquery1.9以上的版本,ajaxfileupload的版本早就不更新了,大家可以下载看:地址这里, 它例子里使用的 ...

  3. JuneX_13

    在积分制的压力下,基本上能打的比赛都打了(除了忘了的).打了这么多比赛(其实也不多),发现有相当一部分题目考察的还是挺基础的内容,像搜索,DP,树,图,然而做的并不好.要么直接不会敲,要么调试大半天, ...

  4. ggplot2 scale相关设置2—时间设置

    在scale设置中,常用的日期方面的设置函数包括: scale_x_date(),scale_y_date(),scale_x_datetime(),scale_y_datetime()   接下来, ...

  5. 6.Hibernate单向的多对一 关联映射

    1.创建如下项目结构 2.在项目的src下创建hibernate.cfg.xml主配置文件 <?xml version="1.0" encoding="UTF-8& ...

  6. 三、ASP.NET MVC Controller 控制器(二:IController控制器的创建过程)

    阅读目录: 1.开篇介绍 2.ASP.NETMVC IControllerFactory 控制器工厂接口 3.ASP.NETMVC DefaultControllerFactory 默认控制器工厂 4 ...

  7. Ambari中添加新服务

    官网: https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=38571133 一.背景 栈的定义可以在源代码树中找到/am ...

  8. LeetCode OJ 39. Combination Sum

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  9. Openjudge-计算概论(A)-骑车与走路

    描述: 在北大校园里,没有自行车,上课办事会很不方便.但实际上,并非去办任何事情都是骑车快,因为骑车总要找车.开锁.停车.锁车等,这要耽误一些时间.假设找到自行车,开锁并车上自行车的时间为27秒;停车 ...

  10. javascript动画效果之多物体透明度

    html和css 仅为布局,需要注意的是filter对应的是老版本的ie浏览器透明度,而opacity对应的其他浏览器的透明度 filter: alpha(opacity: 50); opacity: ...