HDU2844 Coins 多重背包
Coins
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12330 Accepted Submission(s): 4922
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
题意:Tony想要买一个东西,他只有n中硬币每种硬币的面值为a[i]每种硬币的数量为c[i]要买的物品价值不超过m
输入:第一行输入n和m,第二行输入n个硬币的面值和n个硬币的数量,输入0 0结束
输出:1到m之间有多少价格Tony可以支付
#include <iostream>
#include <cstring>
#define INF 0x3f3f3f3f
using namespace std;
int f[],a[],c[];
int n,m; //m背包的总容量、v物品的体积、w物品的价值
void OneZeroPack(int m,int v,int w) //0-1背包
{
for(int i=m;i>=v;i--)
f[i]=max(f[i],f[i-v]+w);
} //m背包的总容量、v物品的体积、w物品的价值
void CompletePack(int m,int v,int w) //完全背包
{
for(int i=v;i<=m;i++)
f[i]=max(f[i],f[i-v]+w);
} //m背包的总容量、v物品的体积、w物品的价值、num物品的数量
void MultiplePack(int m,int v,int w,int num)//多重背包
{
if(v*num>=m)
{
CompletePack(m,v,w);
return ;
}
int k=;
for(k=;k<=num;k<<=)
{
OneZeroPack(m,k*v,k*w);
num=num-k;
}
if(num)
OneZeroPack(m,num*v,num*w);
} int main()
{
while(cin>>n>>m)
{
if(n==&&m==) break;
for(int i=;i<n;i++)
cin>>a[i];
for(int i=;i<n;i++)
cin>>c[i];
for(int i=;i<=m;i++) f[i]=-INF;
f[]=;
for(int i=;i<n;i++)
{
MultiplePack(m,a[i],a[i],c[i]);
}
int sum=;
for(int i=;i<=m;i++)
if(f[i]>) sum++;
cout<<sum<<endl;
}
return ;
}
HDU2844 Coins 多重背包的更多相关文章
- HDU-2844 Coins(多重背包)
Problem Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. On ...
- HDU-2844 Coins 多重背包 物品数量二进制优化
题目链接:https://cn.vjudge.net/problem/HDU-2844 题意 给你一些不同价值和一定数量n的硬币. 求用这些硬币可以组合成价值在[1 , m]之间的有多少. 思路 多重 ...
- hdu2844 Coins -----多重背包+二进制优化
题目意思:给出你n种硬币的面额和数量,询问它能够组合成1~m元中的几种情况. 这题如果直接按照完全背包来写的话,会因为每一种硬币的数目1 ≤ Ci ≤ 1000而超时,所以这里需要运用二进制优化来解决 ...
- POJ 3260 The Fewest Coins(多重背包+全然背包)
POJ 3260 The Fewest Coins(多重背包+全然背包) http://poj.org/problem?id=3260 题意: John要去买价值为m的商品. 如今的货币系统有n种货币 ...
- POJ1742 Coins[多重背包可行性]
Coins Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 34814 Accepted: 11828 Descripti ...
- POJ3260——The Fewest Coins(多重背包+完全背包)
The Fewest Coins DescriptionFarmer John has gone to town to buy some farm supplies. Being a very eff ...
- POJ 1742 Coins(多重背包, 单调队列)
Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...
- HDu -2844 Coins多重背包
这道题是典型的多重背包的题目,也是最基础的多重背包的题目 题目大意:给定n和m, 其中n为有多少中钱币, m为背包的容量,让你求出在1 - m 之间有多少种价钱的组合,由于这道题价值和重量相等,所以就 ...
- hdu2844(多重背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2844 题意:一位同学想要买手表,他有n种硬币,每种硬币已知有num[i]个.已知手表的价钱最多m元,问 ...
随机推荐
- 如何解决nagios安装及运行在的常见错误?(转)
一.安装nrpe的时候,提示:checking for SSL headers… configure: error: Cannot find ssl headers 解决方法如下: yum -y in ...
- 转:http2基本中文翻译
https://github.com/fex-team/http2-spec/blob/master/HTTP2%E4%B8%AD%E8%8B%B1%E5%AF%B9%E7%85%A7%E7%89%8 ...
- HDU 4287-Intelligent IME(哈希)
Intelligent IME Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- docker入门——安装及简单操作
和安装其他软件一样,安装Docker也需要一些基本的前提条件.Docker要求的条件具体如下: 运行64位CPU构架的计算机(目前只能是x86_64和amd64),Docker目前不支持32位CPU. ...
- LoadRunner测试AJAX
什么是AJAX? Ajax, shorthand for Asynchronous JavaScript and XML, is a web development technique for cre ...
- 【Python3 爬虫】15_Fiddler抓包分析
我们要抓取一些网页源码看不到的信息,例如:淘宝的评论等 我们可以使用工具Fiddler进行抓取 软件下载地址:https://pan.baidu.com/s/1nPKPwrdfXM62LlTZsoiD ...
- jrebel license server 激活方法
方法1: 使用已经封装好的jar包,保持一直运行即可(放到服务器上). 链接:https://pan.baidu.com/s/1rrn-6F26JpD5RSsbJV3-hQ 密码: dscu 服务器上 ...
- 《windows核心编程》 18章 堆
堆的优缺点: 优点:让我们专心解决手头问题,不必理会分配粒度和页边界这类事情. 缺点:分配和释放内存块的速度比其他方式慢,而且也无法对物理存储器的调拨和撤销进行直接控制. 什么是堆: 堆就是一块预订的 ...
- EChart-Timeline
timeline-day.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...
- 关于行内元素之间有空隙的问题,例如span与input之间
问题如图: 想要的是下面的效果,而却出现上面的效果,解决方法如下: 对于行元素span或者input来说 很多人会用inline-block来显示他们,但是往往发现 中间会留一段小空隙 , 其实这个 ...