Coins POJ - 1742

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
题解:dp[i+1][j]:=用前i种数加和得到j时第i种数最多能剩余多少个(不能加和得到i的情况下为-1)(摘自《挑战》,话说个人对这种定义方式很懵逼)按照上述定义递推关系,这样如果前i-1个数能加和得到j的话,第i个数就可以留下Mi个。此外,前i种数加和出j-Ai时第i种数还剩下K(K>0)的话,用这i种数加和j时第i种数就剩下K-1个。复杂度O(nK)
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6
 7 const int maxn=100005;
 8
 9 int n,m;
10 int va[105],vo[105];
11 int dp[maxn];
12
13 void solve()
14 {   memset(dp,-1,sizeof(dp));
15     dp[0]=0;
16     for(int i=1;i<=n;i++){
17         for(int j=0;j<=m;j++){
18             if(dp[j]>=0) dp[j]=vo[i];
19             else if(j<va[i]||dp[j-va[i]]<=0) dp[j]=-1;
20             else dp[j]=dp[j-va[i]]-1;
21         }
22     }
23     int ans=0;
24     for(int i=1;i<=m;i++) if(dp[i]>=0) ans++;
25     cout<<ans<<endl;
26 }
27
28 int main()
29 {   while(~scanf("%d%d",&n,&m)){
30         if(n==0&&m==0) break;
31         for(int i=1;i<=n;i++) scanf("%d",&va[i]);
32         for(int i=1;i<=n;i++) scanf("%d",&vo[i]);
33
34         solve();
35     }
36     return 0;
37 } 
时间: 2024-10-09 11:17:07

Coins POJ - 1742的相关文章

Coins POJ - 1742 (背包判断可行性)

 题目链接:  POJ - 1742 题目大意: n个货币,每个货币有一定的数量,然后问你从1~m有多少个数能被凑出来? 具体思路: dp[i][j]代表前i个凑出j元钱第i个的最大剩余量. 二维(超内存): 1 #include<iostream> 2 #include<cstring> 3 #include<stdio.h> 4 using namespace std; 5 # define ll long long 6 # define lson l,mid,rt

hdu 2844 poj 1742 Coins

hdu 2844 poj 1742 Coins 题目相同,但是时限不同,原本上面的多重背包我初始化为0,f[0] = 1;用位或进行优化,f[i]=1表示可以兑成i,0表示不能. 在poj上运行时间正好为时限3000ms....太慢了,hdu直接TLE(时限1s); 之 后发现其实并不是算法的问题,而是库函数的效率没有关注到.我是使用fill()按量初始化的,但是由于memset()可能是系统底层使用了四个字节拷 贝的函数(远比循环初始化快),效率要高得多..这就是为什么一直TLE的原因,fil

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

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

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

[POJ 1742] Coins 【DP】

题目链接:POJ - 1742 题目大意 现有 n 种不同的硬币,每种的面值为 Vi ,数量为 Ni ,问使用这些硬币共能凑出 [1,m] 范围内的多少种面值. 题目分析 使用一种 O(nm) 的 DP (据说这是类多重背包?),枚举每一种硬币,对于每一种硬币 i 枚举每一个面值 j ,如果这个面值 j 使用前 i-1 种硬币已经可以凑出,就直接跳过,否则尝试加入一个硬币 i ,看是否能凑出 j .需要满足 (f[j - Vi] == true) && (UseNum[j - Vi] +

poj 1742 Coins (多重背包)

http://poj.org/problem?id=1742 n个硬币,面值分别是A1...An,对应的数量分别是C1....Cn.用这些硬币组合起来能得到多少种面值不超过m的方案. 多重背包,不过这题很容易超时,用背包九讲的代码有人说行,但是我提交还是超时,后来参考别人代码加了一些优化才能过,有时间要去搞清楚多重背包的单调队列优化. 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using

POJ 1742(Coins)

题目链接:http://poj.org/problem?id=1742 与一般的背包问题不一样,这是要计算满足条件的情况的数量,而不是计算最值,一开始的思路就是按照书上的类比: dp[i][j] := 用前i种硬币能否凑成j 递推:dp[i][j] = (dp[i – 1][j – k * A[i]])为真的时候 但是 MLE,其实一点都不惊讶吧,数组开那么大肯定会出问题呀,所以只能放弃二维数组: dp[j] := 在第i次循环时之前表示用前 i-1 种硬币凑成 j 时第 i 种硬币最多能剩余多

poj 1742 Coins(dp之多重背包+多次优化)

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 pri

Coins (poj 1742 &amp;amp;&amp;amp; hdu 2844 DP)

Language: Default Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 30047   Accepted: 10195 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 fou