Coins

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10961    Accepted Submission(s): 4418

Problem Description
Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One day Hibix opened purse and found there were some coins. He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without change) and he known the price would not more than m.But he didn't know the exact price of the watch.

You are to write a program which reads n,m,A1,A2,A3...An and C1,C2,C3...Cn corresponding to the number of Tony's coins of value A1,A2,A3...An then calculate how many prices(form 1 to m) Tony can pay use these coins.

 
Input
The input contains several test cases. The first line of each test case contains two integers n(1 ≤ n ≤ 100),m(m ≤ 100000).The second line contains 2n integers, denoting A1,A2,A3...An,C1,C2,C3...Cn (1 ≤ Ai ≤ 100000,1 ≤ Ci ≤ 1000). The last test case is followed by two zeros.
 
Output
For each test case output the answer on a single line.
 
Sample Input
3 10
1 2 4 2 1 1
2 5
1 4 2 1
0 0
 
Sample Output
8
4
该题卡多重背包,数组开的不够大WA,开的太大MLE。
下面是用多重背包做该题的思路。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int A[],C[];
int n,m;
int dp[];
int w[];
int used[];
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
if(!n&&!m)
break;
memset(dp,,sizeof(dp));
memset(used,,sizeof(used));
for(int i=;i<n;i++) scanf("%d",&A[i]);
for(int i=;i<n;i++) scanf("%d",&C[i]); int cnt=;
for(int i=;i<n;i++)
{
int e=;
while(e<C[i])
{
w[cnt++]=e*A[i];
C[i]-=e;
e<<=;
}
if(C[i]>)
w[cnt++]=C[i]*A[i];
} for(int i=;i<cnt;i++)
for(int j=m;j>=w[i];j--)
dp[j]=max(dp[j],dp[j-w[i]]+w[i]); int res=;
for(int i=m;i>=;i--)
if(!used[dp[i]])
{
used[dp[i]]=;
res++;
}
printf("%d\n",res);
} return ;
}
 
下面转化为多重部分和问题求解。
 
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int A[],C[];
int n,m;
int dp[];
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
if(!n&&!m)
break;
memset(dp,-,sizeof(dp));
for(int i=;i<n;i++) scanf("%d",&A[i]);
for(int i=;i<n;i++) scanf("%d",&C[i]);
dp[]=;
for(int i=;i<n;i++)
for(int j=;j<=m;j++)
if(dp[j]>=)
dp[j]=C[i];
else if(j<A[i]||dp[j-A[i]]<)
dp[j]=-;
else
dp[j]=dp[j-A[i]]-;
int res=;
for(int i=;i<=m;i++)
if(dp[i]>=)
res++;
printf("%d\n",res);
} return ;
}

HDU2844(多重部分和)的更多相关文章

  1. POJ_1742_Coins_(动态规划,多重部分和)

    描述 http://poj.org/problem?id=1742 n种不同面额的硬币 ai ,每种各 mi 个,判断可以从这些数字值中选出若干使它们组成的面额恰好为 k 的 k 的个数. 原型: n ...

  2. COJ 0557 4013多重部分和问题

    4013多重部分和问题 难度级别:B: 运行时间限制:2000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 n种大小不同的数字 Ai,每种各Mi个,判断是否可以从 ...

  3. 编程算法 - 多重部分和问题 代码(C)

    多重部分和问题 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 有n种不同大小的数字a, 每种各m个. 推断能否够从这些数字之中选出若干使它们的 ...

  4. 多重部分和 poj1742

    Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...

  5. 题解报告:hdu 2844 & poj 1742 Coins(多重部分和问题)

    Problem Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. On ...

  6. 题解报告:hdu 1059 Dividing(多重背包、多重部分和问题)

    Problem Description Marsha and Bill own a collection of marbles. They want to split the collection a ...

  7. DP的初级问题——01包、最长公共子序列、完全背包、01包value、多重部分和、最长上升子序列、划分数问题、多重集组合数

    当初学者最开始学习 dp 的时候往往接触的是一大堆的 背包 dp 问题, 那么我们在这里就不妨讨论一下常见的几种背包的 dp 问题: 初级的时候背包 dp 就完全相当于BFS DFS 进行搜索之后的记 ...

  8. POJ1742 coins 动态规划之多重部分和问题

    原题链接:http://poj.org/problem?id=1742 题目大意:tony现在有n种硬币,第i种硬币的面值为A[i],数量为C[i].现在tony要使用这些硬币去买一块价格不超过m的表 ...

  9. POJ 1742 Coins ( 经典多重部分和问题 && DP || 多重背包 )

    题意 : 有 n 种面额的硬币,给出各种面额硬币的数量和和面额数,求最多能搭配出几种不超过 m 的金额? 分析 : 这题可用多重背包来解,但这里不讨论这种做法. 如果之前有接触过背包DP的可以自然想到 ...

随机推荐

  1. C#网络编程系列文章(一)之Socket实现异步TCPserver

    原创性声明 本文作者:小竹zz 本文地址http://blog.csdn.net/zhujunxxxxx/article/details/44258719 转载请注明出处 文章系列文件夹 C#网络编程 ...

  2. LoadRunner多负载产生器

    Executive Summary : The following explains why it is necessary to have about 6 load generators when ...

  3. Android手机需要安装任务管理软件吗?

    使用android手机的用户可能都安装了任务管理的软件,使用android手机真的有必要安装结束任务的软件吗?大家在使用中也都发现了,很多软件在被结束后,马上就会又出现在任务列表里,或是稍等一会自己也 ...

  4. windows ffmpeg 推送摄像头数据到rtmp服务

    文本主要讲述windows系统下如何利用ffmpeg获取摄像机流并推送到rtmp服务,命令的用法前文 中有讲到过,这次是通过代码来实现.实现该项功能的基本流程如下: 图1 ffmpeg推流流程图 较前 ...

  5. D3.js 制作中国地图 .net 公共基础类

    D3.js 制作中国地图 from:  http://d3.decembercafe.org/pages/map/index.html GeoJSON is a format for encoding ...

  6. Intel平台map

  7. web前端面试系列 - js中的prototype

    js中一切皆为对象,其中函数也是一种对象, 而每个函数都有一个prototype属性,其值也是一个对象. 一.prototype的作用 1. 在多个实例对象之间共享数据和方法. 2. 通过原型链实现继 ...

  8. 深度解析开发项目之 03 - enum的使用

    深度解析开发项目之 03 - enum的使用 01 - 在#import和@interface之间定义typedef enum 注意: 默认是0,1,2,3 02 - 定义可以操作的数据类型的属性 0 ...

  9. JavaScript读书笔记(1)

    从今天开启每天看书记笔记模式,<JavaScript高级程序设计(第3版)> 1. Javascript最初是为了解决输入验证器的问题,现在已经发展成一门复杂的语言: 2.  语言标准为E ...

  10. jni集成第3方third party动态库libwebrtc_audio_preprocessing.so时android.mk的编写

    现在要调用一个第3方库libwebrtc_audio_preprocessing.so 最终布局如下: 即创建2个文件夹,将第3方库放进去. include目录放第3方库的头文件  其中lib目录如下 ...