POJ - 1276 Cash Machine 多重背包模板题

DB(一):数据库概述、SQL概述、Oracle数据类型 包括Oracle、DB2、SQL Server、MySQL数据库概述;数据定义语言DDL、数据操作语言DML、事务控制语言TCL、数据查询语言DQL、数据控制语言DCL语言介绍;NUMBER 、CHAR、VARCHAR2、DATE数据库类型;CHAR和VARCHAR2的存储编码、CHAR和VARCHAR2的最大长度、LONG和CLOB类型。 阅读详情

Cash Machine
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 34097 Accepted: 12357

Description

A Bank plans to install a machine for cash withdrawal. The machine is able to deliver appropriate @ bills for a requested cash amount. The machine uses exactly N distinct bill denominations, say Dk, k=1,N, and for each denomination Dk the machine has a supply of nk bills. For example, 

N=3, n1=10, D1=100, n2=4, D2=50, n3=5, D3=10 

means the machine has a supply of 10 bills of @100 each, 4 bills of @50 each, and 5 bills of @10 each. 

Call cash the requested amount of cash the machine should deliver and write a program that computes the maximum amount of cash less than or equal to cash that can be effectively delivered according to the available bill supply of the machine. 

Notes: 
@ is the symbol of the currency delivered by the machine. For instance, @ may stand for dollar, euro, pound etc. 

Input

The program input is from standard input. Each data set in the input stands for a particular transaction and has the format: 

cash N n1 D1 n2 D2 ... nN DN 

where 0 <= cash <= 100000 is the amount of cash requested, 0 <=N <= 10 is the number of bill denominations and 0 <= nk <= 1000 is the number of available bills for the Dk denomination, 1 <= Dk <= 1000, k=1,N. White spaces can occur freely between the numbers in the input. The input data are correct. 

Output

For each set of data the program prints the result to the standard output on a separate line as shown in the examples below. 

Sample Input

735 3  4 125  6 5  3 350
633 4  500 30  6 100  1 5  0 1
735 0
0 3  10 100  10 50  10 10

Sample Output

735
630
0
0

Hint

The first data set designates a transaction where the amount of cash requested is @735. The machine contains 3 bill denominations: 4 bills of @125, 6 bills of @5, and 3 bills of @350. The machine can deliver the exact amount of requested cash. 

In the second case the bill supply of the machine does not fit the exact amount of cash requested. The maximum cash that can be delivered is @630. Notice that there can be several possibilities to combine the bills in the machine for matching the delivered cash. 

In the third case the machine is empty and no cash is delivered. In the fourth case the amount of cash requested is @0 and, therefore, the machine delivers no cash.

Source


二进制优化的多重背包

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<set>
typedef long long ll;
using namespace std;
int main()
{
	int t,V,v,n,dp[100005],i,j,m;
	while(~scanf("%d",&V))
	{
		memset(dp,0,sizeof(dp));
		scanf("%d",&t);
		while(t--)
		{
			scanf("%d %d",&n,&v);
			for(i=1;(i<<1)<n;i<<=1)
			{
				for(j=V;j>=i*v;j--)
				{
					dp[j]=max(dp[j],dp[j-i*v]+i*v);
				}
			}
			m=n-i+1;
			for(j=V;j>=m*v;j--)
			{
					dp[j]=max(dp[j],dp[j-m*v]+m*v);
			}	
		}
		printf("%d\n",dp[V]);
	} 
	return 0;
 }

单调队列优化的多重背包

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<set>
typedef long long ll;
using namespace std;
struct node{
	int num,value;
}que[500000]; 
int head,tail,dp[100005];
void push(int x,int y){
	while(tail>head&&que[tail-1].value<y)
	tail--;
	que[tail].num=x;
	que[tail++].value=y;
	return ;
}
int main()
{
	int i,j,V,n,c,w;
	while(~scanf("%d %d",&V,&n))
	{
		memset(dp,0,sizeof(dp));
		while(n--)
		{
			scanf("%d %d",&c,&w);
			c=min(c,V/w);
			for(i=0;i<w;i++)
			{
				head=0;
				tail=0;
				for(j=0;j<=(V-i)/w;j++)
				{
					push(j,dp[j*w+i]-j*w);
					while(tail>head&&que[head].num<j-c)
					head++;
					dp[j*w+i]=que[head].value+j*w;
				}
			}
		}
		printf("%d\n",dp[V]);
	}
	return 0;
 }


Vue3 + OpenLayers 开发指南(一) - 环境搭建与基础配置 本文详细介绍了如何从零开始搭建Vue3与OpenLayers的集成开发环境。通过项目初始化、核心依赖安装、目录结构规划及基础配置,手把手引导开发者创建一个结构清晰、可维护的现代化WebGIS应用骨架,为后续添加地图图层和交互功能奠定坚实基础。 阅读详情

相关推荐

LaTeX公式转Word竟这么简单?Python三行代码实现学术论文格式无忧

本文介绍如何利用Python的latex2word库,仅需三行代码即可将LaTeX数学公式精准转换为Word原生Office Math对象,解决学术写作中LaTeX与Word格式割裂的难题。该方法支持批量处理、格式保真,并能与Markdown、Jupyter Notebook等工具集成,构建自动化写作流程,实现学术论文格式无忧。

qsc90123456的博客 296

poj1276

Cash Machine Time Limit:1000MS Memory Limit:10000K Total Submissions:15684 Accepted:5369 Description A Bank plans to install a machine for cash withdrawal. ...

weixin_34174105的博客 297

(完整版)从零开始教你玩转ComfyUI-保姆级部署教程-手把手带你ComfyUI工作流搭建

ComfyUI就像拥有一支神奇魔杖,可以轻松创造出令人惊叹的AI生成艺术。从本质上讲,ComfyUI是构建在Stable Diffusion之上的基于节点的图形用户界面(GUI),而Stable Diffusion是一种最先进的深度学习模型,可以根据文本描述生成图像。但ComfyUI真正特别之处在于,它如何让像你这样的艺术家释放创造力,将你最疯狂的想法变为现实。想象一下有一块数字画布,你可以通过连接不同的节点来构建自己独特的图像生成工作流,每个节点代表一个特定的功能或操作。

php源码 4万+

cx_Oracle和oracledb的爱恨情仇

实习生:我要用python访问oracle,我究竟该用哪个库啊,网上搜的有两个,一个是cx_Oracle另一个是oracledb,这两个有什么区别?我:话说那是2022年5月一个阴云密布的日子,甲骨文发布了python oracledb驱动程序。与早期的cx_Oracle驱动程序(创建于1999年)一样,该模块允许Python应用程序连接到Oracle数据库,执行SQL和PL/SQL语句,并执行SODA操作。这两个驱动程序都实现了Python数据库API规范2.0版,有很多扩展,只有一些必要的遗漏。

SunnyRivers 2591

ESP-IDF 利用ESP32构建智能音乐播放器(一)---------- 实现SD卡MP3文件的解码与播放

本文详细介绍了如何利用ESP32和ESP-IDF开发框架,从零开始构建一个智能音乐播放器。文章重点讲解了如何通过SD卡模块读取MP3文件,并集成Helix MP3解码库进行软件解码,最终通过I2S接口驱动音频模块实现播放。内容涵盖硬件连接、软件环境搭建、核心解码与输出逻辑,为嵌入式音频开发提供了完整的实战指南。

weixin_29163797的博客 234

Golang内存管理:GC调优实战案例

在Golang编程中,内存管理是一个非常重要的方面。良好的内存管理可以提高程序的性能和稳定性,而GC(垃圾回收)调优则是优化内存管理的关键手段。本文的目的就是通过实际案例,深入讲解Golang中GC调优的方法和技巧,让读者能够在实际项目中更好地进行内存管理和性能优化。范围涵盖了从核心概念的解释到具体代码实现和调优过程的详细分析。本文首先会介绍一些相关的术语和核心概念,让读者对Golang内存管理和GC有一个基本的认识。然后通过故事引入的方式,更加形象地解释核心概念及其相互关系。

AI Agent Harness Engineering 1118

Python连接Oracle数据库:cx_Oracle与oracledb库的比较与选择

Python 访问 Oracle可以通过两种Oracle官方库:旧驱动:cx_Oracle(需要安装Oracle Instant client)新驱动:oracledb(Oracle数据库12.1或更高版本不需要安装 Oracle Instant client)

学习笔记 4143

第20期-英伟达笔试题型解析

英伟达作为AI领域的缔造者及硬件基石,是这几年最为炙手可热的公司。英伟达以其高性能GPU设计闻名,硬件工程师岗位的招聘通常包括笔试和技术面试,考察领域涵盖数字电路设计、处理器架构、嵌入式系统和信号处理等。通过搜索公开平台,收集了候选人的面试经历和相关技术问题,,通过分析公开平台收集的笔试面试经验,结合英伟达的GPU架构特点,整理出可能涉及的典型题目,并提供详细解析。 以下是基于英伟达(Nvidia)硬件工程师笔试题型的解析与总结,涵盖核心知识点、解题思路及案例分析。

启芯硬件的博客 746

POJ 1276 Cash Machine

Cash Machine   A Bank plans to install a machine for cash withdrawal. The machine is able to deliver appropriate @ bills for a requested cash amount. The machine uses exactly N distinct bill d...

dbt3498的博客 270

uniapp [全端兼容] - 详细实现引入使用 Google 谷歌地图显示及搜索城市地点,提供地图标点、地址搜索、定位导航、中文显示及汉化、地图打位置标点显示、搜索框搜索功能、省市区详细经纬度数据等

uniapp,h5移动端网页,微信小程序,Android,苹果ios,app,nvue,谷歌地图,官方,谷歌官方文档,googlemap,申请key,uniApp谷歌地图,详细使用教程,无法访问,集成谷歌地图,app引入谷歌地图,选点定位,地图导航,位置搜索,城市搜索,文字地点模糊查询,地点省市区详细数据,经纬度信息,markers地图标记点,一直报错怎么办,接入google地图不显示报错,谷歌地图插件,vue3,vue2,组件,Key申请流程,谷歌key怎么申请,api文档,网站打不开,用什么插件,在u

🏆 前端领域优质创作者 1万+

开源工具对比:5款主流大数据脱敏框架评测

数据脱敏(Data Masking/Anonymization),也称为数据漂白、数据去标识化,是指在不影响数据可用性的前提下,通过一定的技术手段对敏感信息进行处理,使其无法被识别或关联到具体个人或实体的过程。当应用程序向数据库写入数据(INSERT/UPDATE)时,ShardingSphere 会拦截 SQL,对配置了脱敏规则的字段,使用指定的算法进行加密/脱敏处理,然后将处理后的数据写入到。如果你的答案是“是”,那么数据脱敏,尤其是针对海量数据的。好的,各位技术同仁,数据安全领域的探索者们,大家好。

小程序开发 1184

poj1276Cash Machine【多重背包模板题】

SubmitStatus Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver appropriate @ bills for a requested cash amount. The machine uses exactly N disti

MissZhou要努力 2547

Vue3 - 详细实现流程图编辑器可视化平台示例代码,打造前端web网页流程图在线编辑绘制操作展示,vue3自定义绘制流程图在页面上画流程图功能(提供各种操作按钮及节点控件,界面美观交互式流程图插件)

vue3,绘制流程图,在线流程图制作,在vue3中实现流程图,vue3做一个制作流程图的功能,vue3流程图组件,vue3流程图,流程图绘制展示,vue3流程图可视化在线编辑源码,基于VUE3的在线绘制流程图工具,vue3+logicflow流程图,绘制动态流程图并自定义节点和线,vue使用logicFlow绘制流程图,生成各种流程图图形可连接点移除,基于Vue的流程图组件,工作流程图,流程编辑器,vue3流程组件,vue-flow绘制流程图,绘制流程图示例demo,vue3 logicflow流程图使用教

🏆 前端领域优质创作者 9967

POJ 1276 Cash Machine(动态规划、多重背包)

这道题是典型的多重背包题,就是说给你的物品有一个去的件数的上限,我们常用的解决多重背包问题的方法是:将件数乘以费用超过背包容量的物品可以使用完全背包的方法进行求解;除此之外的物品我们采用二进制的思想,将该物品的件数拆分成二进制上的数后,看成一个物品进行01背包。这样通过这两种方法

Bright 1154

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

poj1276 Cash Machine(多重背包)

Cash Machine 本题大意:给出一个最大金额,然后给出k中币的金额和数量,求能正好提供的最大小于等于最大金额的金额。 本题是多重背包模板题,直接套上模版就可以解决。 #include <iostream> #include <cstdio> #include <algorithm> #include <string> #include &lt...

zxc757126309的博客 188

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

POJ1267 Cash machine 多重背包

传送门:POJ1276 题意:给定一种新货币的多种面额和每种面额的数量和一个数额上限,问在不超过数额上限的前提下用给定的货币最多能凑出多大数额来。 思路:典型的多重背包,可以说是模板题,只不过本题中w[i]==v[i]而已。 可以用二进制优化,也可以单调队列优化。 不明白多重背包是啥的请自行搜索《背包九讲》。 二进制优化代码: #include #include #include #i

WA是一笔财富 319

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

【POJ 1276】Cash Machine

【POJ 1276】Cash Machine多重背包模板题 这里分享个背包的好文章:http://blog.youkuaiyun.com/lyhvoyage/article/details/8545852 或者直接去看背包九讲 看了背包九讲 背包从此不用讲…… 麻麻再也不用担心。。。就是个模板 不过我写的极度精简……输入跟处理都放一块了。。。耗时不少= =建议拆开写 时间用的应该能少点。。。代码如下:#in

ACMer' 1069

Cash Machine POJ - 1276 多重背包

一、内容 A Bank plans to install a machine for cash withdrawal. The machine is able to deliver appropriate @ bills for a requested cash amount. The machine uses exactly N distinct bill denominations, say ...

Tizzi的博客 843

POJ 1276 Cash Machine ( 多重背包模板题目 )

Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver appropriate @ bills for a requested cash amount. The machine uses exactly N distinct bill denomin

深海沧澜的博客 332

[多重背包] POJ1276 Cash Machine (二进制优化)

题目 有各种不同面值的货币,每种面值的货币有不同的数量,请找出利用这些货币可以凑成的最接近且小于等于给定的数字cash的金额。 思路 多重背包模板题。 1.朴素的多重背包,每个状态转移时枚举用的某种货币的个数,复杂度:O(C∗∑n[i])O(C∗∑n[i])O(C*\sum n[i]),TLE。 2.二进制优化,背包九讲里面有。 此处应该注意的是,1…n的任意整数用尽量少的几个整数表示...

OI - IceCab 473

Cash Machine POJ - 1276 (多重背包)

链接:http://poj.org/problem?id=1276 题意:给出一个n 代表最多拿的值为n, m种钱币, 每种有 x个, 值为y,问在不超过n的情况下最多能拿多少钱。解题思路:也算是多重背包的模板题了,做法是将多重背包化成0 1背包, 将每种钱币分别分成 1个 2个 4个…..当不够分的时候就将剩下的也当作一组, 这样每一组都对应一个不同的值 y*1, y*2, y*4….然后直接0

sdut16szq的博客 412

poj 1276 Cash Machine(多重背包+倍增)

Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17441   Accepted: 6038 Description

志当存高远 1339

第一周下多重背包(Cash Machine POJ - 1276 )

A Bank plans to install a machine for cash withdrawal. The machine is able to deliver appropriate @ bills for a requested cash amount. The machine uses exactly N distinct bill denominations, say Dk, k=1,N, and for each denomination Dk the machine has a sup

JSASL的博客 214

POJ1276:Cash Machine(多重背包)

Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver appropriate @ bills for a requested cash amount. The machine uses exactly N distinct bill denominatio

ACM!荣耀之路! 1724

POJ 1276 Cash Machine (多重背包&单调队列)

http://poj.org/problem?id=1276 模板题。 完整代码: /*110ms,4316KB*/ #include #include const int mx = 1005; const int mxw = 1000005; int w[mx], m[mx], dp[mxw], deq[mxw], deqv[mxw]; int solve(int m

AC,∑ndless 1846

POJ1276:Cash Machine(多重背包)

题目:http://poj.org/problem?id=1276 多重背包模板题,没什么好说的,但是必须利用二进制的思想来求,否则会超时,二进制的思想在之前的博客了有介绍,在这里就不多说了。 #include &lt;iostream&gt; #include &lt;string.h&gt; #include &lt;stdio.h&gt; #include &lt;algori...

weixin_33895475的博客 108
上一篇: POJ - 1836 Alignment
下一篇: 背包模板(01,完全,多重背包的二进制优化和单调队列优化
jerans
博客等级 码龄10年 54粉丝 255原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值