Coins
Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions:43969   Accepted: 14873

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

题意:有n种硬币,每一枚有一个价值和个数。现在取出一些硬币,面值相加得到结果S。问1~m之间可以得到多少种结果S

思路:硬币为物品,面值为体积,m为背包总容积。一次考虑每种硬币是否被用于拼成最终的面值,以“已经考虑过的物品种数”i作为DP的阶段。阶段i时,dp[j]表示前i种硬币能否拼成面值j。

但是这道题只关注“可行性”而不是“最优性”,可以发现前i种硬币能够拼成面值j只有两种可能。1、前i-1种就可以拼成面值j 2、使用了第i种硬币,发现dp[j-ai]为true,从而dp[j]变为true

于是就有一种贪心策略:设used[j]表示dp[j]在阶段i时为true至少要用到多少枚第i种硬币,并尽量选择第一种情况。在dp[j-ai]为true时,如果dp[j]已经为true,则不执行dp转移,并令used[j]=0。否则执行dp[j] = dp[j] or dp[j - ai]的转移,并令used[j] = used[j - ai] + 1

多重背包问题可以将物品拆分变成01背包问题。拆分方法有直接拆分法,二进制拆分法和单调队列。

二进制拆分法是把数量为Ci的第i种物品拆分成p+2个物品,p是满足2^0 + 2^1 + 2^2 + ... + 2^p <= Ci的最大的整数。

他们的体积分别为2^0*Vi, 2^1*Vi, ..., 2^p*Vi, Ri * Vi, 其中Ri= Ci - 2^0 - 2^1 - 2^2 - ... - 2^p

 //#include <bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<stdio.h>
#include<cstring>
#include<map> #define inf 0x3f3f3f3f
using namespace std;
typedef long long LL; int n, m;
const int maxn = ;
const int maxm = 1e5 + ;
int a[maxn], c[maxn];
int used[maxm];
bool dp[maxm]; int main()
{
while(scanf("%d%d", &n, &m) != EOF && (n || m)){
for(int i = ; i <= n; i++){
scanf("%d", &a[i]);
}
for(int i = ; i <= n; i++){
scanf("%d", &c[i]);
} memset(dp, , sizeof(dp));
dp[] = true;
for(int i = ; i <= n; i++){
memset(used, , sizeof(used));
for(int j = a[i]; j <= m; j++){
if(!dp[j] && dp[j - a[i]] && used[j - a[i]] < c[i]){
dp[j] = true;
used[j] = used[j - a[i]] + ;
}
}
} int ans = ;
for(int i = ; i <= m; i++){
if(dp[i])ans++;
}
printf("%d\n", ans);
}
return ;
}

poj1742 Coins【多重背包】【贪心】的更多相关文章

  1. $POJ1742\ Coins$ 多重背包+贪心

    Vjudge传送门 $Sol$ 首先发现这是一个多重背包,所以可以用多重背包的一般解法(直接拆分法,二进制拆分法...) 但事实是会TLE,只能另寻出路 本题仅关注“可行性”(面值能否拼成)而不是“最 ...

  2. POJ1742 Coins[多重背包可行性]

    Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 34814   Accepted: 11828 Descripti ...

  3. POJ1742:Coins(多重背包)

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

  4. POJ 3260 The Fewest Coins(多重背包+全然背包)

    POJ 3260 The Fewest Coins(多重背包+全然背包) http://poj.org/problem?id=3260 题意: John要去买价值为m的商品. 如今的货币系统有n种货币 ...

  5. HDU-2844 Coins(多重背包)

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

  6. POJ3260——The Fewest Coins(多重背包+完全背包)

    The Fewest Coins DescriptionFarmer John has gone to town to buy some farm supplies. Being a very eff ...

  7. POJ 1742 Coins(多重背包, 单调队列)

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

  8. HDU2844 Coins 多重背包

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

  9. Codeforces 755 F. PolandBall and Gifts 多重背包+贪心

    F. PolandBall and Gifts   It's Christmas time! PolandBall and his friends will be giving themselves ...

  10. HDu -2844 Coins多重背包

    这道题是典型的多重背包的题目,也是最基础的多重背包的题目 题目大意:给定n和m, 其中n为有多少中钱币, m为背包的容量,让你求出在1 - m 之间有多少种价钱的组合,由于这道题价值和重量相等,所以就 ...

随机推荐

  1. atitit.自动生成数据库结构脚本,或者更换数据库,基于hibernate4

    atitit.自动生成数据库结构脚本,或者更换数据库,基于hibernate4 目前近况:: 更换数据库,但是是使用spring集成的. <!-- hibernate配置文件路径 --> ...

  2. SCWS 中文分词

    SCWS 中文分词v1.2.3 开源免费的中文分词系统,PHP分词的上乘之选! 首页 下载 演示 文档 关于 服务&支持 API/HTTP 论坛 捐赠 源码@github 文档目录 SCWS- ...

  3. 禁止Chrome浏览器自动升级

    对于我们测试人员来说,浏览器自动升级是非常可怕的,浏览器的升级会导致出现各种bug,比如我们常用的Selenium,如果Chrome浏览器自动升级就会导致脚本出错,无法打开浏览器等等情况,对于这种情况 ...

  4. NSURLErrorDomain Code=-999(转)

    原文:http://www.henishuo.com/nsurlerrordomain-code-999/ 前言 今天有一个线上bug,是分配给提供H5的团队的,但是后台查不出来原因.于是让前端iOS ...

  5. 利用 jQuery 克隆 Object

    在网上搜索关键字 “javascript object clone”,可以找到很多实现克隆 Object 的代码,可是据我测试,让人满意的几乎没有. 今天发现 jQuery 的作者 John Resi ...

  6. linux - native task api 测试

    #include <stdio.h>#include <signal.h>#include <unistd.h>#include <sys/mman.h> ...

  7. udp program

    UDP program UDP常用函数:recvfrom和sendto recvfrom ssize_t recvfrom(int sockfd, void *buf, size_t len, int ...

  8. hive中创建hive-json格式的表及查询

    在hive中对于json的数据格式,可以使用get_json_object或json_tuple先解析然后查询. 也可以直接在hive中创建json格式的表结构,这样就可以直接查询,实战如下(hive ...

  9. word调整技巧

    a4纸如何留白: 

  10. IOC控制反转

    IOC是Inversion of Control的缩写,多数书籍翻译成“控制反转”,还有些书籍翻译成为“控制反向”或者“控制倒置”.     1996年,Michael Mattson在一篇有关探讨面 ...