Cash Machine

Time Limit: 1000MS

Memory Limit: 10000K

Total Submissions: 24132

Accepted: 8446

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
 
题意, 给你最大的钱数 Case ,和 nk 种不同的面值, 每种面值D1...DN 对应的数量 n1....nN, 找出不超过最大钱数Case
 
思路:多重背包
 
import java.io.*;
import java.util.*;
/*
*
* author : deng_hui_long
* Date : 2013-8-31
*
*/
public class Main {
int Case,n;
int dp[]=new int[1000000];
public static void main(String[] args) {
new Main().work();
}
void work(){
Scanner sc=new Scanner(new BufferedInputStream(System.in));
while(sc.hasNext()){
Case=sc.nextInt();
n=sc.nextInt();
if(n==0&&Case==0)
break;
Node node[]=new Node[n];
Arrays.fill(dp,0);
for(int i=0;i<n;i++){
int a=sc.nextInt();
int b=sc.nextInt();
node[i]=new Node(a,b);
}
for(int i=0;i<n;i++){
multiplePack(node[i].val,node[i].val,node[i].cost);
}
System.out.println(dp[Case]);
}
}
//多重背包
void multiplePack(int cost,int weight,int amount){
if(cost*amount>=Case)//超过最大的钱数,按完全背包处理
completePack(cost,weight);
else{//小于最大的钱数,按01背包处理
int k=1;
while(k<amount){
zeroOnePack(k*cost,k*weight);
amount-=k;
k<<=1;//左移一位,表示乘以2
}
zeroOnePack(amount*cost,amount*weight);
}
}
//完全背包
void completePack(int cost,int weight){
for(int i=cost;i<=Case;i++){
dp[i]=Math.max(dp[i],dp[i-cost]+weight);
}
}
//01背包
void zeroOnePack(int cost,int weight){
for(int i=Case;i>=cost;i--)
dp[i]=Math.max(dp[i],dp[i-cost]+weight);
}
class Node{
int cost;
int val;
Node(int cost,int val){
this.cost=cost;
this.val=val;
}
}
}

POJ 1276  Cash Machine(多重背包)的更多相关文章

  1. POJ 1276 Cash Machine(多重背包的二进制优化)

    题目网址:http://poj.org/problem?id=1276 思路: 很明显是多重背包,把总金额看作是背包的容量. 刚开始是想把单个金额当做一个物品,用三层循环来 转换成01背包来做.T了… ...

  2. Poj 1276 Cash Machine 多重背包

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26172   Accepted: 9238 Des ...

  3. [poj 1276] Cash Machine 多重背包及优化

    Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver ap ...

  4. poj 1276 Cash Machine_多重背包

    题意:略 多重背包 #include <iostream> #include<cstring> #include<cstdio> using namespace s ...

  5. 【转载】poj 1276 Cash Machine 【凑钱数的问题】【枚举思路 或者 多重背包解决】

    转载地址:http://m.blog.csdn.net/blog/u010489766/9229011 题目链接:http://poj.org/problem?id=1276 题意:机器里面共有n种面 ...

  6. poj 1276 Cash Machine(多重背包)

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33444   Accepted: 12106 De ...

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

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 38986   Accepted: 14186 De ...

  8. POJ 1276:Cash Machine 多重背包

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30006   Accepted: 10811 De ...

  9. Cash Machine (POJ 1276)(多重背包——二进制优化)

    链接:POJ - 1276 题意:给你一个最大金额m,现在有n种类型的纸票,这些纸票的个数各不相同,问能够用这些纸票再不超过m的前提下凑成最大的金额是多少? 题解:写了01背包直接暴力,结果T了,时间 ...

  10. Cash Machine(多重背包)

    http://poj.org/problem?id=1276 #include <stdio.h> #include <string.h> ; #define Max(a,b) ...

随机推荐

  1. Delphi中使用GDI+进行绘图(2)

    2)使用IGDIPlus接口 (1)下载安装所需软件 可以在以下地址下载IGDI+最新的安装程序. http://www.mitov.com/products/igdi+ www.igdiplus.o ...

  2. js传真实地址 C:\fakepath

    js给action传真是地址的时候,处于安全,传到action中 浏览器会改变路径变为C:\fakepath\ftp.txt,但是原始路径却是 C:\Documents and Settings\Ad ...

  3. HttpSession具体解释

    session的机制 http是无状态的协议,客户每次读取web页面时,server都打开新的会话,并且server也不会自己主动维护客户的上下文信息,那么要怎么才干实现会话跟踪呢?session就是 ...

  4. HTTP POST请求的Apache Rewrite规则设置

    最近自测后端模块时有个业务需求需要利用WebServer(我用的是Apache)将HTTP POST请求转发至后端C模块,后端处理后返回2进制加密数据.http post请求的url格式为:     ...

  5. - Shell - sort处理大文件(页 1) - ChinaUnix.net

    - Shell - sort处理大文件(页 1) - ChinaUnix.net sort处理大文件

  6. 黑马程序员:Java基础总结----类加载器

    黑马程序员:Java基础总结 类加载器   ASP.Net+Android+IO开发 . .Net培训 .期待与您交流! 类加载器 Java虚拟机中可以安装多个类加载器,系统默认三个主要类加载器,每个 ...

  7. TR90眼镜_百度百科

    TR90眼镜_百度百科 TR90眼镜

  8. projecteuler----&gt;problem=9----Special Pythagorean triplet

    title: A Pythagorean triplet is a set of three natural numbers, a b c, for which, a2 + b2 = c2 For e ...

  9. ExtJS4.2学习(7)——基础知识之Reader&Writer篇

    Reader: 主要用于将proxy数据代理读取的数据按照不同的规则进行解析,将解析好的数据保存到Modle中. 结构图 Ext.data.reader.Reader 读取器的根类(很少直接实例化这个 ...

  10. Linux从用户层到内核层系列 - GNU系列之glibc介绍

    题记:本系列文章的目的是抛开书本从源代码和使用的角度分析Linux内核和相关源代码,byhankswang和你一起玩转linux开发 轻松搞定TCP/IP协议栈,原创文章欢迎交流, byhankswa ...