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 ...
随机推荐
- 操作系统:进程管理和IO控制
一.进程管理 进程管理包括进程控制,进程调度,进程同步与通信,死锁控制四个内容. (一)进程控制 进程是操作系统中运行的基本单位,包括程序段,数据段和进程控制段.操作系统通过进程控制块(PCB)管理进 ...
- Swift游戏实战-跑酷熊猫 02 创建熊猫类
要点: 如何继承SKSpriteNode :子类必须调用SKSpriteNode的一个指定构造器 init(){ super.init(texture:texture,color:UIColor.wh ...
- WAL
WAL record format typedef struct XLogRecord{pg_crc32 xl_crc; /* CRC for this record */XLogRe ...
- SQL top order between 一起使用
select * from emp;
- 转:python webdriver API 之定位 frame 中的对象
在 web 应用中经常会出现 frame 嵌套的应用,假设页面上有 A.B 两个 frame,其中 B 在 A 内,那么定位 B 中的内容则需要先到 A,然后再到 B.switch_to_frame ...
- 。。。Hibernate 查询数据 事务管理。。。
在Hibernate中,查询数据的时候,可能会需要事务的管理,为什么呢?因为在查询数据库的时候,Hibernate将数据从数据库里面查询出来之后,会先把数据放入Hibernate的session缓存里 ...
- 分享Centos作为WEB服务器的防火墙规则
# Firewall configuration written by system-config-firewall # Manual customization of this file is no ...
- spring的事务回滚
@Transactional(rollbackFor = { Exception.class }) 需要把异常抛出到带有@Transactional(rollbackFor = { Exception ...
- Android SDK 安卓失败 提示: “Failed to fetch URL…” 的错误提示
解决方法: 1. 打开Tools 选择 options 将 第一个复选框勾上. (尝试一下,如果还是失败,参考2) 2. 打开本地host 文件, 在最后一行添加 ...
- Visual Studio 2012 怪异的自动重启
学生在做项目的过程中遇到这种问题: -------------- 用 Visual Studio 2012 开发W中eb 项目时,最近总是莫名其妙的自动重启. 后来试了一下,发现是只要在页面中输入 ...