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. sqlserver中索引优化

    背景: MRO表中TimeStamp nvarchar(32),但实际上它存储的内容是日期(2015-09-09 11:20:30). 现在我要执行这样一个sql语句: Select t10.* fr ...

  2. Caused by: org.springframework.core.NestedIOException: ASM ClassReader failed to parse class file

    springframework.version  3.2.6.RELEASE jdk 1.8

  3. java基础(59):Eclipse把自动括号匹配改成C++那样的(强迫症,没办法)

    在Eclipse开发环境下依次选择: 1.Windows->Preferences->Java->Code Style->Formatter: 2.在Active Profil ...

  4. Cookie—的使用

    编写 Cookie 浏览器负责管理用户系统上的 Cookie.Cookie 通过 HttpResponse 对象发送到浏览器,该对象公开称为 Cookies 的集合. 可以将 HttpResponse ...

  5. java 网络编程(一)---基础知识和概念了解

    java 为用户提供了十分完善的网络功能: 1. 获取网络上的各种资源(URL) 2. 与服务器建立连接和通信(ServerSocket和Socket) 3. 无连接传递本地数据(DatagramSo ...

  6. Swift 注释

    注释就是对代码的解释和说明.目的是为了让别人和自己很容易看懂.为了让别人一看就知道这段代码是做什么用的. 正确的程序注释一般包括序言性注释和功能性注释.序言性注释的主要内容包括模块的接口.数据的描述和 ...

  7. C#抽象类及其方法的学习【转】

    转至 http://www.cnblogs.com/flyinthesky/archive/2008/06/18/1224774.html 在C#中使用关键字 abstract 来定义抽象类和抽象方法 ...

  8. shell 日期加减

    shell 日期加减运算   比如今日是2012-04-22 $ date -d "+1 day" +%Y-%m-%d 2012-04-23   $ date -d "- ...

  9. $("label + input") 匹配所有紧接在 prev 元素后的 next 元素

    描述: 匹配所有跟在 label 后面的 input 元素 HTML 代码: <form> <label>Name:</label> <input name= ...

  10. word2007里插入分节符

    1.打开Word文档,将鼠标定位到需要插入分页符的位置(比如第2页的末尾处),切换到"页面布局"功能区. 2.在"页面设置"分组中单击"分隔符&quo ...