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;
 }


DeepSeek+Dify实战:零代码构建智能SQL生成器 本文详细介绍了如何利用DeepSeek和Dify零代码构建智能SQL生成器,帮助非技术人员通过自然语言查询数据库。文章涵盖从数据准备、知识库设计到Agent配置的全流程,并提供了优化技巧和实际应用案例,显著提升团队数据查询效率。 阅读详情

相关推荐

告别USB转接:在STM32探索板上用SPI跑通ESP-Hosted,实现Wi-Fi/BLE连接

本文详细介绍了如何在STM32F469I-DISCO开发板上通过SPI接口实现ESP-Hosted方案,为嵌入式设备添加Wi-Fi和蓝牙连接功能。通过硬件配置、协议栈优化和性能测试,展示了这一创新方案的成本效益和开发效率,特别适合资源受限但需要稳定无线连接的嵌入式应用场景。

weixin_34046298的博客 252

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

从原理图到代码:深入解析SX1278 LoRa模块与STM32的SPI通信驱动(附完整工程)

本文深入解析了SX1278 LoRa模块与STM32的SPI通信驱动实现,从硬件原理图到完整工程代码,详细介绍了LoRa技术的通信原理、寄存器配置、SPI接口设计及低功耗优化策略。适合物联网开发者和嵌入式工程师提升远距离无线通信能力,附完整工程代码助力快速开发。

weixin_42715608的博客 424

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

概率论 知识图谱 + 实战案例解析

本文通过构建概率论知识图谱,系统梳理了从随机试验、概率、条件概率到随机变量与分布的核心概念及其内在联系。结合朴素贝叶斯分类器和蒙特卡洛模拟等实战案例,深入解析了条件概率、独立性及大数定律等理论如何应用于机器学习与实际问题求解,旨在帮助读者建立结构化思维,将理论有效转化为解决实际问题的能力。

weixin_29051193的博客 267

单细胞专题(1) | 单细胞转录组研究样本准备指南(上)

单细胞RNA测序(scRNA-seq)是目前最前沿,能进行大规模实验的转录组研究技术,为理解细胞组成和动态生物学过程提供了很多新的见解。单细胞研究已经为我们理解发育过程,老化和疾病(如癌症)等提供了大量数据。在这里,小编主要就前期的样本准备与保存需注意问题给大家进行讲解,以备大家在不同的研究场景和研究背景下,走好单细胞转录组学研究的第一步。

bioyigene的博客 4231

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

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

学习笔记 4143

Comsol 弧形声学换能器声聚焦仿真

1. 弧度和曲率半径:弧形声学换能器的形状和曲率半径对声聚焦效果有重要影响。通常,较小的曲率半径和较大的弧度可以实现更强的声聚焦效果。弧形声学换能器声聚焦是指将声波能量集中在弧形声学换能器的特定区域内,以实现更强的声场强度和分辨率。4. 数值仿真和优化:使用声学仿真软件进行数值仿真可以帮助设计和优化弧形声学换能器的声聚焦效果。通过调整几何参数、材料特性和激励信号等参数,可以模拟和优化声聚焦效果。2. 激励信号:通过调整激励信号的频率和幅值,可以实现声波在弧形声学换能器中的聚焦效果。

声学黑洞仿真工作室 1618

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

NVIDIA Isaac Sim 入门教程(一)

文档分为以下几个部分: 简介、安装指南、API 文档、教程、手册和参考资料。简介部分是对 Omniverse Isaac Sim 的基本概述,以及它如何融入 NVIDIA Omniverse™ 生态系统。它还概述了 Omniverse Isaac Sim 的架构、工作流程和文档布局。

机器人控制与仿真应用 1万+

复旦微实现串口在线升级BOOT.BIN

将该 demo 生成的 app(升级用)和 fsbl、最简化 bit 文件(可选)打包成 BOOT.BIN,作为 golden image 烧写在 qspi flash 偏移 0 地址开始的位置,其中最简化 bit 文件为在线升级功能提供必要的物理通道(如果不需要,可以不打包该文件),golden image 一旦发布基本不需要进行升级,由该 golden image 负责和上位机进行通信交互,完成业务 app、业务 bit 文件、uboot 以及操作系统 image 等文件在线升级,在qspi flash

wangjie36的博客 3174

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

springboot&mybatis中数组和字符串数据类型的转换

/前端数组字段//数据库中字段/*与前端交互时使用的get/set*/System.out.println("接收前端数据");/*与前端交互时使用的get/set*//*与数据库交互时使用的get/set*/System.out.println("从数据库中读取数据");/*与数据库交互时使用的get/set*/

l_q_l的专栏 2475

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

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

Bright 1154

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

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

【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

poj1276 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 denominations, say Dk, k=1,N, and for each denomination Dk the machine has a sup

ly59782的博客 370
上一篇: 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、付费专栏及课程。

余额充值