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,每个物品的重量为 ...
随机推荐
- Maven中使用Jetty容器
1.在pom.xml中添加Jetty的插件 <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId ...
- MyEclipse持续性开发教程:用JPA和Spring管理数据(四)
MyEclipse红运年货节 在线购买低至69折!火爆开抢>> [MyEclipse最新版下载] 本教程介绍了MyEclipse中的一些基于JPA / Spring的功能.有关设置JPA项 ...
- 怎样取消老毛桃软件赞助商---只需在输入框中输入老毛桃官网网址“laomaotao.org”
来源:www.laomaotao.org 时间:2015-01-29 在众多网友和赞助商的支持下,迄今为止,老毛桃u盘启动盘制作工具已经推出了多个版本.如果有用户希望取消显示老毛桃软件中的赞助商,那不 ...
- Future接口和FutureTask类【FutureTask实现了Runnable和Future接口】
Future API: public interface Future<V> { /** * Attempts to cancel execution of this task. This ...
- HDU 2094:产生冠军(拓扑排序)
产生冠军 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- 只需 5 秒钟,你就能取到 WPF 程序的超高分辨率超高清截图
我想要截取一个 WPF 程序的图标,但是它太小了.如果我就这样截屏截下来,是很不高清的.由于我需要制作一份课件,所以我需要超高清版本,可是,如何做才能最快速拿到 WPF 程序的超高清截图呢? 本文分享 ...
- net core web服务器实现
net core 系列 18 web服务器实现 一. ASP.NET Core Module 在介绍ASP.NET Core Web实现之前,先来了解下ASP.NET Core Module.该模块是 ...
- PHP 7.0 升级备注
PHP 7.0.0 beta1 发布了,在带来了引人注目的性能提升的同时,也带来了不少语言特性方面的改变.以下由 LCTT 翻译自对官方的升级备注,虽然目前还不是正式发布版,不过想必距离正式发布的特性 ...
- 1010. Radix (25) pat
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
- #define WIN32_LEAN_AND_MEAN 的作用
今天看了用mysql的库+vc连接数据库,结果我用mfc application向导建立一个project,然后加入#include "mysql.h"(已经设置好了环境),编译出 ...