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. 洛谷 P1910 L国的战斗之间谍(水题日常)

    题目背景 L国即将与I国发动战争!! 题目描述 俗话说的好:“知己知彼,百战不殆”.L国的指挥官想派出间谍前往I国,于是,选人工作就落到了你身上. 你现在有N个人选,每个人都有这样一些数据:A(能得到 ...

  2. 云梯互联:所有主机已全面支持免费SSL!附小白配置教程。

    HTTPS和HTTP的区别:1.HTTPS是加密传输协议,HTTP是名文传输协议;2.HTTPS需要用到SSL证书,而HTTP不用;3.HTTPS比HTTP更加安全,对搜索引擎更友好,利于SEO4. ...

  3. SQLite – HAVING 子句

    SQLite – HAVING子句 HAVING使您能够指定过滤条件哪一组结果出现在最终的结果. WHERE子句的地方条件选定的列, 在有HAVING 子句的地方 就有GROUP BY子句包含的条件组 ...

  4. Python游戏-实现键盘控制功能

    要想实现键盘控制作用,就需要给游戏键盘监听事件利用pygame模块的key.get_pressed()方法,来检测按键是否按下 key_press =pygame.key.get_pressed() ...

  5. WPF知识点全攻略01- WPF相对WinFrom的优缺点

    对比WPF和WinFrom前,先来了解下GUI现阶段在用的其他一些开发技术: MFC:微软基础类库,以C++的形式封装了Windows API,加上一些实用工具类. QT:奇趣科技开发的跨平台C++图 ...

  6. 【转】关于编写WPF UserControl时提示The name 'InitializeComponent' does not exist in the current contextr的解决!

    1.打开.csproj(工程)文件. 2.找到<Import Project="$(MSBuildBinPath)/Microsoft.CSharp.targets" /&g ...

  7. Java中的BIO,NIO,AIO分别是什么

    BIO:同步并阻塞,服务器实现模式为一个连接一个线程,即客户端有连接请求时服务器端就需要启动一个线程进行处理,如果这个连接不做任何事情会造成不必要的线程开销,当然可以通过线程池机制改善.BIO方式适用 ...

  8. HTML页面中5种超酷的伪类选择器:hover效果

    想在自己的网站中应用超酷的hover效果吗?也许你可以从如下的这些实例中获得一些灵感,如果你喜欢这些效果,也可以直接拷贝代码并应用到你的站点. 给平淡的站点带来活力 hover效果能给网页增加一些动态 ...

  9. 点击增加删除class

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  10. 01Ping程序的设计

    1.Ping程序设计具体设计任务 1.1 实验目的 PING程序是我们使用的比较多的用于测试网络连通性的程序.PING程序基于ICMP,使用ICMP的回送请求和回送应答来工作.由计算机网络课程知道,I ...