Exact Change(背包HDU2753)

时间:2021-11-07 07:51:38

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. &lbrack;Advanced Algorithm&rsqb; - 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&lpar;DP&rpar;

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

  7. FCC(ES6写法) Exact Change

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

  8. (混合背包 多重背包&plus;完全背包)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&lbrack;背包&rsqb;

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

随机推荐

  1. C&num; TryXXXX模式

    public static int? TrayParse(string text) { int ret; if (int.TryParse(text,out ret)) { return ret; } ...

  2. LeetCode22 Generate Parentheses

    题意: iven n pairs of parentheses, write a function to generate all combinations of well-formed parent ...

  3. Python的tkinter和tkinter&period;messagebox应用-鼠标和键盘命令绑定

    __author__ = 'Administrator' from tkinter import * import tkinter.messagebox class MainWindow: def b ...

  4. python 简单验证码 random模块

    random 模块,产生随机数: chr 将数字转成字母. ascii 数字与字符对应表 链接 import randomtemp=""for i in range(0,4): r ...

  5. redis 系列8 数据结构之整数集合

    一.概述 整数集合(intset)是集合键的底层实现之一, 当一个集合只包含整数值元素,并且这个集合元素数量不多时, Redis就会使用整数集合作为集合键的底层实现.下面创建一个只包含5个元素的集合键 ...

  6. Java&lowbar;Runtime&amp&semi;Process&amp&semi;ProcessBuilder

    目录 一.Runtime类 二.Process类 三.ProcessBuilder类 在Java中想调用外部程序,或者执行命令和可运行文件时,网上的典型实例一般都是通过Runtime.getTime( ...

  7. linux端口详解大全

    0|无效端口,通常用于分析操作系统 1|传输控制协议端口服务多路开关选择器 2|管理实用程序 3|压缩进程 5|远程作业登录 7|回显 9|丢弃 11|在线用户 13|时间 17|每日引用 18|消息 ...

  8. 运维监控-使用Zabbix Server 添加自定义 item

    运维监控-使用Zabbix Server 监控自定义 item  作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客就直接开门见山如何使用Zabbix Server 监控自定义 ...

  9. SLAM产品化的一些思考

    这两年一直在做SLAM产品化的一些工作,有些感触,想和大家分享一下.很多想法只是个人浅见,不当之处还望大家指正. 我这两年分别做了AR眼镜和辅助驾驶方向的开发,说实话,挫折大于成果.SLAM产品化之难 ...

  10. ICDAR 成绩 前5企业和高校

    11月20日,有光学字符识别(OCR)领域“奥斯卡”之称的国际文档分析与识别大会(ICDAR)数据集最新结果公布,中国高校及企业包揽排行榜前五. ICDAR2015数据集前五名及框架名称分别是:云从科 ...