DP Coins hdoj
Coins
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 91 Accepted Submission(s) : 36
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
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
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
Sample Input
3 10
1 2 4 2 1 1
2 5
1 4 2 1
0 0
Sample Output
8
4
Source
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
int n,m;
int v[],c[];
int dp[];
int cnt[];//记录当前已用的硬币个数
while(cin>>n>>m)
{
if(m==&&n==) break;
int i,j,k;
memset(dp,,sizeof(dp));
dp[]=;//因为,比如,硬币面值为2,此时dp[2]!=0&&dp[2-2]==1
for(i=;i<=n;i++)
{
cin>>v[i];
}
for(i=;i<=n;i++)
{
cin>>c[i];
}
int cnts=;
for(i=;i<=n;i++)//每一种硬币遍历
{
memset(cnt,,sizeof(cnt));//因为只针对当前硬币
for(j=v[i];j<=m;j++)
{
if(dp[j]!=&&dp[j-v[i]]==&&cnt[j-v[i]]<=c[i]-)
{//价值j还没能被凑成,但j-v[i]可以凑成,且当前所用硬币个数+1<=c[i]
dp[j]=;
cnt[j]=cnt[j-v[i]]+;//在凑成j-v[i]的基础上多用了一个当前的硬币
//如果凑成j-v[i]用了1个当前硬币,j就要用2个
cnts++;//种数++
}
}
}
cout<<cnts<<endl;
}
return ;
}
DP Coins hdoj的更多相关文章
- DP:Coins(POJ 1742)
用硬币换钱 题目大意:就是有面值为A1,A2,A3....的硬币,各有C1,C2,C3...的数量,问在钱数为m的范围内,能换多少钱?(不找零) 这题看名字就知道是完全背包,但是这题又有点不一样, ...
- 二分+DP+Trie HDOJ 5715 XOR 游戏
题目链接 XOR 游戏 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- 【HDOJ】【4336】Card Collector
概率DP/数学期望/状压DP/容斥原理 kuangbin总结中的第14题 好神奇的做法……题解看kuangbin的代码好了…… //HDOJ 4336 #include<cstdio> # ...
- LeetCode OJ 322. Coin Change DP求解
题目链接:https://leetcode.com/problems/coin-change/ 322. Coin Change My Submissions Question Total Accep ...
- [LeetCode] Coin Change 2 硬币找零之二
You are given coins of different denominations and a total amount of money. Write a function to comp ...
- [Swift]LeetCode518. 零钱兑换 II | Coin Change 2
You are given coins of different denominations and a total amount of money. Write a function to comp ...
- [LeetCode] 518. Coin Change 2 硬币找零之二
You are given coins of different denominations and a total amount of money. Write a function to comp ...
- LeetCode之找零钱
题目:已知不同面值的钞票,求如 何用最少数量的钞票组成某个金额,求可 以使用的最少钞票数量.如果任意数量的已知面值钞票都无法组成该金额, 则返回-1. 示例: Input: coins = [1, 2 ...
- LeetCode.518 零钱兑换Ⅱ(记录)
518题是背包问题的变体,也称完全背包问题. 解法参考了该篇文章,然后对自己困惑的地方进行记录. 下面是该题的描述: 有一个背包,最大容量为 amount,有一系列物品 coins,每个物品的重量为 ...
随机推荐
- 栈(stack),C++模板实现
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- mysql left join 查询
inner join(等值连接) 只返回两个表中联结字段相等的行 left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 左连接实例: SELECT t. NAME, t1. ...
- Mathematica 迭代函数
学习Mathematica迭代函数的几个画图例子: 1.三角形沿着某一点旋转 verticse = {{0, 0}, {1, 0}, {1/2, Sqrt[3]/2}}; tri = Line[ver ...
- Linux IO模式-阻塞io、非阻塞io、多路复用io
一 概念说明 在进行解释之前,首先要说明几个概念: - 用户空间和内核空间 - 进程切换 - 进程的阻塞 - 文件描述符 - 缓存 I/O 用户空间与内核空间 现在操作系统都是采用虚拟存储器,那么对3 ...
- antd中fomr中resetFields清空输入框
1.如果没有initValue的情况下,直接使用resetFields可以清空文本框的值 2.如果是有initValue的情况下,直接使用resetFields方法会直接重置为initValue的值 ...
- WEB接口测试之Jmeter接口测试自动化 (四)
Jmeter是压力测试.接口测试工具,Ant是基于Java的构建工具,具有跨平台的作用,jenkins是持续集成工具.将这三者结合起来可以搭建一套webservice接口测试的持续构建环境. 1. ...
- 4天赚4000RMB 引流秘籍 卑鄙无耻之徒
他说:有什么引流秘籍吗? 我说:贴吧.天涯.论坛都不错,只要不明着发广告,每天被动加几十个人轻轻松松的,如果执行力好,几百上千都有可能. 他说:我该怎么操作. 我说:网上找点好的帖子,简单修改一下部分 ...
- gettimeofday的使用
前言 好像只能在linux平台使用,在windows平台不能使用.... #include <sys/time.h> long cur_tp, cost_tp, tmp_tp; struc ...
- URL的应用
1.对于Android来说,开发应用都会去访问服务器地址,那么就要连网,需要通过URL. 先new一个URL来获取路径,然后利用HttpURLConnection来连接并打开url,并通过get 请求 ...
- SharePoint PeopleEditor控件使用
以下用于简要介绍在SharePoint 2016二次开发中如何使用PeopleEditor人员选择器,并采用前端的方式获取和设置值. 一.在使用的.aspx页面进行注册 <%@ Register ...