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. 媒体查询(media):设置PC端网页居中显示

    @media screen and (min-width: 768px){ body{ background-color: #EAEAEA; } #fater{ width: 640px; margi ...

  2. python安装第三方模块

    1.pip 安装命令: pip install 模块名由于国外网站不稳定可能会出现超时的情况,我们可以自己指定下载源命令如下临时修改 pip install 模块名 -i https://pypi.t ...

  3. win下在虚拟机安装CentOS 7 Linux系统

    准备: CentOS 7下载地址(我下的是everthing版本):https://www.centos.org/download/ 一.首先下载安装虚拟机VMware 地址官网下载即可. 二.安装操 ...

  4. Java1.8新特性 - Stream流式算法

    一. 流式处理简介   在我接触到java8流式数据处理的时候,我的第一感觉是流式处理让集合操作变得简洁了许多,通常我们需要多行代码才能完成的操作,借助于流式处理可以在一行中实现.比如我们希望对一个包 ...

  5. SDUT-3402_数据结构实验之排序五:归并求逆序数

    数据结构实验之排序五:归并求逆序数 Time Limit: 50 ms Memory Limit: 65536 KiB Problem Description 对于数列a1,a2,a3-中的任意两个数 ...

  6. C++不支持默认的int

    VS: 工程属性->C/C++->命令行->输入 /wd4430

  7. python小练习--银行取款

    银行取款 今天练习的小程序: #!/usr/bin/env python #-*- coding:utf-8 -*- import time tag=True while tag: name=inpu ...

  8. poj3532 Round Numbers

      题意:给出l.r,求区间[l,r]内二进制中0的个数大于等于1的个数的数字有多少个. 简单的数位dp. //Serene #include<algorithm> #include< ...

  9. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第六章:在Direct3D中绘制

    原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第六章:在Direct3D中绘制 代码工程地址: https://gi ...

  10. TIJ——Chapter Twelve:Error Handling with Exception

    Exception guidelines Use exceptions to: Handle problems at the appropriate level.(Avoid catching exc ...