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. [转] Figuring out why my SVCHOST.EXE is at 100% CPU without complicated tools in Windows 7

    (转自:Figuring out why my SVCHOST.EXE is at 100% CPU without complicated tools in Windows 7 - Scott Ha ...

  2. (转)SpringMVC学习(十)——SpringMVC与前台的json数据交互

    http://blog.csdn.net/yerenyuan_pku/article/details/72514022 json数据格式在接口调用中.html页面中比较常用,json格式比较简单,解析 ...

  3. MATLAB GUI制作快速入门

    创建空白的GUI在MATLAB命令行中输入guide新建GUI,选择Blank GUI (Default),点击确定后就生成了一个空白的GUI制作界面,如下图所示 图1制作GUI的具体过程简单加法器将 ...

  4. VC++绘制金刚石(MFC)

    void CTxx1View::OnDraw(CDC* pDC){ CTxx1Doc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add d ...

  5. Linux之基础命令——文件搜索

    grep(匹配符合条件的字符串) 无参:显示匹配行 -c:显示匹配行数 -e 字符串:匹配特殊字符串,如-开头 -i:忽略大小写 -v:输出不匹配行 -w:匹配指定字符串 可以和别的命令通过" ...

  6. 把txt格式数据制作成xml数据

    txt格式数据: 代码: s1=""" <object> <name>{0}</name> <pose>Unspecifi ...

  7. HDU4300 Clairewd’s message(拓展kmp)

    Problem Description Clairewd is a member of FBI. After several years concealing in BUPT, she interce ...

  8. 【2019.6.2】python:json操作、函数、集合、random()等

    一.json操作: json就是一个字符串,从文件中读取json,必须是json格式.j'son串中必须是双引号,不能有单引号,单引号不能转换 1.1使用: import json #使用json先引 ...

  9. KVM中的网络简介(qemu-kvm)

    emu-kvm主要向客户机提供了如下4种不同模式的网络: 1)基于网桥(bridge)的虚拟网卡 2)基于NAT(Network Addresss Translation)的虚拟网络 3)QEMU内置 ...

  10. 【linux 06】 linux中的用户权限、文件权限与目录权限

    1.用户及用户组的概念: 1.文件所有者 2.用户组 3.用户 以root登录Linux之后,执行ls -al,会看到有关文件属性的信息 -rw-r--r--,第1个字符代表这个文件是“目录,文件或链 ...