POJ 1276 Cash Machine -- 动态规划(背包问题)
题目地址:http://poj.org/problem?id=1276
Description
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
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
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
#include <stdio.h>
#include <string.h> #define MAX 100001
#define MAXN 11
#define Max(a, b) (a) > (b) ? (a) : (b) int cash;
int num[MAXN];
int deno[MAXN];
int dp[MAX]; void ZeroOnePack (int deno){
int i;
for (i=cash; i>=deno; --i)
dp[i] = Max(dp[i], dp[i-deno] + deno);
} void CompletePack (int deno){
int i;
for (i=deno; i<=cash; ++i)
dp[i] = Max(dp[i], dp[i-deno] + deno);
} void MultiplePack (int deno, int num){
if (deno * num >= cash)
CompletePack (deno);
else{
int k = 1;
while (k < num){
ZeroOnePack (deno * k);
num -= k;
k *= 2;
}
ZeroOnePack (deno * num);
}
} int main(void){
int N;
int i; while (scanf ("%d%d", &cash, &N) != EOF){
for (i=1; i<=N; ++i){
scanf ("%d%d", &num[i], &deno[i]);
}
memset (dp, 0, sizeof(dp));
for (i=1; i<=N; ++i){
MultiplePack (deno[i], num[i]);
} printf ("%d\n", dp[cash]);
} return 0;
}
参考资料:背包问题九讲
POJ 1276 Cash Machine -- 动态规划(背包问题)的更多相关文章
- Poj 1276 Cash Machine 多重背包
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26172 Accepted: 9238 Des ...
- POJ 1276 Cash Machine(单调队列优化多重背包)
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 38986 Accepted: 14186 De ...
- poj 1276 Cash Machine(多重背包)
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 33444 Accepted: 12106 De ...
- POJ 1276 Cash Machine
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24213 Accepted: 8476 Descrip ...
- 【转载】poj 1276 Cash Machine 【凑钱数的问题】【枚举思路 或者 多重背包解决】
转载地址:http://m.blog.csdn.net/blog/u010489766/9229011 题目链接:http://poj.org/problem?id=1276 题意:机器里面共有n种面 ...
- [poj 1276] Cash Machine 多重背包及优化
Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver ap ...
- POJ 1276 Cash Machine(多重背包的二进制优化)
题目网址:http://poj.org/problem?id=1276 思路: 很明显是多重背包,把总金额看作是背包的容量. 刚开始是想把单个金额当做一个物品,用三层循环来 转换成01背包来做.T了… ...
- POJ 1276 Cash Machine 【DP】
多重背包的模型,但一开始直接将N个物品一个一个拆,拆成01背包竟然T了!!好吧OI过后多久没看过背包问题了,翻出背包九讲看下才发现还有二进制优化一说........就是将n个物品拆成系数:1,2,4, ...
- POJ 1276 Cash Machine(完全背包模板题)
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44409 Accepted: 16184 Description A B ...
随机推荐
- 利用putty软件连接虚拟机中linux操作系统
http://jingyan.baidu.com/article/9c69d48fbefe6613c8024e6a.html 大家在使用虚拟的过程中有时候会感觉切换操作系统很不方便,那么有什么方法可以 ...
- [C语言(VC)] 打造自己的键盘记录器 (zaroty)
说起键盘记录,想必很多朋友都用过网上流传的一些键盘记录软件吧,但是有没有想过自己写一个呢?也许你会想:会不会很复杂啊?我可以很负责的告诉你,写键盘记录是很简单的.你所需要的仅仅是懂得一些C语言的DLL ...
- ios基础知识
1获取系统语言设置 NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults]; NSArray *languages = ...
- [React Native] Build a Separator UI component
In this lesson we'll create a reusable React Native separator component which manages it's own style ...
- [Python]linux自己定义Python脚本命令
在window下写好的程序配置到Linux上,要实现随意文件夹下的命令调用. 因为初学Linux,这里从文件传输等最主要的方法入手,记录配置的过程中遇到的各种问题. 连接远端server 这里使用pu ...
- eclipse+ADT 进行android应用签名详解
http://jojol-zhou.iteye.com/blog/719428 1.Eclipse工程中右键工程,弹出选项中选择 android工具-生成签名应用包: 2.选择需要打包的android ...
- Android Sqlite 导入CSV文件 .
http://blog.csdn.net/johnnycode/article/details/7413111 今天遇到 Oracle 导出的12万条CSV格式数据导入 Android Sqlite ...
- Android利用Looper在子线程中改变UI
MainActivity如下: package cn.testlooper; import android.app.Activity; import android.os.Bundle; import ...
- iOS开发中图片方向的获取与更改
iOS开发中 再用到照片的时候 或多或少遇到过这样的问题 就是我想用的照片有横着拍的有竖着排的 所以导致我选取图片后的效果也横七竖八的 显示效果不好 比如: 图中红圈选中的图片选取的是横着拍 ...
- axel源码学习(0)——程序逻辑
axel简介 axel是一个命令行下的轻量级http/ftp 下载加速工具,支持多线程下载和断点续传,支持从多个镜像下载同一文件. axel的用法如下: 图 0.1 axel usage axel 粗 ...