个人心得:多重背包,自己根据转换方程写总是TLE,后面去网上看了二进制转换,不太理解;

后面仔细想了下,用自己的思想理解下把,就是将对应number,cash总和用二进制拆分,

然后全部装入到一个数组,这样子就可以减少循环,同时转变为01背包,这样子想把,

5 5,就变成了5,10,20,5然后用01背包互相取与不取也可以组成对应的k=1-5乘以5的值。

转换方式如下:

 while(number[i]-k>)
{
t[account++]=k*cash[i];
number[i]-=k;
k=k*;
}
t[account++]=number[i]*cash[i];
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
int cash[],number[];
int dp[];
int t[];
int main(){
int ti;
while(cin>>ti){
int n;
cin>>n;
int account=;
for(int i=;i<=n;i++)
{
cin>>number[i]>>cash[i];
int k=;
if(number[i]==||cash[i]==)
continue;
while(number[i]-k>)
{
t[account++]=k*cash[i];
number[i]-=k;
k=k*;
}
t[account++]=number[i]*cash[i];
}
if(!n||!ti) {cout<<""<<endl;continue;}
memset(dp,,sizeof(dp));
for(int i=;i<account;i++)
{
for(int j=ti;j>=t[i];j--)
{
dp[j]=max(dp[j],dp[j-t[i]]+t[i]);
}
}
cout<<dp[ti]<<endl; }
return ;
}
Language:
Default
Cash Machine
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 36725   Accepted: 13316

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.

Cash Machine(多重背包二进制转换)的更多相关文章

  1. POJ-1276 Cash Machine 多重背包 二进制优化

    题目链接:https://cn.vjudge.net/problem/POJ-1276 题意 懒得写了自己去看好了,困了赶紧写完这个回宿舍睡觉,明早还要考试. 思路 多重背包的二进制优化. 思路是将n ...

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

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

  3. POJ 1276:Cash Machine 多重背包

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

  4. Poj 1276 Cash Machine 多重背包

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

  5. POJ1276 - Cash Machine(多重背包)

    题目大意 给定一个容量为M的背包以及n种物品,每种物品有一个体积和数量,要求你用这些物品尽量的装满背包 题解 就是多重背包~~~~用二进制优化了一下,就是把每种物品的数量cnt拆成由几个数组成,1,2 ...

  6. POJ1276:Cash Machine(多重背包)

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

  7. POJ1276:Cash Machine(多重背包)

    题目:http://poj.org/problem?id=1276 多重背包模板题,没什么好说的,但是必须利用二进制的思想来求,否则会超时,二进制的思想在之前的博客了有介绍,在这里就不多说了. #in ...

  8. Cash Machine(多重背包)

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

  9. PKU--1267 Cash Machine(多重背包)

    题目http://poj.org/problem?id=1276 分析 这是一个多重背包的问题,可以把请求的金额当作背包的重量,而货币的面值就是价值又是重量. 于是这个问题便很好理解背包了. #];; ...

随机推荐

  1. Cookie用法简介

    java操作Cookie---javax.servlet.http.Cookie 1.增加一个Cookie Cookie cookie = new Cookie("username" ...

  2. layer满屏/禁止最大化最小化 可以做选择框使用

    1.layer弹窗最大化 var index=layer.open(); layer.full(index); 2.layer禁止最大化最小化 layer.open( [ type:2, title: ...

  3. linux 音频驱动

    转:https://wenku.baidu.com/view/7394e16d7e21af45b307a8dc.html?pn=51 linux_sound_alsa_ALSA体系SOC子系统中数据流 ...

  4. 【鸟哥的Linux私房菜】笔记3

    正确地开机 最好不要使用root账号登陆!GNOME图形界面 View items as a list X WindowShell 文本交互界面bash是Shell的名称,Linux的默认壳程序就是b ...

  5. memcached 高级机制(一)

    memcached的高级机制 memcached内存机制 (1)我们知道操作系统对进程的处理方法,在多进程并发的操作系统中,程序的执行不可避免的会产生碎片.同样对于memcached,在存储value ...

  6. FTH: (7156): *** Fault tolerant heap shim applied to current process. This is usually due to previous crashes. ***

    这两天在Qtcreator上编译程序的时候莫名其妙的出现了FTH: (7156): *** Fault tolerant heap shim applied to current process. T ...

  7. hql join

    文章一: 1.用hql语句 ` String hql="select student.id, student.name ,class.name from student映射实体类名 as s ...

  8. Java -- 利用反射 操作任意数组,包括对象数组 和 基本数据类型的数组

    items为任意数组

  9. Java中interface是否继承Object类

    首先我们从C++说起, c++可以多继承.也就是一个类型 --- class,可以继承自2个以上的父类型.多继承导致一个问题,很多人知道.例如,如果类型B,类型C均继承自类型A.然后类型D继承自类型B ...

  10. IPC 进程间通信

    linux下进程间通信的几种主要手段简介: 管道(Pipe)及有名管道(named pipe):管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它 ...