DescriptionCD

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<stdio.h>
#include<string.h> #define N 1100
#define max(a,b) (a>b?a:b) int v[N], dp[N][N], W, n; void Path(int n, int W)
{
if(n==) return ; if(dp[n-][W]==dp[n][W])
Path(n-, W);
else
{
Path(n-, W-v[n]);
printf("%d ", v[n]);
}
} int main()
{
while(scanf("%d%d", &W, &n)!=EOF)
{
int i, j;
memset(v, , sizeof(v));
memset(dp, , sizeof(dp));
for(i=; i<=n; i++)
scanf("%d", &v[i]); for(i=; i<=n; i++)
for(j=; j<=W; j++)
{
if(v[i]>j)
dp[i][j] = dp[i-][j];
else
dp[i][j] = max(dp[i-][j], dp[i-][j-v[i]]+v[i]);
} Path(n, W); printf("sum:%d\n", dp[n][W]);
}
return ;
}

一维数组:

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<string>
#include<stack>
#include<vector>
#include<map>
using namespace std;
#define N 2510
#define INF 0x3f3f3f3f
#define met(a, b) memset(a, b, sizeof(a))
typedef long long LL; int n, m, a[N], dp[N], path[N][N]; int main()
{
while(scanf("%d %d", &m, &n)!=EOF)
{
met(dp, );
met(path, );
for(int i=; i<n; i++)
scanf("%d", &a[i]);
for(int i=; i<n; i++)
{
for(int j=m; j>=a[i]; j--)
{
///dp[j] = max(dp[j], dp[j-a[i]]+a[i]);
if(dp[j] < dp[j-a[i]]+a[i])
{
path[i][j] = ;
dp[j] = dp[j-a[i]]+a[i];
}
}
}
int j = m, k = , ans[N] = {};
for(int i=n-; i>=; i--)
{
if(path[i][j])
{
ans[k++] = a[i];
j -= a[i];
}
}
for(int i=k-; i>=; i--)
printf("%d ", ans[i]);
printf("sum:%d\n", dp[m]);
}
return ;
}

UVA 624 ---CD 01背包路径输出的更多相关文章

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

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

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

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

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

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

  4. UVA 624 CD (01背包)

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

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

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

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

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

  7. uva 624 CD (01背包)

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

  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. uva624 CD (01背包+路径的输出)

    CD Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Practice UVA 624 ...

随机推荐

  1. Altera特殊管脚的使用(适用全系列Altera FPGA,MSEL区别除外)-来自altera论坛

    1.I/O, ASDO  在AS 模式下是专用输出脚,在PS 和JTAG 模式下可以当I/O 脚来用.在AS 模式下,这个脚是CII 向串行配置芯片发送控制信号的脚.也是用来从配置芯片中读配置数据的脚 ...

  2. perl chomp 函数的真正作用

    之前一直以为chomp函数只是去掉字符串末尾的\n, 但是今天写程序遇到一个bug,最后的原因就处在chomp上: 读取fasta文件,内容如下: >1 ATGCTAGCTACGTACGTACG ...

  3. WAMP环境下配置虚拟主机

    1.编辑httpd.conf,查找#Include conf/extra/httpd-vhosts.conf,把前面注释符号“#”删掉 2.编辑httpd-vhosts.conf文件, <Vir ...

  4. Java基础--生成验证码

    HTML <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnco ...

  5. c++ timeval

    struct timeval结构体   struct timeval结构体在time.h中的定义为:struct timeval{__time_t tv_sec;        /* Seconds. ...

  6. Webpack vs Gulp(转载)

    理想的前端开发流程 在说构建工具之前得先说说咱期望的前端开发流程是怎样的? 写业务逻辑代码(例如 es6,scss,pug 等) 处理成浏览器认识的(js,css,html) 浏览器自动刷新看到效果 ...

  7. java程序后台报错java.net.SocketException: Too many open files

    问题描述: 今天一个同事反映程序有问题,让帮忙查看后台日志,发现后台日志报错的信息如下: java.net.SocketException: Too many open files at java.n ...

  8. sublime text 2 破解

    本文是介绍sublime text 2.0.2 build 2221 64位 的破解 在你使用sublime时可能经常出现下图: 这是在提醒你注册 在工具栏上点击help->Enter Lice ...

  9. 常用hive的CLI命令

    1.show tables  --查看所有表 2.desc tabname --查看表信息 3.dfs -ls 目录  查看hdfs上面的文件  dfs -lsr /user  递归显示目录/user ...

  10. django model 数据类型

    转自:http://www.cnblogs.com/lhj588/archive/2012/05/24/2516040.html Django 通过 models 实现数据库的创建.修改.删除等操作, ...