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

poj 1276(多重背包) 我的英语差的不只一点点啊,题意看不完全懂,直接看别人的解题报告才把题意弄清(我很菜),但是感觉这题完全就是完全背包基础知识,只要将cost视为与weight完全相等就ok。 #include #include #include using namespace std; #define N 11 int dp[100010]; int num[N]; int a[N]; int V,n; inli 阅读详情

http://poj.org/problem?id=1276


模板题。


完整代码:

/*110ms,4316KB*/

#include<cstdio>
#include<cstring>
const int mx = 1005;
const int mxw = 1000005;

int w[mx], m[mx], dp[mxw], deq[mxw], deqv[mxw];

int solve(int maxw, int n)
{
	memset(dp, 0, sizeof(dp));
	int i, a, j, s, t, val;
	for (i = 0; i < n; ++i)
		for (a = 0; a < w[i]; ++a)///枚举a=j%w[i]
		{
			s = t = 0; ///deque的头和尾
			for (j = 0; j * w[i] + a <= maxw; ++j)
			{
				val = dp[j * w[i] + a] - j * w[i];///划归为可重复使用的值
				while (t > s && deqv[t - 1] <= val) --t;///保证deque的队首是最大的
				deq[t] = j;
				deqv[t++] = val;
				dp[j * w[i] + a] = deqv[s] + j * w[i];///从双端队列的头部取出最大值
				if (deq[s] == j - m[i]) ++s;///这一头部已无法使用
			}
		}
	return dp[maxw];
}


int main()
{
	int maxw, n, i;
	while (~scanf("%d%d", &maxw, &n))
	{
		for (i = 0; i < n; ++i)
			scanf("%d%d", &m[i], &w[i]);
		printf("%d\n", solve(maxw, n));
	}
	return 0;
}

附:相似题目

HDU 2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活


POJ1276 Cash Machine详解(多重背包与二进制优化) 我的第二篇题解hhh 虽然网上可以搜到的该题的题解并不少,而且都能确定出是“多重背包”的解法。但是对于“背包”和“物品”的描述,我感觉交待的并不是很清楚,至少对于像我这样的初学者来说,是不容易理解的。 这篇博客可以帮助你更好的理解这个题目,但是你必须有多重背包的基础。 ———————————————————————————————————— 原题 POJ1276 Cash Machine 题意 有... 阅读详情

相关推荐

晒代码之二&mdash;&mdash;多重背包(POJ1276)

晒代码之二&mdash;&mdash;多重背包(POJ1276)

poj1276(多重背包)

链接:点击打开链接 题意:给出一个价值sum,然后给出n钱币的个数与价值,要求用这些钱币组成一个最接近sum的值(总和不大于sum) 代码: #include #include #include #include using namespace std; bool f[100005]; long long v[105],w[105],used[100005]; int

stay_accept的专栏 569

【大模型】大模型知识蒸馏 综述解读(Knowledge Distillation of Large Language Models)

知识蒸馏的(论文)方法总结。归纳了通过数据(如指令、对话、多模态)和训练方法(SFT、RL、数据增强),将复杂技能(如对齐、工具使用、多模态)蒸馏到开源模型的核心框架,附典型模型示例(如Alpaca、LLaVA)。

songxia928_928的博客 4335

POJ 1276 多重背包问题

题目在 http://poj.org/problem?id=1276 题目大意是说,我现在要从ATM中取钱,M ATM里面有若干种货币,每一种都有对应的货币面额和张数。 问现在ATM能够取出来的 小于等于M的最大金额。 若将M理解为背包重量,而每种货币的面额理解成 value,货币的面额同样理解成重量,那么这个问题就是一个多重背包问题。 多重背包问题可以转换成完全

文西 2263

PICO+Unity MR空间网格开发实战:从零构建透视交互场景

本文详细介绍了使用PICO 4 Ultra与Unity引擎进行MR空间网格开发的完整实战流程。从环境准备、项目初始化、核心组件配置,到空间网格的生成、可视化与物理交互实现,手把手教你构建一个能识别真实环境并发射虚拟小球的透视交互场景。文章重点解析了PICO SDK中空间网格功能的关键配置与优化技巧,并提供了真机部署与调试的实用指南。

e3f4g5的博客 490

C#量化交易的市场数据采集与分析:从0到1构建你的‘数据雷达’——对比5大神器,代码实战全解析!

数据是量化交易的‘氧气’,而好的数据采集与分析是你的‘呼吸机’!本文5大神器+代码实战,总有一款适合你!(本文代码均经过测试,但请根据实际需求调整!

java专栏 730

poj1276 Cash Machine(多重背包变形)

传送门: Cash Machine 题意:给你争取要获得的钱数, 货币种类和每种货币的数量, 求在不超过给的钱数的情况下,获得尽量多的钱。 思路:一开始我是用常规动态规划的思路来写这道题的,定义dp[i][j]:用第i种货币所能支付的不超过j的钱数的最大钱数。但在实际写的过程中因为担心空间超限,所以我把它降为一维了。状态转移方程为:dp[j]=max(dp[j],dp[j-k✖dk[i]]+k✖dk[i]); 可这种写法是会超时的,时间复杂度为O(nk✖N✖cash),即10的9次方。我们要想办法优化它

qw991的博客 874

(第14天)Veritas NetBackup(NBU) 配合 RMAN 脚本备份 Oracle 数据库

NBU(Veritas Netbackup)是企业中比较常用的备份软件,通常用于备份企业使用中的系统,数据库,文件等等,通常是搭配 Oracle RMAN 来进行备份 Oracle 数据库。

LuciferLiu_DBA 1642

多重背包的两种解决方法(poj1276)

多重背包是指装入背包的若干见物品种,可以拿的数量有上限。 第一种就是二进制优化,转换成01背包,例如一件物品可以拿26件,每一件的权值是w,26可以写成(1+2+4+8)+11,所以就把这种物品分解成权重为w,2 w,4w,8w,11w的五种物品,这五种物品组合,一定能组成小于等于26的任意一个数,这样就把有26件的一种物品换成了五种各有一件的物品,用01背包就能解决了。 poj

emmmmmm 2741

SA8295P QNX主机上电启动流程分析

嵌入式系统的启动过程对于确保系统正常运行至关重要。本文将详细分析SA8295P芯片在QNX主机上的上电启动流程,并提供相应的源代码示例。嵌入式系统的启动过程对于确保系统正常运行至关重要。本文将详细分析SA8295P芯片在QNX主机上的上电启动流程,并提供相应的源代码示例。以下是一个SA8295P QNX主机上电启动流程分析。

CodeMaven的博客 1230

STM32CubeMX配置FSMC驱动外部SRAM全流程详解

FSMC(灵活静态存储控制器)是STM32实现高速并行外部存储扩展的核心外设,其本质是将片外SRAM地址空间映射为CPU可直接读写的内存区域。原理上依赖于精确的时序寄存器配置(如BTRx)、GPIO复用功能启用及AHB3总线时钟同步机制,技术价值在于免DMA搬运的零拷贝访问能力与确定性低延迟。典型应用场景包括工业HMI缓存、医疗设备实时数据采集、嵌入式图形加速等对带宽和确定性要求严苛的系统。本文围绕STM32F407ZG平台,结合STM32CubeMX工程化配置、硬件信号完整性设计与HAL库底层映射逻辑,系

weixin_34718952的博客 232

POJ1267 Cash machine 多重背包

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

WA是一笔财富 319

SOMEIP协议--第三节[SOMEIP/SD](SD格式)

Entry和Option是SD报文中最重要的内容,是服务发现功能的承载。7.Minor Version是指当前服务的小版本号,可以配置,用于给用户区分当前服务的版本用的。注意:stop offer,stop subscribe,Subscribe Nack 这三条的TTL一定设为0,唯一和offer,subscribe,Subscribe的区别。Option用来辅助Entry实现其功能,是Entry携带的附加信息,作用上一般是用来告诉对端自己业务的ip和port信息,方便对端通过ip和port发送报文。

qq_41816308的博客 1338

POJ1276 Cash Machine 【解法二】

单调队列优化多重背包

YHaX的博客 525

Python 爬虫实战:知网论文数据爬取(题录 / 摘要 / 引用提取 + Excel 结构化)

在学术研究中,获取准确且全面的文献数据是至关重要的。知网(CNKI)作为国内最大的学术文献数据库,拥有海量的论文资源。通过 Python 爬虫技术,我们可以高效地从知网爬取论文数据,包括题录、摘要和引用信息,并将这些数据结构化存储到 Excel 文件中,方便后续的学术分析和研究。

yansideyucsdn的博客 1748

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

POJ1276 Cash Machine(多重背包问题)

三种解法:多重背包转化为完全背包和01背包;多重背包通过二进制化化为01背包;通过计数法优化为2重循环。 code1:         47MS  #include #include #define Max(a,b) (a) >(b)?(a):(b) int dp[100005], Cost[11], Count[11], cash; void ZeroOnePack(i

天亮说晚安 2234

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

多重背包练习-计数法-POJ-1276-Cash Machine

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

我是传奇 2120

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain&#39;s apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = &quot;R. B. Palm&quot;, title = &quot;Prediction as a candidate for learning deep hierarchical models of data&quot;, year = &quot;2012&quot;,}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath(&#39;DeepLearnToolbox&#39;));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand(&#39;state&#39;,0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W&#39;); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand(&#39;state&#39;,0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = &#39;sigm&#39;;%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er &lt; 0.10, &#39;Too big error&#39;);```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand(&#39;state&#39;,0)sae = saesetup([784 100]);sae.ae{1}.activation_function = &#39;sigm&#39;;sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)&#39;)% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;;nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er &lt; 0.16, &#39;Too big error&#39;);```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x&#39;,28,28,60000))/255;test_x = double(reshape(test_x&#39;,28,28,10000))/255;train_y = double(train_y&#39;);test_y = double(test_y&#39;);%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you&#39;ll get around 1.2% errorrand(&#39;state&#39;,0)cnn.layers = { struct(&#39;type&#39;, &#39;i&#39;) %input layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 6, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %sub sampling layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 12, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er&lt;0.12, &#39;Too big error&#39;);```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er &lt; 0.08, &#39;Too big error&#39;);%% ex2 neural net with L2 weight decayrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er &lt; 0.1, &#39;Too big error&#39;);%% ex3 neural net with dropoutrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er &lt; 0.1, &#39;Too big error&#39;);%% ex4 neural net with sigmoid activation functionrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er &lt; 0.1, &#39;Too big error&#39;);%% ex5 plotting functionalityrand(&#39;state&#39;,0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = &#39;softmax&#39;; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er &lt; 0.1, &#39;Too big error&#39;);%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand(&#39;state&#39;,0)nn = nnsetup([784 20 10]); nn.output = &#39;softmax&#39;; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er &lt; 0.1, &#39;Too big error&#39;);```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free &quot;Bitdeli Badge&quot;)

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

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

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

jerans的博客 360

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

Cash Machine&amp;#13; &amp;#13; &amp;#13; &amp;#13; &amp;#13; Time Limit: 1000MS&amp;#13;  &amp;#13; Memory Limit: 10000K&amp;#13; &amp;#13; &amp;#13; Total Submissions: 38986&amp;#13;  &amp;#13; Accepted: 14186&amp;#13; &amp;#13; &amp;#13; &amp;#1

蔡军帅 414

poj 1276 Cash Machine

题目大意:给定一个数m,还有一组数,分别表示这个元素的个数num[i], 单个价值为value[i]。要你求最这组元素价值和最接近m的最大值 解题思路:多重背包问题,拆分2进制或单调队列 同poj 1014一样 http://blog.youkuaiyun.com/xiaoxiaoluo/article/details/7816378 2进制拆分方法: #include #include #in

涛涛不解 628

poj1276 Cash Machine

题意: 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 第一行的意思是给一个735大小的背包 然后3表示后面有3种物品,4和125表示,有个物体大小为125,价值为125,有4个 多重背包求解,交的时候忘了注释freopen,狂WA。。二进制解法如下。

zhangwei1120112119的专栏 452

poj-1276 Cash Machine(多重背包)

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

TUT我好菜啊 417

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...

c___c18的博客 313

POJ1276 Cash Machine

Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %lld &amp; %llu Description A Bank plans to install a machine for cash withdrawal. The machine is able to de...

weixin_34125592的博客 84
上一篇: 如何计算Farey序列的第n阶的全部项? UVa 10408 Farey sequences
下一篇: HDU 2686 Matrix (双线程DP)
synapse7
博客等级 码龄14年 432粉丝 701原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值