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
 
 
Tip:注意两次循环的方向是相反的。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std; int dp[],w[];
int path[][]; int main()
{
int m,n;
while(cin>>m>>n)
{
memset(dp,,sizeof(dp));
memset(path,,sizeof(path));
memset(w,,sizeof(w));
for(int i=; i<=n; i++)
cin>>w[i];
for(int i=n; i>=; i--)
{
for(int j=m; j>=w[i]; j--)
{
if(dp[j-w[i]]+w[i]>dp[j])
{
dp[j]=dp[j-w[i]]+w[i];
path[i][j]=;
}
}
}
for(int i=,j=m; i<=n; i++)
{
if(path[i][j]==)
{
cout<<w[i]<<' ';
j-=w[i];
}
}
cout<<"sum:"<<dp[m]<<endl;
}
return ;
}

最新文章

  1. ajax+php数据增加查询获取删除
  2. WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED解决方法
  3. inuit.css – 基于 Sass 的强大,可扩展的 CSS 框架
  4. 【实(dou)力(bi)首(mai)发(meng)】第四次CCF软件能力认证题解
  5. Nodejs Express 4.X 中文API 1--- Application篇
  6. title:EL表达式获取Map里面的数值失败的问题
  7. jQuery来源学习笔记:整体结构
  8. vue 2.0 无法编译ES6语法
  9. 利用SSH端口转发实现跨机器直接访问
  10. Git学习:如何登陆以及创建本地代码仓库、并提交本地代码至Github(最简单方法)
  11. 一分钟内搭建全web的API接口神器json-server详解
  12. [Database.System.Concepts(6th.Edition.2010)].Abraham.Silberschatz. Ch8学习笔记
  13. linux笔记-多服务器同时执行相同命令
  14. mybatis通用mapper源码解析(二)
  15. sublime text2+Ctags+Cscope替代Source Insight
  16. Ext.require 的作用(转)
  17. 【sping揭秘】13、Spring AOP一世&amp;二世
  18. Win7 user profile cant logon
  19. 第一次作业——MathExam285
  20. Linux命令之hostname - 显示或设置主机名

热门文章

  1. HDU 1693 Eat the Trees(插头DP、棋盘哈密顿回路数)+ URAL 1519 Formula 1(插头DP、棋盘哈密顿单回路数)
  2. js验证手机号
  3. 【processing】小代码5
  4. 【linux】ubuntu stmp服务器配置
  5. iOS进阶面试题----Block部分
  6. javascript void运算符
  7. HttpClient 3.X 4.3 4.x超时设置
  8. python基础——返回函数
  9. poj1417(种类并查集+dp)
  10. 初学iPad开发入门