Cash Machine(多重背包二进制转换)
个人心得:多重背包,自己根据转换方程写总是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
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: 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 Sample Output 735 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(多重背包二进制转换)的更多相关文章
- POJ-1276 Cash Machine 多重背包 二进制优化
题目链接:https://cn.vjudge.net/problem/POJ-1276 题意 懒得写了自己去看好了,困了赶紧写完这个回宿舍睡觉,明早还要考试. 思路 多重背包的二进制优化. 思路是将n ...
- POJ 1276 Cash Machine(多重背包的二进制优化)
题目网址:http://poj.org/problem?id=1276 思路: 很明显是多重背包,把总金额看作是背包的容量. 刚开始是想把单个金额当做一个物品,用三层循环来 转换成01背包来做.T了… ...
- POJ 1276:Cash Machine 多重背包
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 30006 Accepted: 10811 De ...
- Poj 1276 Cash Machine 多重背包
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26172 Accepted: 9238 Des ...
- POJ1276 - Cash Machine(多重背包)
题目大意 给定一个容量为M的背包以及n种物品,每种物品有一个体积和数量,要求你用这些物品尽量的装满背包 题解 就是多重背包~~~~用二进制优化了一下,就是把每种物品的数量cnt拆成由几个数组成,1,2 ...
- POJ1276:Cash Machine(多重背包)
Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver ap ...
- POJ1276:Cash Machine(多重背包)
题目:http://poj.org/problem?id=1276 多重背包模板题,没什么好说的,但是必须利用二进制的思想来求,否则会超时,二进制的思想在之前的博客了有介绍,在这里就不多说了. #in ...
- Cash Machine(多重背包)
http://poj.org/problem?id=1276 #include <stdio.h> #include <string.h> ; #define Max(a,b) ...
- PKU--1267 Cash Machine(多重背包)
题目http://poj.org/problem?id=1276 分析 这是一个多重背包的问题,可以把请求的金额当作背包的重量,而货币的面值就是价值又是重量. 于是这个问题便很好理解背包了. #];; ...
随机推荐
- ZOJ 3961 Let's Chat 【水】
题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3961 题意 给出两个人的发消息的记录,然后 如果有两人在连续M天 ...
- Django---media静态文件的配置&全局变量
media 静态文件配置 static 静态文件多用于存放用于渲染前端页面的相关数据,media用于存放客户上传或其他的文件 setting.py 中加入路径 MEDIA_ROOT = ( os.pa ...
- 生产&消费者模型
import queue,threading,time ,random q = queue.Queue() def producer(): count = 1 while count <11: ...
- mongoDB中批量修改字段
// 为每一个文章文档新增一个image_count字段,用于记录此文章包含的图片个数 db['test.articles'].find({'title':'wfc test'}).forEach( ...
- HBase-存储-概览
概览 HBase主要处理两种文件:一种是预写日志(Write-Ahead Log,WAL),另一种是实际的数据文件.这两种文件主要由HRegionServer管理.在某些情况下,HMaster也可以进 ...
- JavaScript -- 广告随鼠标移动, 点击一次后关闭
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Dom4j quick start guide
Parsing XML Using Iterators Powerful Navigation with XPath Fast Looping Creating a new XML document ...
- ubuntu/centos printk 终端中不能打印信息及解决办法
今天用ubuntu来调试信息,printk死活打印不出信息,即使把级别跳到<0>,即KERN_ALERT也不行,后再搜了好长时间网络, 这个地址:http://bbs.chinaunix. ...
- QT 实现按住鼠标左键点击对话框空白处,拖动对话框
定义头文件 QPoint move_point; //移动的距离 bool mouse_press; //按下鼠标左键 protected: void mousePressEvent( ...
- java八大基本类型介绍
//今天说一下java的八大基本类型: // 数字类型:byte(8位).short(16位).int(32位).long(64位) //浮点类型:float(32位).double(64位) //字 ...