描述


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. Visual C++ 打印编程技术-编程基础

    背景: windows产生前,操作系统(如DOS等)都不提供支持图像处理的打印机驱动程序,使得程序员为打印出图像,不得不针对使用的打印机 自己编写设备驱动程序,导致了大量的.不必要的重复开发. 随着w ...

  2. Codevs 2449 骑士精神 2005年省队选拔赛四川

    2449 骑士精神 2005年省队选拔赛四川 时间限制: 1 s 空间限制: 128000 KB 题目等级 : **大师 Master** 题目描述 Description 在一个5×5的棋盘上有12 ...

  3. 常用数据与VARIANT之间的转换---从网上整理

    //头文件 1 #pragma once class VariantConvert { public: VariantConvert(void); ~VariantConvert(void); pub ...

  4. 禁止 apache 开机启动

    sudo update-rc.d apache2 disable http://askubuntu.com/questions/19320/what-is-the-recommended-way-to ...

  5. python相关博客

    入门:http://www.pythontip.com/ Python之禅--大道至简 美胜于丑,显胜于隐,简胜于繁,繁胜于杂,平胜于迭,疏胜于密,读胜于写...名可名, 请常名 http://www ...

  6. O_NONBLOCK模式下写fifo的注意事项

    后台网络通信框架一般采用fifo来作为事件通知的机制:创建一个fifo,然后以非阻塞读和非阻塞写的方式打开fifo,然后把fd加到epoll里面,作为通知网络事件的fd. 在这里有个隐晦的问题容易被忽 ...

  7. ubuntu 基本操作(1)

    命令行界面与图形界面 全屏模式: ctrl + alt + f1进入命令行界面 ctrl + alt + f7 切换图形界面 小窗口: ctrl+alt+t:进入终端 设计ubuntu root 密码 ...

  8. IE6下解决select层级高的问题

    div在IE6下无法遮盖select,原因是在IE6下,浏览器将select元素视为窗口级元素,这时div或者其它的普通元素无论z-index设置的多高都是无法遮住select元素的. 解决方法有三种 ...

  9. 【 java版坦克大战--事件处理】 坦克动起来了

    折腾了这么久,坦克总算能动了.只贴代码编辑不给上首页,花了半个小时的时间写了n多注释. 再顺便把绘图的原理发在这里: 绘图原理 Component类提供了两个和绘图有关的重要方法: ①   paint ...

  10. C# 使用Salt+Hash来为密码加密

    (一) 为什么要用哈希函数来加密密码 如果你需要保存密码(比如网站用户的密码),你要考虑如何保护这些密码数据,象下面那样直接将密码写入数据库中是极不安全的,因为任何可以打开数据库的人,都将可以直接看到 ...