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 分析 这是一个多重背包的问题,可以把请求的金额当作背包的重量,而货币的面值就是价值又是重量. 于是这个问题便很好理解背包了. #];; ...
随机推荐
- 仿Facebook登录表单
在线演示 本地下载
- NoSQL数据库memcache和redis区别
在web后台发开面试中,经常会被问道memcache和redis的区别和使用情况. 其中memcache和redis都是基于内存存储的缓存系统,存储形式key--value键值对的形式. 区别: 1. ...
- C++逗号表达式
c++中,逗号表达式的结果是最右边表达式的值
- phpMyAdmin中config.inc.php设置密码和修改密码的方法
phpMyAdmin有3种授权模式: 1. cookie: 显示一个web登录页面,输入mysql的用户名和密码,然后进入管理界面. $cfg['Servers'][$i]['auth_type'] ...
- 【P2774】方格取数问题(贪心+最大流,洛谷)
首先,我们要读懂这道题,否则你会和我一开始产生一样的疑问,把所有的数都取走剩下一个最小的不就可以了么???然后我们发现样例完全不是这么回事.题目中所说的使相邻的两个数没有公共边,是指你去走的数,也就是 ...
- 【arc101】比赛记录
这场还好切出了D,rt应该能涨,然而这场的题有点毒瘤,700分的D没多少人切,更别说EF了.(暴打出题人)既然这样,干脆就水一篇博客,做个简单的比赛记录. C - Candles 这题是一道一眼题,花 ...
- CentOS 7 安装 docker-compose
compose是用来在docker中定义和运行复杂应用的小工具,比如在一个文件中定义多个容器,只用一行命令就可以让一切就绪并运行. 安装pip: 这里显示,找不到相应的包?? 说没有python-pi ...
- ZooKeeper-znode概念与使用
可以将ZK看作一个具有高可用性特征的文件系统.这个文件系统中没有文件和目录,而是统一使用节点(znode)的概念,称为znode.znode既可以作为保存数据的容器(如同文件),也可以作为保存其他zn ...
- HIVE 2.1.0 安装教程。(数据源mysql)
前期工作 安装JDK 安装Hadoop 安装MySQL 安装Hive 下载Hive安装包 可以从 Apache 其中一个镜像站点中下载最新稳定版的 Hive, apache-hive-2.1.0-bi ...
- NOI2013
Bless All 其实已经没有什么遗憾了呢 下一篇就是OI 再见吧2333