描述


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. 转载---SQL Server XML基础学习<1>之--FOR XML PATH

    --> 测试数据:#tbIF OBJECT_ID('TEMPDB.DBO.#tb') IS NOT NULL    DROP TABLE #tbGO CREATE TABLE #tb      ...

  2. IBM WebSphere MQ 通道类型配置

    IBM WebSphere MQ 通道类型配置 初学MQ,四种常见通道,windows下操作 目录 Sender--Receiver Server-Receiver Server-Requester ...

  3. Spring中Bean实例的生命周期及其行为

  4. tomcat内存溢出问题

    内存泄露java.lang.OutOfMemoryError: PermGen space解决办法 今天访问web服务器,tomcat服务就瘫痪了,通过查看日志,发现java.lang.OutOfMe ...

  5. MasterCard信用卡测试卡号-creditcard-1

    MasterCard信用卡测试卡号-creditcard-1 510510510510510051111111111111185454545454545454550000000000000455555 ...

  6. Python编写的Linux网络设置脚本,Debian Wheezy上测试通过

    hon编写的Linux网络设置脚本,Debian Wheezy上测试通过       阿里百川梦想创业大赛,500万创投寻找最赞的APP 技术细节参见Linux网络设置高级指南 注意事项参见程序注释 ...

  7. linux 信号处理

    查看信号 kill -l 信号实际就是一个进程发送给另一个进程的消息

  8. jquery api 笔记(2) 事件 事件对象

    事件 #1.resize()     缩放窗体:window.resizeTo(width, height); 并不是兼容做法.   #2 .scroll() ->获取滚动条的位置: .scro ...

  9. 快速设置IP的脚本

    @echo off cls ::set NAME="本地连接" set NAME="无线网络连接" set IP=192.168.1.55 set MASK=2 ...

  10. Delphi 我常用的几个下载源码的站点

    盒子.Delphi园地就不说了,介绍几个其它的: 源码爱好者,特别喜欢. http://www.codefans.net/sort/list_10_1.shtml 新兴源码: http://www.n ...