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

POJ1276 Cash Machine详解(多重背包与二进制优化) 我的第二篇题解hhh 虽然网上可以搜到的该题的题解并不少,而且都能确定出是“多重背包”的解法。但是对于“背包”和“物品”的描述,我感觉交待的并不是很清楚,至少对于像我这样的初学者来说,是不容易理解的。 这篇博客可以帮助你更好的理解这个题目,但是你必须有多重背包的基础。 ———————————————————————————————————— 原题 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 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.
题意:
你有n元钱,去银行取出来,银行里有只有固定数量的某些面值货币,用这些货币尽可能多的取出你的钱,输出取出的钱数。
先输入总钱数n,然后输入货币种类数m,随后输入m对数,表示货币数和货币面值

题解:
多种背包模板题
代码:

#include"stdio.h"
#include"string.h"
#include"algorithm"
using namespace std;
int m,n;
int dp[100010],cost[15],num[15];
void zeroonebag(int t)
{
	for(int i=n;i>=t;i--)//从n开始逆序更新
	dp[i]=max(dp[i],dp[i-t]+t);
} 
void completebag(int t)//t面值的货币储量大于总钱数
{
	for(int i=t;i<=n;i++)//更新t到总钱数n之间的dp的值,因为此时剩下的钱数可以选择此面值的货币
	dp[i]=max(dp[i],dp[i-t]+t);
}
void multibag()
{
	int k=1;
	int count=0;
	for(int i=1;i<=m;i++)//遍历每一个面值的货币
	{
		if(cost[i]>n)//如果单张此面值的货币就大于总钱数,则直接找下一面值
			continue;
		if(num[i]*cost[i]>=n)//此面值的货币储量大于总钱数
			completebag(cost[i]);
		else
		{
			k=1;
			count=num[i];
			while(k<count)
			{
				zeroonebag(k*cost[i]);
				count-=k;
				k*=2;//用二进制的形式进行优化
			}
			zeroonebag(count*cost[i]);//对二进制优化剩下的货币进行处理
		}
	}
}
int main()
{
	while(~scanf("%d",&n))
	{
		scanf("%d",&m);
		memset(num,0,sizeof(num));
		memset(cost,0,sizeof(cost));
		memset(dp,0,sizeof(dp));
		for(int i=1;i<=m;i++)
		scanf("%d %d",&num[i],&cost[i]);
		multibag();
		printf("%d\n",dp[n]);
	}
}

代码2
直接进行二进制优化,使题目变成01背包问题

#include"stdio.h"
#include"string.h"
#include"algorithm"
using namespace std;
int w[100010],dp[100010];
int main()
{
	int m,n;
	while(~scanf("%d %d",&n,&m))
	{
		memset(dp,0,sizeof(dp));
		int i,j,t,x,y,k=0;
		for(i=0;i<m;i++)
		{
			scanf("%d %d",&x,&y);
			t=1;
			while(x>=t)
			{
				w[k++]=y*t;
				x-=t;
				t*=2;
			}
			w[k++]=x*y;
		}
		for(i=0;i<k;i++)
		{
			for(j=n;j>=w[i];j--)
			dp[j]=max(dp[j-w[i]]+w[i],dp[j]);
		}
		printf("%d\n",dp[n]);
	}
	return 0;
}
【机器学习】实验2,基于回归分析的大学综合得分预测(完整代码实现+分析) 大学排名是一个非常重要同时也极富挑战性与争议性的问题,一所大学的综合实力涉及科研、师资、学生等方方面面。目前全球有上百家评估机构会评估大学的综合得分进行排序,而这些机构的打分也往往并不一致。在这些评分机构中,世界大学排名中心(Center for World University Rankings,缩写CWUR)以评估教育质量、校友就业、研究成果和引用,而非依赖于调查和大学所提交的数据著称,是非常有影响力的一个。 阅读详情

相关推荐

仿真每日一练 | 有限元模态分析详解

结构的振动一直是工业领域产品分析的重点,在振动的仿真分析中,模态分析是其最基本的分析类型,通过研究结构的固有频率和振型对产品的设计提供指导。2.2、在interaction模块,通过special->Spring,在各点之间分别创建弹簧单元,弹簧刚度为1000。2.1、在interaction模块,创建两个RP点,与两侧RP点各间隔100大小,如图3所示;其中M、K、C均为n*n的矩阵,s、x、v为n*1的矢量,n为系统自由度数量。3、在Assembly模块创建装配体,两点之间的距离为100,如图2所示;

ansysxuexizhe的博客 1655

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!荣耀之路! 8150

9 海康视觉平台VisionMaster 上手系列:典型案例之螺丝机案例

海康视觉平台VisionMaster 上手系列:典型案例之螺丝机案例 快速入门 工业机器视觉 机器视觉

HuangGong2022的博客 7176

POJ 1276 Cash Machine(多重背包)

题目地址:

scf0920 1047

mtk-drm commit送显流程(二):内核态atomic_commit实现解析

本文深入解析了mtk-drm框架中atomic_commit机制的内核态实现流程,详细介绍了从用户态触发到硬件提交的完整调用链,包括状态检查、硬件提交等关键阶段。特别分析了MTK平台在动态刷新率、内存带宽优化等方面的特有实现策略,为开发者提供了调试技巧和常见问题解决方案。

weixin_30839881的博客 1305

代号OneNET02 STM32+ESP8266 WIFI系列-第2节-STM32单片机通过ESP8266连接WIFI访问OneNET OTA服务器实现SOTA远程程序升级

第2节-STM32单片机通过ESP8266连接WIFI访问OneNET OTA服务器实现SOTA远程程序升级,这一节主要是实现通过OneNET OTA服务器将需要更新的程序远程下载到STM32单片机,这一节主要是引导程序和应用程序的讲解。

weixin_44862509的博客 1904

POJ1276-Cash Machine(多重背包)

题目链接:poj挂了以后补上 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 dis

Yorozuya 564

GD32F105RBT6 keil工程模板

创建GD32F105RBT6的keil初始基础工程,未来的所有外设驱动都是基于这个基础工程进行扩展。

RMDYBW的博客 1927

多重背包模板 C++

多重背包模板 多重背包: 有N种物品和一个容量为V的背包。第i种物品最多有numi件可用。 每件物品的重量是wi,价值是vi。 求解将哪些物品装入背包可使这些物品的重量总和不超过背包容量,且价值总和最大。 const int maxn = 100005; int w[maxn], v[maxn], num[max...

Patience 2105

RoboMaster电控入门:手把手教你用STM32CubeMX配置大疆C板CAN总线驱动GM6020电机

本文详细介绍了如何使用STM32CubeMX配置大疆C板CAN总线驱动GM6020电机,适用于RoboMaster电控入门。从硬件准备到CubeMX工程初始化,再到CAN过滤器配置和电机控制协议解析,手把手教你避开常见配置陷阱,实现精准控制。

weixin_42524762的博客 267

SAP PI 系统复制,不只是把数据库拷过去,而是一次集成拓扑的重新落位

文章摘要:SAP PI/PO系统刷新生产数据到测试环境时,标准系统复制无法自动处理集成中枢特有的环境绑定问题。系统复制后,PI仍保留生产环境的连接配置,可能导致测试系统向生产环境发送消息。SAP提供的CTC模板可自动化部分后续配置,但无法替代人工对接口风险的全面评估。关键在于系统复制前绘制接口风险地图,复制后进行分层次验证:禁用生产连接、重定向测试接口、复核安全配置,并通过代表性链路测试确保系统行为符合预期。这需要结合SAP标准流程与业务语义判断,形成完整的治理闭环。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 84

POJ1267 Cash machine 多重背包

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

WA是一笔财富 319

AI Agent开发第85课-DeepSeek-OCR 安装全指南:从 Transformers 到 vLLM,手把手教你跑通企业级 OCR

本文系统讲解 DeepSeek-OCR 的两种部署方式——基于 Hugging Face Transformers 的标准推理与基于 vLLM 的高性能加速推理。详细说明 CUDA 与 PyTorch 版本匹配、环境验证、依赖安装等关键细节,并提供可直接运行的完整代码示例。无论你是单卡调试还是高并发服务部署,都能在这里找到清晰路径。

打造全国最全的AI Agent开发知识领域的博客 2542

POJ 1276 Cash Machine【多重背包】

题意:有各种不同面值的货币,每种面值的货币有不同的数量,请找出利用这些货币可以凑成的最接近且小于等于给定的数字cash的金额. 样例1: 要取735,取款机内有3种钱:4张125的,6张5的,3张350的. 735=3*125+2*5+1*350 动态规划,多重背包,可以二进制优化.

AC_Dreameng 1105

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

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

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

【POJ】1276 Cash Machine(多重背包)

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 distinc

Silence的程序实验室 996

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

poj1276 Cash Machine(多重背包)

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

zxc757126309的博客 188

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

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

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 exactl

Learning_is_endless的博客 368

【POJ 1276】Cash Machine

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

ACMer' 1069

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 denominatio

WZJRJ28 739

【DP|多重背包】POJ-1276 Cash Machine

Cash Machine Time Limit: 1000MS   Memory Limit: 10000K       Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver appropriate

JSure的代码库 1088

【动态规划】POJ 1276 Cash Machine

这段时间要沉迷刷题一段时间了,就让优快云陪我一起吧! 一、题目大意 题目的大致意思就是,题目输入一个事务,其中包括总钱数cash,总的货币种数N,以及每种货币的面额Dk和每种货币的库存nk,然后要求你在给定这些输入的情况下,求解不超过总钱数的最大钱数。 二、题目思路以及AC代码 这题其实也没啥写题解的必要,毕竟大家一看就能看出来,这就是一个多重背包的问题。 一提到这里我就感触颇深,由于之前好...

Johnson的博客 280

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 多重背包

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

Houheshuai的专栏 538

POJ 1276 Cash Machine (多重背包)

题意:我现在要从ATM中取钱,(ATM机中能取出的钱数最多为M)ATM里面有若干种货币,每一种都有对应的货币面额和张数。 问现在ATM能够取出来的 小于等于M的最大金额。 分析:若将M理解为背包体积(V ),而每种货币的面额理解成 value,货币的面额同样理解成cost,那么这个问题就是一个多重背包问题。 #include #include #define MAX_CASH

我要一步一步往上爬 584

POJ 1276 Cash Machine 多重背包+二进制优化

来源:http://poj.org/problem?id=1276 题意:就是有一些一定数量和一定面值的钱,还有一个总钱数,让你在所给的纸币中选出一定数目的钱,使这些钱的和小于等于总钱数,并且使这个和尽可能的大。 思路:将总钱数看作是背包的体积,将钱币的面值看作是物体的体积,物体的价值和体积相等。即转化为多重背包问题,由于数据比较大,所以需要用到二进制优化。 代码: #include #

969

poj 1276 Cash Machine(多重背包)

Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19122   Accepted: 6659 Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver

nealgavin的专栏 608
上一篇: 第一周下记忆化搜索(滑雪)
下一篇: 第二周上二分图(Oil Skimming HDU - 4185)
JSASL
博客等级 码龄7年 12粉丝 52原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值