ps:原来用新浪,可是代码的排版不是很好,所以用博客园啦,先容许我把从八月份开始的代码搬过来,从这里重新出发,希望这里可以一直见证我的成长。

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12337    Accepted Submission(s): 4925

Problem Description
Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One day Hibix opened purse and found there were some coins. He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without change) and he known the price would not more than m.But he didn't know the exact price of the watch.

You are to write a program which reads n,m,A1,A2,A3...An and C1,C2,C3...Cn corresponding to the number of Tony's coins of value A1,A2,A3...An then calculate how many prices(form 1 to m) Tony can pay use these coins.

 
Input
The input contains several test cases. The first line of each test case contains two integers n(1 ≤ n ≤ 100),m(m ≤ 100000).The second line contains 2n integers, denoting A1,A2,A3...An,C1,C2,C3...Cn (1 ≤ Ai ≤ 100000,1 ≤ Ci ≤ 1000). The last test case is followed by two zeros.
 
Output
For each test case output the answer on a single line.
 
Sample Input
3 10
1 2 4 2 1 1
2 5
1 4 2 1
0 0
 
Sample Output
8 4
#include<iostream>
#include<string.h>
using namespace std;
const int N=100005;
int a[N],b[N],dp[N];
int i,j;
int max(int a,int b)
{
return a<b?b:a;
} void onepack(int c,int w,int m)
{
for(j=m;j>=c;j--)
dp[j]=max(dp[j],dp[j-c]+w);
}
void allpack(int c,int w,int m)
{
for(j=c;j<=m;j++)
{
dp[j]=max(dp[j],dp[j-c]+w);
}
} void mulpack(int c,int w,int m,int n)
{
if(c*n>=m) allpack(c,w,m);
else
{
int k=1;
while(k<n)//二进制优化,下面有具体说明
{
onepack(k*c,k*w,m);
n-=k;
k*=2;
}
onepack(n*c,n*w,m); }
} int main()
{
int n,m; while(cin>>n>>m,n||m)
{
for(i=1;i<=n;i++)
cin>>a[i];
for(i=1;i<=n;i++)
cin>>b[i];
memset(dp,0,sizeof(dp));
for(i=1;i<=n;i++)
{
mulpack(a[i],a[i],m,b[i]);
}
int cnt=0;
for(i=1;i<=m;i++)
if(dp[i]==i) cnt++;
cout<<cnt<<endl;
}
return 0;
}

  

下面是找到一个叫老杨的人对二进制优化的理解:
 

这是一个多重背包的模板,也是十分好用的一种模板,因为这个比直接拆除01 背包来做

要省些时间。这是为啥呢,首先先由我讲一下为什么能换成01 背包吧。

举个栗子吧。 假如给了我们 价值为 2,但是数量却是10 的物品,我们应该把10给拆开,要知道二进制可是能够表示任何数的,所以10 就是可以有1,2, 4,8之内的数把它组成,一开始我们选上 1了,然后让10-1=9,再选上2,9-2=7,在选上 4,7-4=3,

而这时的3<8了,所以我们就是可以得出 10由 1,2,4,3,来组成,就是这个数量为1,2,3,4的物品了,那么他们的价值是什么呢,是2,4,6,8,也就说给我们的价值为2,数量是10的这批货物,已经转化成了价值分别是2,4,6,8元的货物了,每种只有一件哎!!!!这就是二进制优化的思想。

那为什么会有完全背包和01 背包的不同使用加判断呢?原因也很简单啊,当数据很大,大于背包的容纳量时,我们就是在这个物品中取上几件就是了,取得量时不知道的,也就理解为无限的啦,这就是完全背包啦,反而小于容纳量的就是转化为01背包来处理就是了,可以大量的省时间。

coins_多重背包的更多相关文章

  1. poj 1742 coins_多重背包

    题意:给你N个种硬币,价值和数量,知道手表不大于m,问能组成(1~m)的价格有多少种情况 套套上次那题的模板直接就行了,http://blog.csdn.net/neng18/article/deta ...

  2. 洛谷P1782 旅行商的背包[多重背包]

    题目描述 小S坚信任何问题都可以在多项式时间内解决,于是他准备亲自去当一回旅行商.在出发之前,他购进了一些物品.这些物品共有n种,第i种体积为Vi,价值为Wi,共有Di件.他的背包体积是C.怎样装才能 ...

  3. HDU 2082 找单词 (多重背包)

    题意:假设有x1个字母A, x2个字母B,..... x26个字母Z,同时假设字母A的价值为1,字母B的价值为2,..... 字母Z的价值为26.那么,对于给定的字母,可以找到多少价值<=50的 ...

  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: 33444   Accepted: 12106 De ...

  6. (混合背包 多重背包+完全背包)The Fewest Coins (poj 3260)

    http://poj.org/problem?id=3260   Description Farmer John has gone to town to buy some farm supplies. ...

  7. (多重背包+记录路径)Charlie's Change (poj 1787)

    http://poj.org/problem?id=1787   描述 Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie dri ...

  8. 单调队列优化DP,多重背包

    单调队列优化DP:http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列优化多重背包:http://blog.csdn ...

  9. POJ1742 Coins[多重背包可行性]

    Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 34814   Accepted: 11828 Descripti ...

随机推荐

  1. 为大家分享一个 Ajax Loading —— spin.js(转)

    原文地址:http://www.cnblogs.com/lxblog/p/3425599.html 我们在做Ajax 异步请求的时候,一般都会利用一个动态的 Gif 小图片来制作一个Ajax Load ...

  2. override和overload的区别

    override(重写,覆盖) 1.方法名.参数.返回值相同. 2.子类方法不能缩小父类方法的访问权限. 3.子类方法不能抛出比父类方法更多的异常(但子类方法可以不抛出异常). 4.存在于父类和子类之 ...

  3. (转载整理)SAP ERP常用T-CODE

    其实最讨厌做ERP的项目了.不过,身不由己的嘛! 网上资料加一些整理. 与客户相关  VD01 建立客户 Create customerVD02 更改客户 Change customerVD03 显示 ...

  4. 在列表页,按照指定的category取所属的post列表

    在某些指定的页面,例如news,blog等页面,需要列出指定某种类型的文章列表,这种情况下,可以先添加两个category,分别命名news,blog,然后再分别添加几个post,指定每个post所属 ...

  5. uva 11624(bfs)

    11624 - Fire! Time limit: 1.000 seconds Joe works in a maze. Unfortunately, portions of the maze hav ...

  6. 手动实现ArrayList

    public interface List { public void insert(int i,Object obj)throws Exception; public void delete(int ...

  7. double int char 数据类型

    贴心的limits... 测试代码: #include <iostream> #include <stdio.h> #include <limits> #inclu ...

  8. 使用MediaRecorder录制音频

    手机一般都提供了麦克风硬件,而Android系统就可以利用该硬件来录制音频了. 为了在Android应用中录制音频,Android提供了MediaRecorder类,使用MediaRecorder录制 ...

  9. 使用MediaPlayer播放音频-----之一

    MediaPlayer提供了如下方法来指定装载相应的音频文件: 1.void  setDataSource( String  path):指定装载path路径所代表的文件. 2.void  setDa ...

  10. POJ 1573 Robot Motion 模拟 难度:0

    #define ONLINE_JUDGE #include<cstdio> #include <cstring> #include <algorithm> usin ...