题意:给定n种硬币的价值和数量,问能组成1~m中多少种面值。

分析:

1、dp[j]表示当前用了前i种硬币的情况下,可以组成面值j。

2、eg:

3 10

1 3 4 2 3 1

(1)使用第1种硬币,可以组成的面值0 1 2,eg:当前cnt[2]表示组成面值2使用了两(cnt[2])个第一种硬币。

(2)在使用第一种硬币基础上,使用第二种硬币,可组成0 1 2 3 6 9,eg:当前cnt[6]表示组成面值6使用了两(cnt[6])个第二种硬币,依此类推。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-10;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 100 + 10;
const int MAXT = 100000 + 10;
using namespace std;
int value[MAXN];
int num[MAXN];
int dp[MAXT];
int cnt[MAXT];
int main(){
int n, m;
while(scanf("%d%d", &n, &m) == 2){
if(!n && !m) return 0;
memset(dp, 0, sizeof dp);
for(int i = 0; i < n; ++i){
scanf("%d", &value[i]);
}
for(int i = 0; i < n; ++i){
scanf("%d", &num[i]);
}
dp[0] = 1;
int ans = 0;
for(int i = 0; i < n; ++i){
memset(cnt, 0, sizeof cnt);
for(int j = value[i]; j <= m; ++j){
if(!dp[j] && dp[j - value[i]] && cnt[j - value[i]] < num[i]){
dp[j] = 1;
cnt[j] = cnt[j - value[i]] + 1;
++ans;
}
}
}
printf("%d\n", ans);
}
return 0;
}

  

POJ - 1742 Coins(dp---多重背包)的更多相关文章

  1. POJ 1742 Coins 【多重背包DP】

    题意:有n种面额的硬币.面额.个数分别为A_i.C_i,求最多能搭配出几种不超过m的金额? 思路:dp[j]就是总数为j的价值是否已经有了这种方法,如果现在没有,那么我们就一个个硬币去尝试直到有,这种 ...

  2. poj 1742 Coins (多重背包)

    http://poj.org/problem?id=1742 n个硬币,面值分别是A1...An,对应的数量分别是C1....Cn.用这些硬币组合起来能得到多少种面值不超过m的方案. 多重背包,不过这 ...

  3. Poj 1742 Coins(多重背包)

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

  4. POJ 1742 Coins(多重背包,优化)

    <挑战程序设计竞赛>上DP的一道习题. 很裸的多重背包.下面对比一下方法,倍增,优化定义,单调队列. 一开始我写的倍增,把C[i]分解成小于C[i]的2^x和一个余数r. dp[i][j] ...

  5. POJ 1742 Coins(多重背包) DP

    参考:http://www.hankcs.com/program/cpp/poj-1742-coins.html 题意:给你n种面值的硬币,面值为a1...an,数量分别为c1...cn,求问,在这些 ...

  6. POJ 1742 Coins(多重背包?)

    题解 一个自然的思路是对于每一个物品做一次01背包 然后T飞了. 试着用二进制拆分,还是T了. 单调队列,对不起,懒,不想写. 我们这样想.设dp[i]代表i这个面值前几种硬币是否能凑到 然后对于每一 ...

  7. POJ 1742 Coins DP 01背包

    dp[i][j]表示前i种硬币中取总价值为j时第i种硬币最多剩下多少个,-1表示无法到达该状态. a.当dp[i-1][j]>=0时,dp[i][j]=ci; b.当j-ai>=0& ...

  8. HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化)

    HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化) 题意分析 先把每种硬币按照二进制拆分好,然后做01背包即可.需要注意的是本题只需要求解可以凑出几种金钱的价格,而不需要输出种数 ...

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

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

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

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

随机推荐

  1. P1064 朋友数

    P1064 朋友数 转跳点:

  2. Centos7 rsync+inotify两台服务器同步文件(单向)

    注:本篇介绍的是单向同步,即A文件同步到B,但B的文件不同步到A,双向同步的在下一篇文章中. rsync与inotify不再赘述,直接进入实战. 0.背景 两台服务器IP地址分别为: 源服务器:192 ...

  3. Java笔记--常用类

    1.String类: --使用Unicode字符编码,一个字符占两个字节: --String类是一个final类,代表不可变的字符序列: --字符串是不可变的,一个字符串对象一旦被配置,其内容是不可变 ...

  4. 15.Pythonic与python杂记

    switcher ={ :'sunday', :'monday', :'thuesday' } day = day_name=switcher.get(day,'Unknow') print(day_ ...

  5. 007.CI4框架CodeIgniter, 加载自己的helper辅助类,调用自己helper中定义各种全局函数

    01. 我们在Helpers文件中创建一个Tx_helper.php的文件,里面就下一个函数 <?php //输出 function ShowMessage($AMsg) { echo &quo ...

  6. 007.Oracle数据库 , 使用%进行模糊查询

    /*Oracle数据库查询日期在两者之间*/ SELECT PKID, OCCUR_DATE, ATA FROM LM_FAULT WHERE ( ( OCCUR_DATE >= to_date ...

  7. leetcode1 twoSum

    """ Given an array of integers, return indices of the two numbers such that they add ...

  8. arduino 通过串口接收string,int类型数据

    串口接收string类型数据源码如下 String comdata = ""; void setup() {     Serial.begin(9600); }   void lo ...

  9. Day4-T2

    原题目 某大厦共有 N 层,现在知道共有 K 个请求要上下电梯:下面告诉你每个请求乘电梯的出发层次和结 束层次.请你求出整个电梯的运行过程.假设电梯一开始停在第一层,运行 K 个请求最后回到第一层. ...

  10. require(): open_basedir restriction in effect. File(/www/wwwroot/xcx/zerg/thinkphp/start.php) is not within the allowed path(s): (/www/wwwroot/xcx/zerg/public/:/tmp/:/proc/) in /www/wwwroot/xcx/zerg/p

    解决方法: 在如下文件增加一项(如图所示) 在如下文件增加一项(如图所示): #php文件采用fastcgi解析并设置参数    location ~ \.php {        try_files ...