CD

Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu

Submit Status Practice UVA 624

Appoint description:

Description

Download as PDF

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

Miguel A. Revilla

2000-01-10

//把题目转化为背包问题。即选择尽量多的CD曲子去装满这个时间区间
#include<iostream>
#include<algorithm>
#include<map>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<cstring>
#include<stack>
#include<string>
#include<fstream>
#define pb(s) push_back(s)
#define cl(a,b) memset(a,b,sizeof(a))
#define bug printf("===\n");
using namespace std;
typedef vector<int> VI;
#define rep(a,b) for(int i=a;i<b;i++)
#define rep_(a,b) for(int i=a;i<=b;i++)
#define P pair<int,int>
#define bug printf("===\n");
#define PL(x) push_back(x)
#define X first
#define Y second
#define vi vector<int>
#define rep(i,x,n) for(int i=x;i<n;i++)
#define rep_(i,x,n) for(int i=x;i<=n;i++)
const int maxn=15000;
const int inf=999999999;
typedef long long LL;
int a[maxn];
int dp[maxn];
int f[maxn][maxn];
int cnt;
int ans[maxn];
void print(int n,int m){//是dp的逆过程。递归回去
if(m==0)return ;
if(f[m][n]){
print(n-a[m],m-1);
ans[cnt++]=a[m];
}
else {
print(n,m-1);
}
}
int main(){
int n,m;
while(~scanf("%d%d",&n,&m)){
for(int i=1;i<=m;i++){
scanf("%d",&a[i]);
}
for(int i=0;i<=n;i++){
dp[i]=0;
for(int j=0;j<=n;j++){
f[i][j]=0;
}
}
for(int i=1;i<=m;i++){
for(int j=n;j>=a[i];j--){
dp[j]=max(dp[j],dp[j-a[i]]+a[i]);
if(dp[j]==dp[j-a[i]]+a[i]){
f[i][j]=1;
}
}
}
cnt=0;
print(n,m);
for(int i=0;i<cnt;i++){
printf("%d ",ans[i] );
}
printf("sum:%d\n",dp[n]);
}
return 0;
}

uva624 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背包路径输出

    DescriptionCD You have a long drive by car ahead. You have a tape recorder, but unfortunately your b ...

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

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

  4. CD(01背包)

    You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is o ...

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

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

  6. Coderfroces 864 E. Fire(01背包+路径标记)

    E. Fire http://codeforces.com/problemset/problem/864/E Polycarp is in really serious trouble — his h ...

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

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

  8. UVA 624 CD【01背包+路径记录】

    You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is o ...

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

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

随机推荐

  1. oracle 两表关联查询

      oracle 两表关联查询 CreationTime--2018年7月4日17点27分 Author:Marydon 情景描述 查询学生表student,sname,sex,age信息及所在班级c ...

  2. docker sshd image problem, session required pam_loginuid.so, cann't login

    在使用sshd docker 镜像时, 发现一个比较诡异的问题, 有些启动的容器可以连接, 有些不能. 例如 :  启动2个容器(这两个容器都有问题) :  [root@localhost ~]# d ...

  3. oracle中如何设置主键并且让其自动增长

    由于oracle中是没有自动增长的的,需要自己去进行写触发器等方式去进行设置: 找了一下他人写的,有两种方法可以设置主键,一种是自增长主键,另一种就是生成唯一序列. 一.自增长主键 我创建一个用户的信 ...

  4. 【php写日志】php将日志写入文件

    php 写内容到文件,把日志写到log文件 <?php header("Content-type: text/html; charset=utf-8"); /******** ...

  5. 【LeetCode】97. Interleaving String

    Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...

  6. C#USB录像视频拍照-代码

    论坛帖:http://bbs.csdn.net/topics/390536016 using System; using System.Collections.Generic; using Syste ...

  7. MS SQL自定义函数IsPositiveInteger MS SQL自定义函数IsNumeric 水晶报表使用IEnumerable<T>数据源

    MS SQL自定义函数IsPositiveInteger   判断字符串是否为正整数,0开始的的数字不算. SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON ...

  8. pycharm 控制台字体背景颜色设定让你美到爆

    还在为你控制台字体颜色不入眼而发愁吗? 在某些特殊时刻想要改变下字体的颜色却无从下手而发愁吗? 那你就进来看一看,瞧一瞧,之需看2分钟,花2分钟理解,花3分钟练练手就能让你的控制能字体炫酷起来! 一. ...

  9. C#:数学运算(待补充)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MyCo ...

  10. [Windows驱动开发](三)基础知识——驱动例程

    一.NT式驱动的基本例程 1. 驱动入口函数——DriverEntry // 驱动程序的一般性定义 NTSTATUS DriverEntry(IN PDRIVER_OBJECT pDriverObje ...