poj1742 Coins【多重背包】【贪心】

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

[email protected]

题意:有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

 1 //#include <bits/stdc++.h>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<stdio.h>
 6 #include<cstring>
 7 #include<map>
 8
 9 #define inf 0x3f3f3f3f
10 using namespace std;
11 typedef long long LL;
12
13 int n, m;
14 const int maxn = 105;
15 const int maxm = 1e5 + 5;
16 int a[maxn], c[maxn];
17 int used[maxm];
18 bool dp[maxm];
19
20 int main()
21 {
22     while(scanf("%d%d", &n, &m) != EOF && (n || m)){
23         for(int i = 1; i <= n; i++){
24             scanf("%d", &a[i]);
25         }
26         for(int i = 1; i <= n; i++){
27             scanf("%d", &c[i]);
28         }
29
30         memset(dp, 0, sizeof(dp));
31         dp[0] = true;
32         for(int i = 1; i <= n; i++){
33             memset(used, 0, sizeof(used));
34             for(int j = a[i]; j <= m; j++){
35                 if(!dp[j] && dp[j - a[i]] && used[j - a[i]] < c[i]){
36                     dp[j] = true;
37                     used[j] = used[j - a[i]] + 1;
38                 }
39             }
40         }
41
42         int ans = 0;
43         for(int i = 1; i <= m; i++){
44             if(dp[i])ans++;
45         }
46         printf("%d\n", ans);
47     }
48     return 0;
49 }

原文地址:https://www.cnblogs.com/wyboooo/p/9757029.html

时间: 2024-08-29 11:30:22

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

POJ1742 Coins 多重背包+贪心

Vjudge传送门 $Sol$ 首先发现这是一个多重背包,所以可以用多重背包的一般解法(直接拆分法,二进制拆分法...) 但事实是会TLE,只能另寻出路 本题仅关注“可行性”(面值能否拼成)而不是“最优性”,这是一个特殊之处. 从这里找优化 在“最优性”的问题中,$f[j]$从$f[j]$或$f[j-a[i]]$中转移而来:而在这样的“可行性”问题中,其实只要$f[j]$可行,我们就可以不用考虑$f[j-a[i]$了,也可以反过来说. 于是我们可以考虑一种贪心策略,设$used[j]$表示$f[

POJ1742 Coins[多重背包可行性]

Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 34814   Accepted: 11828 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

POJ 1742 Coins (多重背包)

Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 28448   Accepted: 9645 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

hdu2844 &amp; poj1742 Coin ---多重背包--两种方法

题意:你有N种硬币,每种价值A[i],每种数量C[i],问.在不超过M的情况下,我们用这些硬币,付款有多少种情况.也就是:1,2,3,4,5,....,M这么多种情况下,你能用你的硬币不找钱,付款多少种情况. 例如: 你有一种硬币,价值2,个数2,那么 你是不能付款 3元的..你只能付款2,或者4元.. OK,题意差不多就是这样啦. 那么这里有两种方式! 分析: 那么这里我们可以用多重背包来解决,我们把价值和重量看成一样的w[i] = A[i]:用M作为背包.那么dp 过后,我们就可以知道 dp

POJ 1742 Coins 多重背包单调队列优化

http://poj.org/problem?id=1742 题意: 很多硬币,有价值和数量,给出一个上限,问上限内有多少种钱数可以由这些硬币组成. 分析: 好像是楼教主男人八题之一.然后学多重背包单调队列优化时看了别人的程序..所以后来写了就1A了=.= 前一篇小小总结了一下多重背包单调队列优化(http://www.cnblogs.com/james47/p/3894772.html),这里就不写了. 1 #include<cstdio> 2 #include<cstring>

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

The Fewest Coins DescriptionFarmer John has gone to town to buy some farm supplies. Being a very efficient man, he always pays for his goods in such a way that the smallest number of coins changes hands, i.e., the number of coins he uses to pay plus

HDU 2844 Coins 多重背包(二进制优化)

点击打开链接 Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8167    Accepted Submission(s): 3327 Problem Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dolla

hdu2844 coins 多重背包

1 #include<stdio.h> 2 #include<string.h> 3 int a[102],c[102],dp[100005]; 4 int max(int a,int b) 5 { 6 return a>b?a:b; 7 } 8 void CompletePack(int v,int w,int m) //完全背包 9 { 10 for(int j=v;j<=m;j++) 11 dp[j]=max(dp[j],dp[j-v]+w); 12 } 13 v

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

http://poj.org/problem?id=3260 题意: John要去买价值为m的商品. 如今的货币系统有n种货币,相应面值为val[1],val[2]-val[n]. 然后他身上每种货币有num[i]个. John必须付给售货员>=m的金钱, 然后售货员会用最少的货币数量找钱给John. 问你John的交易过程中, 他给售货员的货币数目+售货员找钱给他的货币数目 的和最小值是多少? 分析: 本题与POJ 1252类型: http://blog.csdn.net/u013480600

poj 3260 The Fewest Coins 多重背包+完全背包

题目链接:http://poj.org/problem?id=3260 给店家的钱是多重背包 dp[] 店家的找钱是完全背包 dp2[] 然后最后 其中i表示多给了多少钱 也就是需要找回多少钱 int ans = INF; ans = min(ans, dp[m+i] + dp2[i]); 是一个比较简单的思路 神坑题 看到每种货币的面值不大于120 我就觉得找钱一定不会超过119块钱结果wa到死 后来才发现不是的啊 之所以我有这种思维定式是因为现实生活中有1块钱这种货币单位 考虑一种极限的情况