HDU 2844 Coin 多重背包
Coins
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6279 Accepted Submission(s): 2561
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 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.
#include<stdio.h>
#include<string.h>
#include<algorithm>
int v[];
int w[];
int dp[];
using namespace std;
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==&&m==)
break;
memset(v,,sizeof(v));
memset(w,,sizeof(w));
for(int i=;i<=;i++)
dp[i]=-;
dp[]=;
for(int i=; i<=n; i++)
scanf("%d",&v[i]);
for(int i=; i<=n; i++)
scanf("%d",&w[i]);
for(int i=; i<=n; i++)
{
if(v[i]*w[i]>=m)//完全背包
{
for(int j=v[i]; j<=m; j++)
{
dp[j]=max(dp[j],dp[j-v[i]]+v[i]);
}
}
else
{
for(int k=; k<=w[i]; k=k*)
{
for(int j=m; j>=v[i]*k; j--)
{
dp[j]=max(dp[j],dp[j-v[i]*k]+v[i]*k);
}
w[i]-=k;
}
if(w[i]>)
{
for(int j=m; j>=v[i]*w[i]; j--)
dp[j]=max(dp[j],dp[j-v[i]*w[i]]+v[i]*w[i]);
}
}
}
int count=;
for(int i=; i<=m; i++)
{
if(dp[i]>=)
count++;
}
printf("%d\n",count);
}
return ;
}
AC代码二:
#include<iostream>
#include<string.h>
using namespace std; int a[],c[],F[]; void inline ZeroOnePack(int ResVal,int ResVol,int BpCap)
{
for(int i=BpCap;i>=ResVol;--i)
{
F[i]=max(F[i],F[i-ResVol]+ResVal);
}
} void inline CompletePack(int ResVal,int ResVol,int BpCap)
{
for(int i=ResVol;i<=BpCap;++i)
{
F[i]=max(F[i],F[i-ResVol]+ResVal);
}
} void MultiplePack(int ResVal,int ResVol,int ResNum,int BpCap)
{
if(ResVol*ResNum>=BpCap)
{ CompletePack(ResVal,ResVol,BpCap); }
for(int i=;(<<i)<=ResNum;++i)
{
ZeroOnePack((ResVal<<i),(ResVol<<i),BpCap);
ResNum-=(<<i);
}
if(ResNum) { ZeroOnePack(ResVal*ResNum,ResVol*ResNum,BpCap); }
} int main()
{
int i,j,n,m;
while(cin>>n>>m)
{
if(n+m==)
break;
memset(F,,sizeof(F));
for(i=;i<n;i++)
cin>>a[i];
for(j=;j<n;j++)
cin>>c[j];
for(i=;i<n;i++)
{
MultiplePack(a[i],a[i],c[i],m) ;
}
int num=;
for(i=;i<=m;i++)
{
if(F[i]==i)
num++;
}
cout<<num<<endl;
}
return ;
}
HDU 2844 Coin 多重背包的更多相关文章
- hdu 2844 Coins (多重背包+二进制优化)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2844 思路:多重背包 , dp[i] ,容量为i的背包最多能凑到多少容量,如果dp[i] = i,那么代表 ...
- HDu -2844 Coins多重背包
这道题是典型的多重背包的题目,也是最基础的多重背包的题目 题目大意:给定n和m, 其中n为有多少中钱币, m为背包的容量,让你求出在1 - m 之间有多少种价钱的组合,由于这道题价值和重量相等,所以就 ...
- HDU - 2844 Coins(多重背包+完全背包)
题意 给n个币的价值和其数量,问能组合成\(1-m\)中多少个不同的值. 分析 对\(c[i]*a[i]>=m\)的币,相当于完全背包:\(c[i]*a[i]<m\)的币则是多重背包,考虑 ...
- HDU 2844 Coins (多重背包计数 空间换时间)
Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- hdu 2844 coins(多重背包 二进制拆分法)
Problem Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. On ...
- hdu 2844 Coins 多重背包(模板) *
Coins Time Limit: 2000/1 ...
- hdu 1963 Investment 多重背包
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1963 //多重背包 #include <cstdio> #include <cstr ...
- 杭电1171 Big Event in HDU(母函数+多重背包解法)
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- hdu2844 & poj1742 Coin ---多重背包--两种方法
意甲冠军:你有N种硬币,每个价格值A[i],每个号码C[i],要求. 在不超过M如果是,我们用这些硬币,有多少种付款的情况下,.那是,:1,2,3,4,5,....,M这么多的情况下,,你可以用你的硬 ...
随机推荐
- WCF 自承载 提供源码
一.WCF 简单介绍 Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,是一套通讯接口.现在比较流行的SOA就可以通过WCF实现. ...
- 解决 PowerDesigner 错误 The generation has been cancelled because errors have been found by the check model.
在通过概念数据模型生成为物理数据模型时出现错误“The generation has been cancelled because errors have been found by the chec ...
- Hadoop中Partition深度解析
本文地址:http://www.cnblogs.com/archimedes/p/hadoop-partitioner.html,转载请注明源地址. 旧版 API 的 Partitioner 解析 P ...
- (转)Apache的安装与配置
转自:http://www.dreamdu.com/webbuild/apache/ 5.2. Apache的安装与配置 5.2.1. Apache安装与配置视频教程 下面将介绍如何在WinXP下安装 ...
- C# 网络斗地主源码开源
C# 网络斗地主源码开源多线程 讨论交流及 下载地址 可以发送聊天消息
- T-SQL 之 控制流语句
控制流语句也称为流程控制语句,是和高级编程语言中的类似功能一致的,引入控制流语句将使T-SQL代码有顺序执行转变为按控制执行. 批处理:一个批处理段是由一个或者多个语句组成的一个批处理,之所以叫批处理 ...
- GDB高级使用方法
1.设置环境变量 用户可以在GDB的调试环境中定义自己需要的变量,用来保存一些调试程序中的运行数据.要定义一个GDB的变量很简单,只需使用GDB的set命令. GDB的环境变量和Linux一样,也是以 ...
- Vijos P1002 过河 (NOIP提高组2005)
链接:https://www.vijos.org/p/1002 解析: 若 p*x+(p+1)*y=Q(採用跳跃距离p和p+1时能够跳至不论什么位置Q),则在Q ≥ P*(P-1)时是一定有解的. 因 ...
- react 调用项目中的 .html 文件
(1)将 html 文件 放于 public 文件夹下 (2)window.open('about:blank').location.href="http://localhost:3000/ ...
- org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'statisticalMapper' defined in file
::, [localhost-startStop-1] DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFact ...