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)的更多相关文章

  1. FCC高级编程篇之Exact Change

    Exact Change Design a cash register drawer function checkCashRegister() that accepts purchase price ...

  2. Exact Change(01背包)

    描述 Seller: That will be fourteen dollars. Buyer: Here's a twenty. Seller: Sorry, I don't have any ch ...

  3. Exact Change

    设计一个收银程序 checkCashRegister() ,其把购买价格(price)作为第一个参数 , 付款金额 (cash)作为第二个参数, 和收银机中零钱 (cid) 作为第三个参数. cid ...

  4. [Advanced Algorithm] - Exact Change

    题目 设计一个收银程序 checkCashRegister(),其把购买价格(price)作为第一个参数 , 付款金额 (cash)作为第二个参数, 和收银机中零钱 (cid) 作为第三个参数. ci ...

  5. Exact Change FreeCodeCamp

    function checkCashRegister(price, cash, cid) { var change; var sumCid = 0; // Here is your change, m ...

  6. UVA-11517 Exact Change(DP)

    题目大意:有n张钞票,面值可能不同.你要买一件东西,可能需要找零钱.问最少付多少钱,并求出最少的钞票张数. 题目分析:定义状态dp(i,w)表示前i张钞票凑成w元需要的最少钞票张数.则状态转移方程为d ...

  7. FCC(ES6写法) Exact Change

    设计一个收银程序 checkCashRegister() ,其把购买价格(price)作为第一个参数 , 付款金额 (cash)作为第二个参数, 和收银机中零钱 (cid) 作为第三个参数. cid  ...

  8. (混合背包 多重背包+完全背包)The Fewest Coins (poj 3260)

    http://poj.org/problem?id=3260   Description Farmer John has gone to town to buy some farm supplies. ...

  9. POJ3260The Fewest Coins[背包]

    The Fewest Coins Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6299   Accepted: 1922 ...

随机推荐

  1. centos 6.4下的postgresql 9.2使用

    http://www.cnblogs.com/mchina/archive/2012/06/06/2539003.html

  2. Applescript 带参数调用某个App的方法

    do shell script "open '/Users/eran/Documents/Workground/DragonAdventure/FlashCode/tools/SWFInfo ...

  3. topsort

    top排序每次入队的是限制情况为0的... 就可以直接记录了,每次出队的就也是为0的,出队结果对每个子节点或者被限制节点,的限制条件-1 然后再判断有没有后继有没有为0的,有就入队! 出入队一次,找后 ...

  4. 转:python类型转换、数值操作

    类型转换   1 函数                                    描述 2 int(x [,base ])                      将x转换为一个整数 3 ...

  5. [原创]java WEB学习笔记77:Hibernate学习之路---Hibernate 版本 helloword 与 解析,.环境搭建,hibernate.cfg.xml文件及参数说明,持久化类,对象-关系映射文件.hbm.xml,Hibernate API (Configuration 类,SessionFactory 接口,Session 接口,Transaction(事务))

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  6. c++之路起航——指针

    c++一阶指针 定义 存储类型名 数据类型 * 指针变量名: Eg:int *a://定义了一个指向整型的指针 a: 指针使用方法 int a,*b; b=&a;//表明将a的地址赋值给b: ...

  7. HDU 4833 Best Financing(DP)(2014年百度之星程序设计大赛 - 初赛(第二轮))

    Problem Description 小A想通过合理投资银行理财产品达到收益最大化.已知小A在未来一段时间中的收入情况,描述为两个长度为n的整数数组dates和earnings,表示在第dates[ ...

  8. JS 实现中英文翻译

    缺点就是还是会闪出中文,但是效果还行. var langPackage = { "主题":"Title", "下一页":"Next ...

  9. ansible自动化运维工具的安装与使用

    运行环境 centOS6.6 ansible ansible的功能还是比较多的,博主只用它在集群上进行批量部署软件和维护的功能,其他不多做研究,有需要的话这篇文章会慢慢补充. ansible特点 轻量 ...

  10. 夺命雷公狗ThinkPHP项目之----企业网站20之网站前台头尾分离

    我们的网站直接让他头尾进行分离即可: 然后在代码里面找到id 为header的这段代码: 然后将整个div的内容都给弄出来,然后在view里面创建一个Public的目录,然后在创建一个header.h ...