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 这题主要是输出路径有问题
就是把路径记录下来
其他都好写 现在有两种方法
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<stdlib.h>
#include<queue>
#include<iostream>
using namespace std; int dp[],vis[][];
int f[],k=,w[]; void prime(int n,int m)
{
if(n==||m<)
return;
if(vis[n][m]==)
prime(n-,m);
else
{
prime(n-,m-w[n]);
f[k++]=w[n];;
}
}
int main()
{
int m,n,i,j;
while(scanf("%d",&m)!=EOF)
{
memset(w,,sizeof(w));
scanf("%d",&n);
for(i=;i<=n;i++)
{
scanf("%d",&w[i]);
}
memset(dp,,sizeof(dp));
memset(vis,,sizeof(vis));
for(i=;i<=n;i++)
{
for(j=m;j>=w[i];j--)
{
if(dp[j]<dp[j-w[i]]+w[i])
{
dp[j]=dp[j-w[i]]+w[i];
vis[i][j]=;
}
}
}
k=;
memset(f,,sizeof(f));
prime(n,m);
for(i=;i<k;i++)
printf("%d ",f[i]);
printf("sum:%d\n",dp[m]);
}
return ;
}
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<stdlib.h>
#include<queue>
#include<iostream>
using namespace std; int dp[],vis[][];
int main()
{
int m,n,w[],i,j;
while(scanf("%d",&m)!=EOF)
{
scanf("%d",&n);
for(i=;i<=n;i++)
{
scanf("%d",&w[i]);
}
memset(dp,,sizeof(dp));
memset(vis,,sizeof(vis));
for(i=n;i>0;i--)
{
for(j=m;j>=w[i];j--)
{
if(dp[j]<dp[j-w[i]]+w[i])
{
dp[j]=dp[j-w[i]]+w[i];
vis[i][j]=;
}
}
}
for(i=,j=dp[m];i<=n&&j>;i++)
{
if(vis[i][j])
{
printf("%d ",w[i]);
j=j-w[i];
}
}
printf("sum:%d\n",dp[m]);
}
return ;
}

CD-----UVa624(01背包+输出路径)的更多相关文章

  1. Codeforces Gym-102219 2019 ICPC Malaysia National E. Optimal Slots(01背包+输出路径)

    题意:给你一个体积为\(T\)的背包,有\(n\)个物品,每个物品的价值和体积都是是\(a_{i}\),求放哪几个物品使得总价值最大,输出它们,并且输出价值的最大值. 题解:其实就是一个01背包输出路 ...

  2. UVA624(01背包记录路径)

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

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

    题目链接: https://vjudge.net/problem/UVA-624 题目大意: 这道题给定一个时间上限,然后一个数字N,后面跟着N首歌的时间长度,要我们 求在规定时间w内每首歌都要完整的 ...

  4. vijos 1071 01背包+输出路径

    描述 过年的时候,大人们最喜欢的活动,就是打牌了.xiaomengxian不会打牌,只好坐在一边看着. 这天,正当一群人打牌打得起劲的时候,突然有人喊道:“这副牌少了几张!”众人一数,果然是少了.于是 ...

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

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

  6. UVA 624 CD[【01背包】(输出路径)

    <题目链接> 题目大意: 你要录制时间为N的带子,给你一张CD的不同时长的轨道,求总和不大于N的录制顺序 解题分析: 01背包问题,需要注意的是如何将路径输出. 由于dp时是会不断的将前面 ...

  7. UVA 624 CD(01背包,要记录路径)

    题意: 有n张CD(n<=20),每张能播放的时长不同.给定一个时长限制t,挑出部分的CD使得总播放时间最长.顺便输出路径! 思路: 重点在输出路径,否则这题很普通.那就要用二维数组记录每个CD ...

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

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

  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. oid和节点名称

    由于单篇文档最大字限制是40000个字符,不能将OID附上,因此写出我是如何得到这些OID的. 1.安装NET-SNMP yum install net-snmp yum install net-sn ...

  2. Eclipse被卡死了或者失去响应了后分析根源的一个小技巧

    提升程序员工作效率的工具/技巧推荐系列 推荐一个功能强大的文件搜索工具SearchMyFiles 介绍一个好用的免费流程图和UML绘制软件-Diagram Designer 介绍Windows任务管理 ...

  3. CAD交互绘制圆弧(网页版)

    在CAD设计时,需要绘制圆弧,用户可以在图面点圆弧起点,圆弧上的一点和圆弧的终点,这样就绘制出圆弧. 主要用到函数说明: _DMxDrawX::DrawArc2 由圆弧上的三点绘制一个圆弧.详细说明如 ...

  4. python json.loads json.dumps的区别

    json.loads() 是将字符串传化为字典 json.dumps () 是将字典转化为字符串 >>> dict = "{8:'bye', 'you':'coder'}& ...

  5. 多线程test

    import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Map; import ...

  6. 10MongoDB

    一.  介绍MongoDB 1. NoSQL 1)“NoSQL”⼀词最早于1998年被⽤于⼀个轻量级的关系数据库的名字 随着web2.0的快速发展, NoSQL概念在2009年被提了出来 2)NoSQ ...

  7. <Spring Data JPA>一 JPA原生

    1.pom依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...

  8. 剑指Offer(书):用两个栈实现队列

    题目:用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 分析:入栈时只入栈1,出栈为栈2:若栈2不为空,直接出栈:否则,将栈1中的值依次入栈2,之后栈2出栈 Sta ...

  9. LeetCode(25)Reverse Nodes in k-Group

    题目 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...

  10. java环境配置—配置Tomcat8环境

    先安装JDK,配置好jdk环境后再配置Tomcat 8 配置环境变量: TOMCAT_HOME:D:\Program Files\apache-tomcat-8.0.28 CATALINA_HOME: ...