POJ 1742 Coins 【可行性背包】【非原创】
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
Output
Sample Input
3 10
1 2 4 2 1 1
2 5
1 4 2 1
0 0
Sample Output
8
4
题意:给你n种面值的硬币,面值为a1...an,数量分别为c1...cn,求问,在这些硬币的组合下,能够多少种面值,该面值不超过m
思路:设d[i][j]——前i种硬币,凑成总值j时,第i种硬币所剩余的个数。(能否想到这样构造是个难点
默认d[i][j] = -1,代表无法凑成总值j
转移方程为,若d[i-1][j]≥0,代表前i-1种已能够凑成j,那么就不必花费第i种硬币,所以d[i][j] = c[i]
否则就看d[i][j-a[i]]的值,显然如果j < a[i],那么d[i][j] = -1,否则d[i][j-a[i]] ≤ 0,代表此刻第i种硬币已使用完了,所以自然d[i][j] = -1;
否则,d[i][j] = d[i][j-a[i]]-1;
可以看到d[i][]的值只与d[i-1][]和d[i][]有关,所以我们可以采用一维数组的形式,从而能够节省内存空间。
AC代码:
1 #include <cstdio>
2 #include <cstring>
3 #include <algorithm>
4 using namespace std;
5 typedef unsigned long long ll;
6 const int maxn = 1e3 + 10;
7 const int inf = 0x3f3f3f3f;
8 const int maxx = 1e5 + 10;
9 int dp[maxx];
10 int a[maxn];
11 int c[maxn];
12 bool vis[maxx];
13 int main()
14 {
15 int n, m;
16 while(~scanf("%d %d", &n, &m),(n||m))
17 {
18
19 memset(dp, -1, sizeof(dp));
20 for(int i = 1; i <= n; ++i)
21 {
22 scanf("%d", a+i);
23 // printf("%d ", a[i]);
24 }
25 for(int i = 1; i <= n; ++i)
26 {
27 scanf("%d", c+i);
28 }
29 dp[0] = 0;
30 for(int i = 1; i <= n; ++i)
31 {
32 for(int j = 0; j <= m; ++j)
33 {
34 if(dp[j] >= 0)
35 {
36 dp[j] = c[i];
37 }
38
39 else if(j - a[i] >= 0 && dp[j - a[i]] > 0)
40 {
41 dp[j] = dp[j - a[i]] - 1;
42 }
43 }
44 }
45 int ans = 0;
46 for(int i = 1; i <= m; ++i)
47 {
48 // printf("%d ", dp[i]);
49 if(dp[i] >= 0) ++ans;
50 }
51 printf("%d\n",ans);
52 }
53 return 0;
54 }
转载博客:戳这里
POJ 1742 Coins 【可行性背包】【非原创】的更多相关文章
- POJ 1742 Coins (多重背包)
Coins Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 28448 Accepted: 9645 Descriptio ...
- POJ 1742 Coins(多重背包, 单调队列)
Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...
- hdu 2844 poj 1742 Coins
hdu 2844 poj 1742 Coins 题目相同,但是时限不同,原本上面的多重背包我初始化为0,f[0] = 1;用位或进行优化,f[i]=1表示可以兑成i,0表示不能. 在poj上运行时间正 ...
- [POJ 1742] Coins 【DP】
题目链接:POJ - 1742 题目大意 现有 n 种不同的硬币,每种的面值为 Vi ,数量为 Ni ,问使用这些硬币共能凑出 [1,m] 范围内的多少种面值. 题目分析 使用一种 O(nm) 的 D ...
- 题解报告:hdu 2844 & poj 1742 Coins(多重部分和问题)
Problem Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. On ...
- 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(dp之多重背包+多次优化)
Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...
- POJ 1742 Coins 【多重背包DP】
题意:有n种面额的硬币.面额.个数分别为A_i.C_i,求最多能搭配出几种不超过m的金额? 思路:dp[j]就是总数为j的价值是否已经有了这种方法,如果现在没有,那么我们就一个个硬币去尝试直到有,这种 ...
随机推荐
- 2021 Duilib最新入门教程(一)Duilib简介
目录 Duilib解决什么问题? 方案一.自己画界面 方案二.使用标准控件 方案三.使用Duilib框架 Duilib是什么? 先看下Duilib官方简介 再看下DirectUI 百度百科 比起介 ...
- Py迭代和迭代器,生成器,生产者和消费者模型
迭代器iter 1.迭代的含义: 每次生成的结果依赖于上一次.问路,先问第一个人,第一个人不知道他就说第二个人知道,然后去找第二个人.第二个人不知道就说第三个人知道,然后去找第三个人 2.递归的含义: ...
- Tensorflow-线性回归与手写数字分类
线性回归 步骤 构造线性回归数据 定义输入层 设计神经网络中间层 定义神经网络输出层 计算二次代价函数,构建梯度下降 进行训练,获取预测值 画图展示 代码 import tensorflow as t ...
- NAT模式、路由模式、桥接模式的区别
NAT模式 NAT模式概述 NAT是"Network Address Translation"的缩写,中文意思是"网络地址转换",它允许一个整体机构以一个公用I ...
- 解决window10 和 ubuntu 双系统安装没有启动选项问题
win10 和Ubuntu 双系统安装在网上已经有很多例子了,这里就不在赘述了. 今天新买的笔记本,想安装双系统.正常安装完ubuntu 重启后没有选项. 解决方法一 下载和解压以后,按照以下的步骤安 ...
- MDX学习笔记(整理) MDX语法
1.1.members和Children的用法. select [Measures].[Internet Sales Count] on columns, [客户].[全名] on rows from ...
- 数据湖-Apache Hudi
Hudi特性 数据湖处理非结构化数据.日志数据.结构化数据 支持较快upsert/delete, 可插入索引 Table Schema 小文件管理Compaction ACID语义保证,多版本保证 并 ...
- 五万字长文带你学会Spring
Sping Spring概念介绍 spring是啥呢,你在斗地主的时候把别人打爆了那叫spring, 你成功的追到了你爱慕已久的女神,人生中的春天来了,那也叫sping 好了别看我老婆了,咱来讲讲啥是 ...
- owncloud搭建
使用OwnCloud建立属于自己私有的云存储网盘 OwnCloud概述: OwnCloud 一款文件主机服务软件,就是我们平时使用的云存储,不过这是在自己主机的服务器上建立属于自己的私有云,OwnCl ...
- Ceph对象存储 S3
ceph对象存储 作为文件系统的磁盘,操作系统不能直接访问对象存储.相反,它只能通过应用程序级别的API访问.ceph是一种分布式对象存储系统,通过ceph对象网关提供对象存储接口,也称为RADOS网 ...