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. 文本聚类——Kmeans

    上两篇文章分别用朴素贝叶斯算法和KNN算法对newgroup文本进行了分类測试.本文使用Kmeans算法对文本进行聚类. 1.文本预处理 文本预处理在前面两本文章中已经介绍,此处(略). 2.文本向量 ...

  2. Solaris文件系统管理

    不同的操作系统使用不同类型的文件系统 1.文件(管理)系统:是用来对文件和目录进行管理.控制的数据结构的总称. Windows当中的文件系统: ntfs ,fat32 ,fat64 Solaris 当 ...

  3. 【HDOJ 5654】 xiaoxin and his watermelon candy(离线+树状数组)

    pid=5654">[HDOJ 5654] xiaoxin and his watermelon candy(离线+树状数组) xiaoxin and his watermelon c ...

  4. 这6种思维,学会了你就打败了95%文案!zz

    ​本文笔者为大家讲述了文案高手写文案时最常用的六种思维,这六种思维也都是文案新手容易入的坑. 有的人做了3,5年的文案,还是小白一个.而有的人短短1,2年的时间,却可以成为文案高手. 为什么? 我总结 ...

  5. JavaScript包管理器综述

    JavaScript包管理器综述 作者:chszs,未经博主同意不得转载.经许可的转载需注明作者和博客主页:http://blog.csdn.net/chszs 对于JavaScript来说.包管理器 ...

  6. C++输入一行字符串的一点小结

    C++输入一行字符串的一点小结 原文链接: http://www.wutianqi.com/?p=1181 大家在学习C++编程时.一般在输入方面都是使用的cin. 而cin是使用空白(空格,制表符和 ...

  7. 压力测试工具ab,wrk,locust简介

    ab 无疑是目前最常见的压力测试工具.其典型用法如下: shell> ab -k -c 100 -t 10 http://domain/path 其中,参数「c」表示的是并发, 参数「t」表示的 ...

  8. 一个兼容性比较好的图片左右滚动的js

    下载地址:http://www.cnblogs.com/RightDear/admin/Files.aspx 文件:shhds.rar

  9. EasyDarwin开源流媒体云平台之语音对讲功能设计与实现

    本文由EasyDarwin开源团队成员Alex贡献:http://blog.csdn.net/cai6811376/article/details/52006958 EasyDarwin云平台一直在稳 ...

  10. RTSP流媒体转发服务器源码

    最新EasyDarwin已经支持海康.大华等标准RTSP/RTP协议的转发,代码及使用方法参看:用Darwin开发RTSP级联服务器(拉模式转发)http://blog.csdn.net/xiejia ...