题目大意

给定一个容量为M的背包以及n种物品,每种物品有一个体积和数量,要求你用这些物品尽量的装满背包

题解

就是多重背包~~~~用二进制优化了一下,就是把每种物品的数量cnt拆成由几个数组成,1,2,4,~~~cnt-2^K+1,k满足cnt-2^K+1>0的最大整数,体积和价值乘上相应的数就是相应物品的价值和体积,这样用这些物品能够表示1~~cnt所有的情况~~~这就转化成01背包了~~~

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
#define MAXN 100005
int dp[MAXN];
int value[],cnt[];
int cash,n;
void CompletePack(int c,int w)
{
for(int i=c; i<=cash; i++)
dp[i]=max(dp[i],dp[i-c]+w);
}
void ZeroOnePack(int c,int w)
{
for(int i=cash; i>=c; i--)
dp[i]=max(dp[i],dp[i-c]+w);
}
void MultiplePack(int c,int w,int m)
{
if(c*m>=cash)
{
CompletePack(c,w);
return;
}
int k=;
while(k<m)
{
ZeroOnePack(k*c,k*w);
m-=k;
k*=;
}
if(m>) ZeroOnePack(c*m,w*m);
}
int main()
{ while(scanf("%d",&cash)!=EOF)
{
scanf("%d",&n);
for(int i=; i<=n; i++) scanf("%d%d",&cnt[i],&value[i]);
memset(dp,,sizeof(dp));
for(int i=; i<=n; i++)
MultiplePack(value[i],value[i],cnt[i]);
printf("%d\n",dp[cash]);
}
return ;
}

POJ1276 - Cash Machine(多重背包)的更多相关文章

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

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

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

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

  3. POJ1276: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 多重背包

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

  5. POJ 1276:Cash Machine 多重背包

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

  6. Cash Machine(多重背包)

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

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

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

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

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

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

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

随机推荐

  1. Python数据库连接池实例——PooledDB

    不用连接池的MySQL连接方法 import MySQLdbconn= MySQLdb.connect(host='localhost',user='root',passwd='pwd',db='my ...

  2. Educational Codeforces Round 7 F - The Sum of the k-th Powers 拉格朗日插值

    The Sum of the k-th Powers There are well-known formulas: , , . Also mathematicians found similar fo ...

  3. 2014年度辛星html教程夏季版第七节

    经过前面六节的学习,我们大致清楚了HTML教程中的基础内容,那么接下来我们开始继续向后推进,可以说,下面我们介绍一下HTML中的区块. ***************区块*************** ...

  4. C# - 中断模式下的调试

    1. 设置断点 选中需要设置断点的行,右键选择断点插入断点,此行左侧显示红色圆形标志.或者F9 有几个条件断点类型: a. 条件断点 b. 命中次数,大于,几倍于,大于等于你设置的断点次数此时中断 c ...

  5. uitableviewcell 自适应大小 参考

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {     ...

  6. CSS3学习之 transform 属性

    CSS3 transform是什么? transform的含义是:改变,使…变形:转换 CSS3 transform都有哪些常用属性? transform的属性包括:rotate() / skew() ...

  7. Spring 数据源配置二:多数据源

    通过上一节  Spring 数据源配置一: 单一数据源  我们了解单一数据源的配置, 这里我们继续多个数据源的配置 如下(applicationContent.xml 内容) 一:  Spring   ...

  8. 49. 面向对象的LotusScript(十五)之Log4Dom下

    Log4Dom是模仿Log4J的思想建立的.Log4J能够向多种记录媒介以统一的格式写入各种级别的日志信息(包括错误.调试和信息等),还可以籍配置文件在运行时方便地修改记入日志的级别.Log4Dom提 ...

  9. Case swapping

    Case swapping Description: Given a string, swap the case for each of the letters. e.g. CodEwArs --&g ...

  10. BZOJ1334: [Baltic2008]Elect

    1334: [Baltic2008]Elect Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 386  Solved: 201[Submit][Sta ...