POJ 1742 Coins (多重背包)
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 28448 | Accepted: 9645 |
Description
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
last test case is followed by two zeros.
Output
Sample Input
3 10
1 2 4 2 1 1
2 5
1 4 2 1
0 0
Sample Output
8
4
Source
代码:1297MS
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
#define M 105
#define N 100005
int wight[M],cost[M],dp[N],user[N];
int main()
{
int i,j,n,m,ans;
while(scanf("%d%d",&n,&m)!=EOF &&n && m)
{
for(i=1;i<=n;i++) cin>>wight[i];
for(i=1;i<=n;i++) cin>>cost[i];
memset(dp,0,sizeof(dp));
dp[0]=1;ans=0;
for(i=1;i<=n;i++)
{
memset(user,0,sizeof(user)); //这里user表示的是这样的钱币在到达某种状态时,用了多少。j表示要到达的状态。也就是钱的总额。
for(j=wight[i];j<=m;j++)
{ //假设该钱币总额状态没到过,而前一个状态到过,也就是说,仅仅要加一个wight[i]表示的钱币就能到到该状态,
//而到达前一状态还剩这类钱币。就表示该状态能够到达。 if(!dp[j] && dp[j-wight[i]] && user[j-wight[i]]+1<=cost[i])
{
dp[j]=1;
user[j]=user[j-wight[i]]+1; //用一个钱币到达了该状态,这一个钱币当然要加上。 ans++; //能到达的钱币总额状态加一。 }
}
}
cout<<ans<<endl;
}
return 0;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
POJ 1742 Coins (多重背包)的更多相关文章
- POJ 1742 Coins(多重背包, 单调队列)
Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...
- poj 1742 coins_多重背包
题意:给你N个种硬币,价值和数量,知道手表不大于m,问能组成(1~m)的价格有多少种情况 套套上次那题的模板直接就行了,http://blog.csdn.net/neng18/article/deta ...
- POJ 3260 The Fewest Coins(多重背包+全然背包)
POJ 3260 The Fewest Coins(多重背包+全然背包) http://poj.org/problem?id=3260 题意: John要去买价值为m的商品. 如今的货币系统有n种货币 ...
- hdu 2844 poj 1742 Coins
hdu 2844 poj 1742 Coins 题目相同,但是时限不同,原本上面的多重背包我初始化为0,f[0] = 1;用位或进行优化,f[i]=1表示可以兑成i,0表示不能. 在poj上运行时间正 ...
- poj 1742 Coins (多重背包)
http://poj.org/problem?id=1742 n个硬币,面值分别是A1...An,对应的数量分别是C1....Cn.用这些硬币组合起来能得到多少种面值不超过m的方案. 多重背包,不过这 ...
- Poj 1742 Coins(多重背包)
一.Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dolla ...
- poj 1742 Coins(二进制拆分+bitset优化多重背包)
\(Coins\) \(solution:\) 这道题很短,开门见山,很明显的告诉了读者这是一道多重背包.但是这道题的数据范围很不友好,它不允许我们直接将这一题当做01背包去做.于是我们得想一想优化. ...
- POJ 1742 Coins ( 经典多重部分和问题 && DP || 多重背包 )
题意 : 有 n 种面额的硬币,给出各种面额硬币的数量和和面额数,求最多能搭配出几种不超过 m 的金额? 分析 : 这题可用多重背包来解,但这里不讨论这种做法. 如果之前有接触过背包DP的可以自然想到 ...
- POJ 1742 Coins 【多重背包DP】
题意:有n种面额的硬币.面额.个数分别为A_i.C_i,求最多能搭配出几种不超过m的金额? 思路:dp[j]就是总数为j的价值是否已经有了这种方法,如果现在没有,那么我们就一个个硬币去尝试直到有,这种 ...
随机推荐
- CodeForces 10C. Digital Root
乞讨A.B.C ∈[1.N] && A*B != C,d(A*B) == d(C)组的数量. 首先要知道d(x) = (x%9 == 0 ? 9 : x%9); 那么则会有A*B == ...
- cocos2d-x-3.1在eclipse中的环境搭建
cocos2d-x-3.0出来后,到如今3.1. 自己在eclipse配置上走了不少弯路,记下来给大家方便,给自己方便. 前提条件: * Android NDK * Android SDK **OR* ...
- Android毛玻璃处理代码(Blur)
以下为将bitmap图像处理为毛玻璃效果的图像的工具类: public class FastBlurUtil { public static Bitmap doBlur(Bitmap sentBitm ...
- Android学习路径(两)项目文件本身使用场景和文件演示
ios讨论群1群:135718460 1.src文件:java源码存放文件夹 2.gen 文件:自己主动生成全部由android开发工具自己主动生成的文件,文件夹中最重要的就是R.java文件,这个 ...
- 得到View Frustum的6飞机
笔者:i_dovelemon 资源:CSDN 日期:2014 / 9 / 30 主题:View Frustum, Plane, View Matrix, Perspective Projection ...
- VS2015企业版本(安装包+key)
VS2015中文企业版: http://pan.baidu.com/s/1eQtWvNs VS2015英文企业版: http://pan.baidu.com/s/1i3gZaVN —————————— ...
- [渣译文] SignalR 2.0 系列: 支持的平台
原文:[渣译文] SignalR 2.0 系列: 支持的平台 英文渣水平,大伙凑合着看吧,并不是逐字翻译的…… 这是微软官方SignalR 2.0教程Getting Started with ASP. ...
- Leetcode - Jump Game Two
和Jump Game几乎相同的想法,他们是DP.关键是使用数组maxNumbers[k]储存的地方k步骤的话.序列号的最远范围,注阵maxNumbers[]它递增. class Solution { ...
- 如何使用SQLite数据库 匹配一个字符串的子串?
select * from table_name where 字符串 like '%'||列名||'%'
- mysql 删除重复数据sql声明
CREATE TABLE tmp AS SELECT id FROM get_review_url WHERE (no,title,name,content) IN (SELECT no,title, ...