Exact Change(背包HDU2753)
Exact Change
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 545 Accepted Submission(s): 172
Problem Description
* Seller: That will be fourteen dollars.
* Buyer: Here’s a twenty.
* Seller: Sorry, I don’t have any change.
* Buyer: OK, here’s a ten and a five. Keep the change.
When travelling to remote locations, it is often helpful to bring cash, in case you want to buy something from someone who does not accept credit or debit cards. It is also helpful to bring a variety of denominations in case the seller does not have change. Even so, you may not have the exact amount, and will have to pay a little bit more than full price. The same problem can arise even in urban locations, for example with vending machines that do not return change.
Of course, you would like to minimize the amount you pay (though you must pay at least as much as the value of the item). Moreover, while paying the minimum amount, you would like to minimize the number of coins or bills that you pay out.
Input
The first line of each test chunk contains an integer specifying the number of test cases in this chunk to follow. Each test case begins with a line containing an integer, the price in cents of the item you would like to buy. The price will not exceed 10 000 cents (i.e., 100).Thefollowinglinecontainsasingleintegern,thenumberofbillsandcoinsthatyouhave.Thenumbernisatmost100.Thefollowingnlineseachcontainoneinteger,thevalueincentsofeachbillorcointhatyouhave.Notethatthedenominationscanbeanynumberofcents;theyarenotlimitedtothevaluesofcoinsandbillsthatweusuallyuseinCanada.However,nobillorcoinwillhaveavaluegreaterthan10000cents(100). The total value of your bills and coins will always be equal to or greater than the price of the item.
Please process to the end of the data file.
Output
For each test case, output a single line containing two integers: the total amount paid (in cents), and the total number of coins and bills used.
Sample Input
1
1400
3
500
1000
2000
1
1400
3
500
1000
2000
Sample Output
1500 2
1500 2
Source
University of Waterloo Local Contest 2008.10.04
题意:买一件价值为m的物品,在对方不找零的情况下,拿出最少的前n(n>=m),钱数相同的情况下,钱的数量最少.
思路:
将自己的金钱进行01背包,然后查找最接近m的钱
#include <set>
#include <map>
#include <list>
#include <stack>
#include <cmath>
#include <vector>
#include <queue>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const double eps = 1e-5;
int n,m;
int sum;
int Dp[110000];
int a[110];
int main()
{
int T;
while(~scanf("%d",&T))
{
while(T--)
{
scanf("%d %d",&m,&n);
sum=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
sum=max(sum,a[i]);
}
sum+=m;
memset(Dp,INF,sizeof(Dp));
Dp[0]=0;
for(int i=1;i<=n;i++)
{
for(int j=sum;j>=a[i];j--)
{
if(Dp[j-a[i]]!=INF)//标记,判断这个状态是否存在
{
Dp[j]=min(Dp[j],Dp[j-a[i]]+1);
}
}
}
for(int i=m;;i++)//查找
{
if(Dp[i]!=INF)
{
printf("%d %d\n",i,Dp[i]);
break;
}
}
}
}
return 0;
}
Exact Change(背包HDU2753)的更多相关文章
- FCC高级编程篇之Exact Change
Exact Change Design a cash register drawer function checkCashRegister() that accepts purchase price ...
- Exact Change(01背包)
描述 Seller: That will be fourteen dollars. Buyer: Here's a twenty. Seller: Sorry, I don't have any ch ...
- Exact Change
设计一个收银程序 checkCashRegister() ,其把购买价格(price)作为第一个参数 , 付款金额 (cash)作为第二个参数, 和收银机中零钱 (cid) 作为第三个参数. cid ...
- [Advanced Algorithm] - Exact Change
题目 设计一个收银程序 checkCashRegister(),其把购买价格(price)作为第一个参数 , 付款金额 (cash)作为第二个参数, 和收银机中零钱 (cid) 作为第三个参数. ci ...
- Exact Change FreeCodeCamp
function checkCashRegister(price, cash, cid) { var change; var sumCid = 0; // Here is your change, m ...
- UVA-11517 Exact Change(DP)
题目大意:有n张钞票,面值可能不同.你要买一件东西,可能需要找零钱.问最少付多少钱,并求出最少的钞票张数. 题目分析:定义状态dp(i,w)表示前i张钞票凑成w元需要的最少钞票张数.则状态转移方程为d ...
- FCC(ES6写法) Exact Change
设计一个收银程序 checkCashRegister() ,其把购买价格(price)作为第一个参数 , 付款金额 (cash)作为第二个参数, 和收银机中零钱 (cid) 作为第三个参数. cid ...
- (混合背包 多重背包+完全背包)The Fewest Coins (poj 3260)
http://poj.org/problem?id=3260 Description Farmer John has gone to town to buy some farm supplies. ...
- POJ3260The Fewest Coins[背包]
The Fewest Coins Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6299 Accepted: 1922 ...
随机推荐
- Swift动画编程指南-01 简介
大家好,我是老镇,这段时间家里和工作上发生了很多的事情,所以很长一段时间都没有出来搞什么小动作了.在接下来的一段时间内我会制作一些列关于使用Swift进行动画编程的视频,希望和大家胃口. 在iOS的世 ...
- FAQ: C++中定义类的对象:用new和不用new有何区别?
C++用new创建对象和不用new创建对象的区别解析 作者: 字体:[增加 减小] 类型:转载 时间:2013-07-26 我要评论 在C++用new创建对象和不用new创建对象是有区别的,不知你是否 ...
- C#: XML Serializer
这里主要讲如何将一个class序列化为一个string.如下一个class: public class OrderedItem { private string itemName; private s ...
- 将activity变成dialog风格
其实很简单: 只是把activity的theme设置成dilog形式的: <activity android:name=".ActivityPackage.DatePickerActi ...
- Creating Directives that Communicate
<my-tabs> <my-pane title="Hello"> <h4>Hello</h4> <p>Lorem ip ...
- 解析xml文件
package com.ss1.xml; import java.io.File; import java.io.FileOutputStream; import java.io.IOExceptio ...
- lower power设计中的DVFS设计
Pswitch = Ceff * Vvdd^2*Fclk, Pshort-circuit = Isc * Vdd * Fclk, Pleakage = f(Vdd, Vth, W/L) 尽管对电压的s ...
- spring的事务回滚
@Transactional(rollbackFor = { Exception.class }) 需要把异常抛出到带有@Transactional(rollbackFor = { Exception ...
- mysql 管理工具
摘自: http://www.chinaz.com/free/2009/0306/68691.shtml MySQL是一个非常流行的小型关系型数据库管理系统,2008年1月16号被Sun公司收购.目前 ...
- 白盒测试的学习之路----(五)TestNG的参数分离
之前的测试用例直接嵌套在代码中,不便于维护和测试设计,应该单独把测试用例放在excel内,然后程序从中读取数据到相应的接口内即可.使用ava程序对Microsoft Office格式档案读和写的功能提 ...