UVA-11517 Exact Change(DP)
题目大意:有n张钞票,面值可能不同。你要买一件东西,可能需要找零钱。问最少付多少钱,并求出最少的钞票张数。
题目分析:定义状态dp(i,w)表示前i张钞票凑成w元需要的最少钞票张数。则状态转移方程为dp(i,w)=min(dp(i-1,w),dp(i-1,w-a(i))+1)。其中a(i)为第i张钞票的面值。
代码如下:
//# define AC # ifndef AC # include<iostream>
# include<cstdio>
# include<cstring>
# include<vector>
# include<queue>
# include<list>
# include<cmath>
# include<set>
# include<map>
# include<string>
# include<cstdlib>
# include<algorithm>
using namespace std; # define mid (l+(r-l)/2) typedef long long LL;
typedef unsigned long long ULL; const int N=10000;
const int mod=1e9+7;
const int INF=0x3fffffff;
const LL oo=0x7fffffffffffffff; int n,w,a[105];
int dp[105][N*2+5]; int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&w,&n);
int m=0;
for(int i=0;i<n;++i){
scanf("%d",a+i);
m+=a[i];
}
m=min(m,N<<1);
fill(dp[0],dp[0]+m+1,INF);
dp[0][0]=0;
dp[0][a[0]]=1;
for(int i=1;i<n;++i){
dp[i][0]=0;
for(int j=1;j<=m;++j){
if(j<a[i]) dp[i][j]=dp[i-1][j];
else{
dp[i][j]=INF;
if(dp[i-1][j]!=-INF)
dp[i][j]=dp[i-1][j];
if(dp[i-1][j-a[i]]!=INF)
dp[i][j]=min(dp[i][j],dp[i-1][j-a[i]]+1);
}
}
}
while(dp[n-1][w]==INF||!dp[n-1][w])
++w;
printf("%d %d\n",w,dp[n-1][w]);
}
return 0;
} # endif
UVA-11517 Exact Change(DP)的更多相关文章
- UVA.674 Coin Change (DP 完全背包)
UVA.674 Coin Change (DP) 题意分析 有5种硬币, 面值分别为1.5.10.25.50,现在给出金额,问可以用多少种方式组成该面值. 每种硬币的数量是无限的.典型完全背包. 状态 ...
- UVA 674 Coin Change(dp)
UVA 674 Coin Change 解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730#problem/ ...
- Exact Change(背包HDU2753)
Exact Change Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- uva 10817(数位dp)
uva 10817(数位dp) 某校有m个教师和n个求职者,需讲授s个课程(1<=s<=8, 1<=m<=20, 1<=n<=100).已知每人的工资c(10000 ...
- FCC高级编程篇之Exact Change
Exact Change Design a cash register drawer function checkCashRegister() that accepts purchase price ...
- UVA 674 Coin Change (DP)
Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to make c ...
- UVA 674 Coin Change 换硬币 经典dp入门题
题意:有1,5,10,25,50五种硬币,给出一个数字,问又几种凑钱的方式能凑出这个数. 经典的dp题...可以递推也可以记忆化搜索... 我个人比较喜欢记忆化搜索,递推不是很熟练. 记忆化搜索:很白 ...
- Exact Change(01背包)
描述 Seller: That will be fourteen dollars. Buyer: Here's a twenty. Seller: Sorry, I don't have any ch ...
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
随机推荐
- 【转】RadControls for Silverlight(学习1-GridView)
引用:Telerik(官 网:http://www.telerik.com/)是保加利亚的一个软件公司,专注于微软.Net平台的表示层与内容管理控件.我们提供高度稳定性和丰富性能的组件产品,并可应用在 ...
- Ajax与Comet
1.Ajax核心? XHR >>1.新建XMLHttpRequest >>2.open(),接受3个参数. >>3.send(),接受1个参数. >>4 ...
- 利用QJSON将FDQuery转成JSON串
服务器要支持Http协议,打算采用Http+JSON的方式来交换数据.一开始考虑使用superobject,因为以前使用比较多,比较熟悉. 代码如下: class function FDQueryTo ...
- Multivariance Linear Regression练习
%% 方法一:梯度下降法 x = load('E:\workstation\data\ex3x.dat'); y = load('E:\workstation\data\ex3y.dat'); x = ...
- vs2012 opencv 配置
一直没有学习C++,以为该语言太过old,所以要学习新的咚咚.一番学习归来,在进行OpenCV的时候,还是要用到这个C++.几次琢磨,终于能够配置好相关的开发环境和问题初步处理,有些内容得赶快记录下来 ...
- spring aop 声明式事务管理
一.声明式事务管理的概括 声明式事务(declarative transaction management)是Spring提供的对程序事务管理的方式之一. Spring的声明式事务顾名思义就是采用声明 ...
- 370. Range Addition
Assume you have an array of length n initialized with all 0's and are given k update operations. Eac ...
- Android开发学习笔记:Intent的简介以及属性的详解【转】
一.Intent的介绍 Intent的中文意思是“意图,意向”,在Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述 ...
- 1st贝塞尔函数的使用
x=-100:0.1:100; y1=besselj(7,x);y2=besselj(10,x);y3=besselj(20,x);y4=besselj(40,x);y5=besselj(60,x); ...
- 谈谈pooling?
使用pooling的目的之一是获取一定的特征不变性,目前用的比较多的是Max..,非线性对于Deep的重要性不用多说,pooling是主要贡献之一,当然少不了relu类的激活函数.pooling还有一 ...