CD 

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 20
  • 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=5, number of tracks=3, first track lasts for 1 minute, second one 3 minutes, next one 4 minutes

Output

Set of tracks (and durations) which are the correct solutions and string ``sum:" and sum of duration times.

Sample Input

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

Sample Output

1 4 sum:5
8 2 sum:10
10 5 4 sum:19
10 23 1 2 3 4 5 7 sum:55
4 10 12 9 8 2 sum:45 简单的01背包,不过要输出路径,第一次写感觉特别麻烦,建立二维数组记录是否选中此物体。 题意:第一个数是路程,第二个数是歌曲的个数,后面的数是听每首歌的时间。要求在这段时间上尽可能地长时间听歌,每首歌必须听完整,总共最大能听歌的时间长度。
输出所有听的歌曲的时间,顺序为输入的顺序,以及最后总共的时间。 附上代码:
 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int maxs(int a,int b)
{
return a>b?a:b;
}
int main()
{
int i,j,n,m;
int a[],dp[];
int s[][];
while(~scanf("%d %d",&m,&n))
{
for(i=; i<n; i++)
scanf("%d",&a[i]);
memset(dp,,sizeof(dp));
memset(s,,sizeof(s));
for(i=n-; i>=; i--) //反序输入,为了后面能用正序
for(j=m; j>=a[i]; j--) //最简单的01背包
if(dp[j]<dp[j-a[i]]+a[i])
{
// dp[j]=maxs(dp[j],dp[j-a[i]]+a[i]); //这样写会报错!!!我也不知道为什么= = 可能是电脑的问题,纠结了一下午
dp[j]=dp[j-a[i]]+a[i];
s[i][j] =;
}
for(i=,j=dp[m]; i<n,j>; i++) //输出路径
{
if(s[i][j])
{
printf("%d ",a[i]);
j-=a[i];
}
}
printf("sum:%d\n",dp[m]);
}
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背包)

    //路径记录方法:若是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背包+输出方案)

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

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

  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. 如何在CentOS 7 / Fedora 31/30/29上安装ELK Stack

    原文地址:https://computingforgeeks.com/how-to-install-elk-stack-on-centos-fedora/ 原作者: Josphat Mutai 译者: ...

  2. golang字符串常用系统函数

  3. WPF Popup实现拖动

    问题一.popup总是置顶,遮挡其他窗口 最近发现popup设置打开后,总是会遮挡其他窗口,而我们只想让它仅仅在应用程序的上一层即可,并不像让它在最上面 解决方案是继承Popup重新定义控件Popup ...

  4. 超线程(Hyper-Threading)

    运行方式 每个单位时间内,一个CPU只能处理一个线程(操作系统:thread),以这样的单位进行,如果想要在一单位时间内处理超过一个线程是不可能的,除非是有两个CPU的实体单元.双核心技术是将两个一样 ...

  5. C++学习笔记(2)---2.5 C++函数编译原理和成员函数的实现

    转载自:http://c.biancheng.NET/cpp/biancheng/view/2996.html点击打开链接 从上节的例子可以看出,对象的内存模型中只保留了成员变量,除此之外没有任何其他 ...

  6. 【Pyqt5】自定义信号简单原理(易懂版),多窗口交互,传输数据,调用方法

    PS:如果你想在2窗口调用1窗口的内部方法,或者在2窗口传递数据给1窗口数据,本片博客可以放心食用 主窗口: class MainWindow(QWidget,Ui_MainFrom): insert ...

  7. qt,pro文件中用于平台区分的写法

    qt,pro文件中用于平台区分的写法 切记: 大括号和平台需要在同一行中,否则会失效 unix { TARGET = appname } macx { TARGET = appname2 } win3 ...

  8. 【水滴石穿】AB-B-Clone

    地址: 源码 运行效果 无别的效果,代码如下 //index.js /** * @format * @lint-ignore-every XPLATJSCOPYRIGHT1 */ import {Ap ...

  9. 【风马一族_php】NO4_php基础知识

    原文来自:http://www.cnblogs.com/sows/p/6017018.html(博客园的)风马一族 侵犯版本,后果自负 回顾 运算符:算术运算符.逻辑运算符.比较运算符.位运算符.赋值 ...

  10. PHP汉字验证码

    转自:http://www.blhere.com/1167.html 12345678910111213141516171819202122232425262728293031323334353637 ...