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
题目大意:
给定n,m,然后给m个数字,组成尽可能接近n的和,以及组成部分具体数字
解题思路:
01背包模板,需要进行记录路径.
#include <bits/stdc++.h>
using namespace std;
const int N=1e4+;
int dp[N],val[];
int vis[][N];///记录路径
int main()
{
int n,m;
while(cin>>n>>m)
{
memset(dp,,sizeof dp);
memset(vis,,sizeof vis);
for(int i=;i<m;i++)
cin>>val[i];
for(int i=;i<m;i++)
{
for(int j=n;j>=val[i];j--)
{
if(dp[j]<=dp[j-val[i]]+val[i])
dp[j]=dp[j-val[i]]+val[i],vis[i][j]=;
}
}
int j=n;
for(int i=m-;i>=;i--)///从后往前的线路是唯一的
{
if(vis[i][j])
cout<<val[i]<<' ',j-=val[i];
}
cout<<"sum:"<<dp[n]<<'\n';
}
}

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. UVA624 CD,01背包+打印路径,好题!

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

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

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

  4. UVA 624 CD (01背包)

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

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

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

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

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

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

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

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

  9. uva 624 CD (01背包)

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

随机推荐

  1. 题解报告:hdu 2141 Can you find it?(二分)

    Problem Description Give you three sequences of numbers A, B, C, then we give you a number X. Now yo ...

  2. python_函数嵌套(4)

    第1章 名称空间 1.1 定义 1.2 变量运行流程 1.3 临时名称空间 1.4 python三种名称空间 第2章 作用域 2.1 作用域分类 2.2 加载顺序 2.3 取值顺序 函数嵌套 2.4 ...

  3. DNS正、反向解析+负载均衡+智能DNS+密钥认证

    主机名 IP 软件包 系统版本 内核版本 实验环境 master 192.168.30.130 bind.x86_64 32:9.8.2-0.17.rc1.el6_4.6 bind-chroot.x8 ...

  4. AJPFX谈Java 性能优化之基本类型 vs 引用类型

    ★名词定义 先明确一下什么是“基本类型”,什么是“引用类型”. 简单地说,所谓基本类型就是 Java 语言中如下的8种内置类型: booleancharbyteshortintlongfloatdou ...

  5. 微信小程序 开放能力学习

    1. 用户信息小程序登录使用微信的个人信息快速搭建用户体系,登录逻辑:小程序向微信获取code 给服务端生成用户. 说明1. 小程序端调用 wx.login() 获取临时登录凭证 code,并传到服务 ...

  6. 【学习笔记】深入理解js闭包

    本文转载: 一.变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域. 变量的作用域无非就是两种:全局变量和局部变量. Javascript语言的特殊之处,就在于函数内部可以直接 ...

  7. Android Platform Version 和 API Level对照

    Platform Version API Level VERSION_CODE Notes Android 5.1 22 LOLLIPOP_MR1 Platform Highlights Androi ...

  8. 如何创建你的第一个手机APP?

    本文使用helloworld来作为android的入门项目,通过这个最简单的项目来帮助大家了解android程序开发包含哪些部分,以及如何运行android程序,本次开发android程序的工具是ec ...

  9. synchronize早已经没那么笨重

    我发现一些同学在网络上有看不少synchronize的文章,可能有些同学没深入了解,只看了部分内容,就急急忙忙认为不能使用它,很笨重,因为是采用操作系统同步互斥信号量来实现的.关于这类的对于synch ...

  10. java比较日期大小及日期与字符串的转换【SimpleDateFormat操作实例】

    java比较日期大小及日期与字符串的转换[SimpleDateFormat操作实例] package com.ywx.test; import java.text.ParseException; im ...