//路径记录方法:若是dp[j-value[i]]+value[i]>dp[j]说明拿了这个东西,标志为1,

//for循环标志,发现是1,就打印出来,并把背包的容量减少,再在次容量中寻找标志;

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int value[],dp[],s[][]; int main()
{
int n,m;
while(cin>>m)
{
cin>>n;
for(int i=;i<=n;i++)
cin>>value[i];
memset(dp,,sizeof(dp));
memset(s,,sizeof(s));
for(int i=;i<=n;i++)
for(int j=m;j>=value[i];j--)
if(dp[j]<=dp[j-value[i]]+value[i])
{
dp[j]=dp[j-value[i]]+value[i];
s[i][j]=;
}
for(int i=n,j=m;i>=;i--)
{
if(s[i][j])
{
cout<<value[i]<<" ";
j=j-value[i];
}
}
cout<<"sum:"<<dp[m]<<endl;
}
return ;
}

UVA 624 CD (01背包)的更多相关文章

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

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

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

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

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

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

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

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

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

  6. uva 624 CD (01背包)

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

  7. UVA624 CD,01背包+打印路径,好题!

    624 - CD 题意:一段n分钟的路程,磁带里有m首歌,每首歌有一个时间,求最多能听多少分钟的歌,并求出是拿几首歌. 思路:如果是求时常,直接用01背包即可,但设计到打印路径这里就用一个二维数组标记 ...

  8. CD(01背包)

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

  9. 紫书 习题 10-5 UVa 1213(01背包变形)

    这里就是01背包多了一维物品个数罢了 记得不能重复所以有一层循环顺序要倒着来 边界f[0][0] = 1 #include<cstdio> #include<vector> # ...

随机推荐

  1. sqlcipher for android

    github 地址 https://github.com/sqlcipher/android-database-sqlcipher 官网教程 https://www.zetetic.net/sqlci ...

  2. php 常量定义

    php常量定义及取值  常量在定义时赋值:  不能变 :不能销毁: 具有超全局作用于:常量只能储存标量数据(字符 整型 浮点 ): <?php define("hello", ...

  3. java读写锁源码分析(ReentrantReadWriteLock)

    读锁的调用,最终委派给其内部类 Sync extends AbstractQueuedSynchronizer /** * 获取读锁,如果写锁不是由其他线程持有,则获取并立即返回: * 如果写锁被其他 ...

  4. ASP.NET使用ImageMap控件

    文章来自:http://www.baike369.com/content/?id=5773

  5. python 之遍历目录树(可匹配输出特定后缀的文件)

    涉及到的模块有os, fnmatch:1.通过os模块中的方法获取dir.subdir.files,通过os.path.join可拼接成完整路径: 2.fnmatch主要通过fnmatch.fnmat ...

  6. C#中的DataSet添加DataTable问题

    最近在使用DataTable来给前台控件绑定数据,开始时查了网上的一些给DataSet添加DataTable时需要注意的地方,一般都要添加表名并且使用DataTable.Copy()方法,否则会报错, ...

  7. DUIlib使用Fastreport--报表简单使用

    fastreport是一个简单优秀的报表,fastreport更多是和delphi联合使用预览和打印数据的.我在开始使用duilib做项目时,打印和数据预览都是自己绘制的,这样不仅绘制麻烦费事费事,而 ...

  8. linux操作数据库

    1.显示数据库 show databases; 2.选择数据库 use 数据库名; 3.显示数据库中的表 show tables; 4.显示数据表的结构 describe 表名; 5.显示表中记录 S ...

  9. matlab里textread出现错误“Trouble reading floating point number from file (row 1, field 1)”

    matlab里textread出现错误“Trouble reading floating point number from file (row 1, field 1)” 解决办法:traindata ...

  10. springmvc集成aop记录操作日志

    首先说明一下,这篇文章只做了记录日志相关事宜 具体springmvc如何集成配置aop对cotroller进行拦截,请看作者的另一篇文章 http://www.cnblogs.com/guokai87 ...