CD(01背包)
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
题目大意:
给定n,m,然后给m个数字,组成尽可能接近n的和,以及组成部分具体数字
解题思路:
01背包模板,需要进行记录路径.
#include <bits/stdc++.h>
using namespace std;
const int N=1e4+;
int dp[N],val[];
int vis[][N];///记录路径
int main()
{
int n,m;
while(cin>>n>>m)
{
memset(dp,,sizeof dp);
memset(vis,,sizeof vis);
for(int i=;i<m;i++)
cin>>val[i];
for(int i=;i<m;i++)
{
for(int j=n;j>=val[i];j--)
{
if(dp[j]<=dp[j-val[i]]+val[i])
dp[j]=dp[j-val[i]]+val[i],vis[i][j]=;
}
}
int j=n;
for(int i=m-;i>=;i--)///从后往前的线路是唯一的
{
if(vis[i][j])
cout<<val[i]<<' ',j-=val[i];
}
cout<<"sum:"<<dp[n]<<'\n';
}
}
CD(01背包)的更多相关文章
- UVA 624 ---CD 01背包路径输出
DescriptionCD You have a long drive by car ahead. You have a tape recorder, but unfortunately your b ...
- UVA624 CD,01背包+打印路径,好题!
624 - CD 题意:一段n分钟的路程,磁带里有m首歌,每首歌有一个时间,求最多能听多少分钟的歌,并求出是拿几首歌. 思路:如果是求时常,直接用01背包即可,但设计到打印路径这里就用一个二维数组标记 ...
- UVA 624 - CD (01背包 + 打印物品)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVA 624 CD (01背包)
//路径记录方法:若是dp[j-value[i]]+value[i]>dp[j]说明拿了这个东西,标志为1, //for循环标志,发现是1,就打印出来,并把背包的容量减少,再在次容量中寻找标志: ...
- UVA--624 CD(01背包+路径输出)
题目http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- uva 624 CD 01背包打印路径
// 集训最终開始了.来到水题先 #include <cstdio> #include <cstring> #include <algorithm> #includ ...
- UVA 624 CD(01背包+输出方案)
01背包,由于要输出方案,所以还要在dp的同时,保存一下路径. #include <iostream> #include <stdio.h> #include <stri ...
- 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 ...
- uva 624 CD (01背包)
CD You have a long drive by car ahead. You have a tape recorder, but unfortunately your best musi ...
随机推荐
- Python Selenium设计模式 - PO设计模式
整理一下python selenium自动化测试实践中使用较多的po设计模式. 为什么要用PO 基于python selenium2开始开始ui自动化测试脚本的编写不是多么艰巨的任务.只需要定位到元素 ...
- 获得select被选中option的value和text
一:JavaScript原生的方法 1:得到select对象: var myselect=document.getElementById(“test”); 2:得到选中项的索引:var index=m ...
- web前端怎么样才能入门
web前端怎么样才能入门,首先我们要从什么是初级web前端工程师说起: 按照我的想法,我把前端工程师分为了入门.初级.中级.高级这四个级别: 入门级别指的是了解什么是前端(前端到底是什么其实很多人还是 ...
- 毕业设计:HomeFragment(一)
一.主要思路 主要是通过ListView实现. 考虑到以后会添加长按修改功能,所以好几个地方都是用的FramLayout,而且CheckBox初始状态是被隐藏的.给ListView添加OnItemCl ...
- HttpURLConnection读取http信息
废话不多说,直接上code. package mytest; import java.io.BufferedReader; import java.io.IOException; import jav ...
- Jmeter官网文档翻译
Jmeter目录 入门 1.0概述 测试计划建设 负载测试运行 负载测试分析 开始吧 1.1要求 1.1.1 Java版本 1.1.2操作系统 1.2可选 1.2.1 Java编译器 1.2.2 SA ...
- 洛谷 P1163 银行贷款
题目描述 当一个人从银行贷款后,在一段时间内他(她)将不得不每月偿还固定的分期付款.这个问题要求计算出贷款者向银行支付的利率.假设利率按月累计. 输入输出格式 输入格式: 输入文件仅一行包含三个用空格 ...
- oracle的Hint
与优化器模式相关的Hint 1 ALl_ROWS 让优化器启用CBO /*+ all_rows */ 2 first_rows(n) 让优化器启用CBO 模式,而且得到目标sql的执行计 ...
- libs/tools.js stringToDate dateToString 日期字符串转换函数
libs/tools.js stringToDate dateToString 日期字符串转换函数 import { stringToDate } from '@/libs/tools.js' e ...
- css 两列 左侧列固定 width: 100px; float: left; 右侧列自适应 margin-left:100px; 注意要用在div上的style
css 两列 左侧列固定 width: 100px; float: left; 右侧列自适应 margin-left:100px; 注意要用在div上的style .con1{ width: 100p ...