描述


http://poj.org/problem?id=1742

n种不同面额的硬币 ai ,每种各 mi 个,判断可以从这些数字值中选出若干使它们组成的面额恰好为 k 的 k 的个数.

原型:

n种不同大小的数字 ai ,每种各 mi 个,判断是否可以从这些数字之中选出若干使它们的和恰好为 k .

Coins
Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 33732   Accepted: 11453

Description

People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box 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

Source

分析


图文详解:

http://www.hankcs.com/program/cpp/poj-1742-coins.html

用f[j]表示在第i次循环时,用前i-1中硬币组成面额j,第i-1中硬币还剩多少个,如果不能组成面额j,则f[j]=-1.

最后统计f[j]>=0(1<=j<=m)的个数即可.

注意:

1.起始时f[0]=0,表示刚开始可以组成面额0,且之前啥也不剩.

2.之后每次j要从0开始循环,要更新组成面额0(也就是啥都不组成)之后,第i-1种硬币还剩多少个.

3.bind1st表示一元什么什么...也可以写成 " bind2nd(greater_equal<int>(),0) ",这东西貌似在<set>头文件里.

ps.前几天才看过书上的多重部分和问题,这题简直就是模板,然而几乎忘得一干二净了,走马观花真是啥用都没有= =只有真正想明白了才算掌握了一点吧.

 #include<cstdio>
#include<cstring>
#include<set>
#include<algorithm>
using namespace std; const int maxn=,maxm=;
int n,m;
int a[maxn],c[maxn],f[maxm]; void solve()
{
memset(f,-,sizeof(f));
f[]=;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if(f[j]>=)
{
f[j]=c[i];
}
else if(a[i]>j||f[j-a[i]]<=)
{
f[j]=-;
}
else
{
f[j]=f[j-a[i]]-;
}
}
}
int ans=count_if(f+,f+m+,bind1st(less_equal<int>(),));//统计满足f[j]>=0的个数
printf("%d\n",ans);
} void init()
{
while(scanf("%d%d",&n,&m)==&&(n!=||m!=))
{
for(int i=;i<=n;i++) scanf("%d",&a[i]);
for(int i=;i<=n;i++) scanf("%d",&c[i]);
solve();
}
} int main()
{
#ifndef ONLINE_JUDGE
freopen("coin.in","r",stdin);
freopen("coin.out","w",stdout);
#endif
init();
#ifndef ONLINE_JUDEG
fclose(stdin);
fclose(stdout);
system("coin.out");
#endif
return ;
}

POJ_1742_Coins_(动态规划,多重部分和)的更多相关文章

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

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

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

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

  3. POJ_3181_Dollar_Dayz_(动态规划,完全部分和,完全背包)

    描述   http://poj.org/problem?id=3181 FJ有n元钱,有k种商品,各为1,2,...,k-1,k元,问有多少种花掉这n元钱的方法. Dollar Dayz Time L ...

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

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

  5. HDU2844(多重部分和)

    Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  6. 多重部分和 poj1742

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

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

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

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

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

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

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

随机推荐

  1. ssh(Struts2+hibernate+spring)简单分页

    实体类+实体映射+entity(pagebean)+dao层+service层+action层

  2. 摘录android工具类

    import android.content.Context; import android.content.pm.PackageInfo; import android.content.pm.Pac ...

  3. let和const====均参考阮大神的es6入门

    // 解构复制// let [foo,[[bar],baz]] = [1,[[2],3]];// console.log(foo);//1// console.log(bar);//2// conso ...

  4. C语言——N个人围成一圈报数淘汰问题

    <一>问题描述: 有17个人围成一圈(编号为0-16),从第 0号的人开始从 1报数, 凡报到 3的倍数的人离开圈子,然后再数下去,直到最后只剩下一个人为止. 问此人原来的位置是多少号? ...

  5. OpenJudge/Poj 1657 Distance on Chessboard

    1.链接地址: http://bailian.openjudge.cn/practice/1657 http://poj.org/problem?id=1657 2.题目: 总时间限制: 1000ms ...

  6. linux采用模块方法,添加一个新的设备

    该文转载自:http://rangercyh.blog.51cto.com/1444712/521244 系统调用是操作系统内核和应用程序之间的接口,而设备驱动程序是操作系统内核和机器硬件之间的接口. ...

  7. git 彩色显示当前branch

    环境: fedora 20 $ curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt. ...

  8. ubuntu下怎么合并windows下分割的zip包

    cat ziptest.z* > google_bak.zip 点击打开链接http://blog.51yip.com/linux/988.html

  9. NPOI2.0

    今天在使用NPOI2.0读取上传excel文件(excel2010)时,报了一个很奇怪的错误:无效的 URI: 未能分析主机名. 在网上查找了下找到的出错情况跟这个完全不着边际,没有办法只有自己去测试 ...

  10. 规则引擎-BRMS在企业开发中的应用

    1. 什么是规则复杂企业级项目的开发以及其中随外部条件不断变化的业务规则(business logic),迫切需要分离商业决策者的商业决策逻辑和应用开发者的技术决策,并把这些商业决策放在中心数据库或其 ...