You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is on
CDs. You need to have it on tapes so the problem to solve is: you have a tape N minutes long. How
to choose tracks from CD to get most out of tape space and have as short unused space as possible.
Assumptions:
• number of tracks on the CD does not exceed
• no track is longer than N minutes
• tracks do not repeat
• length of each track is expressed as an integer number
• N is also integer
Program should find the set of tracks which fills the tape best and print it in the same sequence as
the tracks are stored on the CD
Input
Any number of lines. Each one contains value N, (after space) number of tracks and durations of the
tracks. For example from first line in sample data: N = , number of tracks=, first track lasts for
minute, second one minutes, next one minutes
Output
Set of tracks (and durations) which are the correct solutions and string ‘sum:’ and sum of duration
times.
Sample Input Sample Output
sum:
sum:
sum:
sum:
sum:

【代码】:

【一维 + vis标记数组 + 倒序输出】:用一个vis[i][j]记录容量为j的背包里面有没有用到过i物品,物品是倒着放的。

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,n,x) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = ;
const int maxm = 1e5 + ;
const double PI = acos(-1.0);
const double eps = 1e-;
const int dx[] = {-,,,,,,-,-};
const int dy[] = {,,,-,,-,,-};
const int mon[] = {, , , , , , , , , , , , };
const int monn[] = {, , , , , , , , , , , , };
int n,m;
int v[maxm], w[maxm],dp[maxm];
int t;
bool vis[][maxm]; int main()
{
while(cin>>m>>n){
memset(vis,,sizeof(vis));
memset(dp,,sizeof(dp)); for(int i=;i<=n;i++)
cin>>w[i]; for(int i=n;i>=;i--){ //输出倒序
for(int j=m;j>=w[i];j--){
//dp[j]=max(dp[j],dp[j-w[i]]+w[i]);
if(dp[j]<dp[j-w[i]]+w[i]){
dp[j]=dp[j-w[i]]+w[i];
vis[i][j]=true;
}
}
}
for(int i=,j=dp[m]; i<=n && j>; i++){
if(vis[i][j]){
cout<<w[i]<<' ';
j -= w[i];
}
}
cout<<"sum:"<<dp[m]<<endl;
}
}
/*
5 3 1 3 4
10 4 9 8 4 2
20 4 10 5 7 4
90 8 10 23 1 2 3 4 5 7
45 8 4 10 44 43 12 9 8 2
*/

一维逆序

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,n,x) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = ;
const int maxm = 1e5 + ;
const double PI = acos(-1.0);
const double eps = 1e-;
const int dx[] = {-,,,,,,-,-};
const int dy[] = {,,,-,,-,,-};
const int mon[] = {, , , , , , , , , , , , };
const int monn[] = {, , , , , , , , , , , , };
/*
5 3 1 3 4
10 4 9 8 4 2
20 4 10 5 7 4
90 8 10 23 1 2 3 4 5 7
45 8 4 10 44 43 12 9 8 2
*/
int dp[][maxm];
int w[],n,m;
int main()
{
while(cin>>m>>n)
{
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++) cin>>w[i]; for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++) //注意是顺推
{
if(j>=w[i]) dp[i][j]=max(dp[i-][j], dp[i-][j-w[i]]+w[i]);
else dp[i][j]=dp[i-][j]; //必须判断
}
} for(int i=n,j=m; i>; i--)
{
if(dp[i][j]>dp[i-][j]) //在顺推时考察第i是放还是不放是从前一个状态推出来的,如果放入i时大,说明放入了i,否则没放和前一个状态一样
{
cout << w[i] <<' ';
j -= w[i];
}
}
cout << "sum:"<<dp[n][m]<<endl;
}
}

二维顺推

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,n,x) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = ;
const int maxm = 1e5 + ;
const double PI = acos(-1.0);
const double eps = 1e-;
const int dx[] = {-,,,,,,-,-};
const int dy[] = {,,,-,,-,,-};
const int mon[] = {, , , , , , , , , , , , };
const int monn[] = {, , , , , , , , , , , , };
/*
5 3 1 3 4
10 4 9 8 4 2
20 4 10 5 7 4
90 8 10 23 1 2 3 4 5 7
45 8 4 10 44 43 12 9 8 2
*/
int dp[maxm],vis[maxm];
int w[],n,m;
int main()
{
while(cin>>m>>n)
{
memset(dp,,sizeof(dp));
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++) cin>>w[i];
for(int i=;i<=n;i++)
{
for(int j=m;j>=w[i];j--)
{
if(dp[j] < dp[j-w[i]]+w[i])
{
dp[j] = dp[j-w[i]]+w[i];
vis[j] = vis[j-w[i]] | (<<i);
}
}
} for(int i=; i<=n; i++)
{
if(vis[m] & (<<i))
{
cout << w[i] << ' ';
}
}
cout << "sum:"<<dp[m]<<endl;
}
}

一维状态压缩

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,n,x) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = ;
const int maxm = 1e5 + ;
const double PI = acos(-1.0);
const double eps = 1e-;
const int dx[] = {-,,,,,,-,-};
const int dy[] = {,,,-,,-,,-};
const int mon[] = {, , , , , , , , , , , , };
const int monn[] = {, , , , , , , , , , , , };
/*
5 3 1 3 4
10 4 9 8 4 2
20 4 10 5 7 4
90 8 10 23 1 2 3 4 5 7
45 8 4 10 44 43 12 9 8 2
*/
int dp[maxm],vis[maxm];
int w[],n,m;
void print(int i)
{
if(vis[i]){
print(vis[i]);
cout<<i-vis[i]<<' ';
}
else{
cout<<i<<' ';
return ;
}
}
int main()
{
while(cin>>m>>n)
{
memset(dp,,sizeof(dp));
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++) cin>>w[i];
for(int i=;i<=n;i++)
{
for(int j=m;j>=w[i];j--)
{
if(dp[j] < dp[j-w[i]]+w[i])
{
dp[j] = dp[j-w[i]]+w[i];
vis[j] = j - w[i];
}
}
}
print(dp[m]);
cout << "sum:"<<dp[m]<<endl;
}
}

一维递归

UVA 624 CD【01背包+路径记录】的更多相关文章

  1. UVA 624 ---CD 01背包路径输出

    DescriptionCD You have a long drive by car ahead. You have a tape recorder, but unfortunately your b ...

  2. UVA 624 - CD (01背包 + 打印物品)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  3. UVA 624 CD (01背包)

    //路径记录方法:若是dp[j-value[i]]+value[i]>dp[j]说明拿了这个东西,标志为1, //for循环标志,发现是1,就打印出来,并把背包的容量减少,再在次容量中寻找标志: ...

  4. uva 624 CD 01背包打印路径

    // 集训最终開始了.来到水题先 #include <cstdio> #include <cstring> #include <algorithm> #includ ...

  5. UVA--624 CD(01背包+路径输出)

    题目http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  6. PAT L3-001 凑零钱(01背包dp记录路径)

    韩梅梅喜欢满宇宙到处逛街.现在她逛到了一家火星店里,发现这家店有个特别的规矩:你可以用任何星球的硬币付钱,但是绝不找零,当然也不能欠债.韩梅梅手边有104枚来自各个星球的硬币,需要请你帮她盘算一下,是 ...

  7. uva 624 CD (01背包)

      CD  You have a long drive by car ahead. You have a tape recorder, but unfortunately your best musi ...

  8. UVA 624 CD(01背包+输出方案)

    01背包,由于要输出方案,所以还要在dp的同时,保存一下路径. #include <iostream> #include <stdio.h> #include <stri ...

  9. UVA 624 CD(DP + 01背包)

    CD You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music i ...

随机推荐

  1. javascript md5 二次加密 和 java md5 二次加密结果不同

    最近研究httpclient post 时遇到了一个问题,很费解. js  md5(str) 和 java md5(str),第一次md5 加密结果一样,(当时忽略了大小写问题,java 大写,js小 ...

  2. DOS程序员手册(三)

    56页     第4章DOS和BIOS接口     本章介绍了用户程序访问DOS内核和BIOS所提供的各种服务的方法.为了访问这 些服务,我们可以从任何编程语言中调用各个软件中断,这些中断便是我们在本 ...

  3. Linux编译安装与配置-MySQL(5.5,5.6)版本系(笔记)

    MySQL 5.5(5.6)后版本,需要使用cmake(Cross make , https://cmake.org/ )编译 我的环境如下: VMWare虚拟机,CentOS 5.5 x86_64( ...

  4. iOS笔记057 - UI总结03

    控制器的父子关系 1.控制器父子关系的建立原则        如果2个控制器的view是父子关系(不管是直接还是间接的父子关系),那么这2个控制器也应该为父子关系 [self.view addSubv ...

  5. springboot示例参考网站

    https://blog.csdn.net/u013248535/article/details/55100979/

  6. ASP.NET Core ---异常处理

    一.局部异常处理: 在Action里面catch 二.全局异常处理: 1.默认的异常处理配置: 默认配置在StartUp文件的Configure中注册错误处理,显示开发者错误页面: public vo ...

  7. 洛谷P1071潜伏者(提高组)

    题目描述 R国和S国正陷入战火之中,双方都互派间谍,潜入对方内部,伺机行动.历尽艰险后,潜伏于S国的R 国间谍小C终于摸清了 S 国军用密码的编码规则: 1. S国军方内部欲发送的原信息经过加密后在网 ...

  8. BETA(1)

    目录 组员情况 组员1(组长):胡绪佩 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示组内最新成果 团 ...

  9. SPOJ AMR10I Dividing Stones

    Time limit: 7s Source limit: 50000B Memory limit: 256MB The first line contains the number of test c ...

  10. springmvc项目搭建三-添加前端框架

    这几年前端框架发展可以说非常迅猛了...实际项目中也用到了几个,easyui相对来讲,算是我第一个接触的前端框架了,用的时候感觉很方便,省了很多代码量,一个好的前端框架可以为你省去很多精力在前端布局上 ...