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这么多的情况下,,你可以用你的硬 ...
随机推荐
- Object 转换为 BigDecimal
项目中遇到读取Excel文件里面的数据转为金额的情况,为了程序更加的健壮,进行处理如下: import java.math.BigDecimal; import java.math.BigIntege ...
- Istio在Openshift 3.11的安装
详细安装步骤及解释参考 https://docs.openshift.com/container-platform/3.11/servicemesh-install/servicemesh-insta ...
- 数学图形(2.7)sphere sine wave
在球上以SIN曲线的轨迹游走. #http://www.mathcurve.com/courbes3d/couronnetangentoidale/couronnetangentoidale.shtm ...
- c++学习之多态(虚函数和纯虚函数)
c++是面向对象语言,面向对象有个重要特点,就是继承和多态.继承之前学过了,就是一种重用类的设计方式.原有的类叫父类,或者基类,继承父类的类叫子类.在设计模式中,我们总是要避免继承,推荐用组合.因为继 ...
- 如何修改Git commit的信息
原文地址: http://xiguada.org/change-git-commit-message Git cimmit信息push后,如何修改,amend可以修改最后一次commit信息,但对 ...
- [Algorithm] Array production problem
Given an array of integers, return a new array such that each element at index i of the new array is ...
- 微信公众帐号开发教程第4篇-----开发模式启用及接口配置Java
欢迎加入群:347245650 345531810 进行讨论相互交流 我的微信号:572839485 我的微信公众账号 我的微社区欢迎关注 索取源码←请点击 图床:没有服务器 拖拽图片 外网即 ...
- 即将来到: CSS Feature Queries (CSS特性查询)
Feature Queries 是CSS3 Conditional Rules specification中的一部分,它支持“@supports”规则,“@supports”规则可以用来测试浏览器是否 ...
- 论文笔记《Feedforward semantic segmentation with zoom-out features》
[论文信息] <Feedforward semantic segmentation with zoom-out features> CVPR 2015 superpixel-level,f ...
- angularjs中的$state.go
路由是这么定义的: $stateProvider .state('page1', { url: '/page1', templateUrl: 'views/page1.htm', controller ...