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这么多的情况下,,你可以用你的硬 ...
随机推荐
- 探寻C++最快的读取文件的方案 ——C++ IO优化
在竞赛中,遇到大数据时,往往读文件成了程序运行速度的瓶颈,需要更快的读取方式.相信几乎所有的C++学习者都在cin机器缓慢的速度上栽过跟头,于是从此以后发誓不用cin读数据.还有人说Pascal的re ...
- Spark Streaming:大规模流式数据处理的新贵(转)
原文链接:Spark Streaming:大规模流式数据处理的新贵 摘要:Spark Streaming是大规模流式数据处理的新贵,将流式计算分解成一系列短小的批处理作业.本文阐释了Spark Str ...
- 大端和小端(Big endian and Little endian)
一.大端和小端的问题 对于整型.长整型等数据类型,Big endian 认为第一个字节是最高位字节(按照从低地址到高地址的顺序存放数据的高位字节到低位字节):而 Little endian 则相反,它 ...
- 实现微信小程序的3rd_session
function 3rd_session($len) { $fp = @fopen('/dev/urandom','rb'); $result = ''; if ($fp !== FALSE) { $ ...
- Android倒计时案例展示
1. Handler 与Message方法实现倒计时功能 关于Handler与Message消息机制的原理可查看:Android--Handler使用应运及消息机制处理原理分析 这个设计思路也是最经常 ...
- leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法
Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...
- Shodan:黑客的物联网搜索引擎
记得看过一个电影.里面的科学家开发了一个超级系统,能够实时监控全部可用摄像头.让逃犯无处遁形. Shodan这个新型的搜索引擎可能会让这个想法变成现实. 和Google这些传统互联网信息搜索引擎不同. ...
- Xcode5 打包 发布配置
http://www.cnblogs.com/zhaoqingqing/p/3553750.html 主题 Unity导出Xcode项目,使用Xocde打包ipa并提交到AppStore 步骤 1.设 ...
- Android SDK 更新和下载慢怎么办?
博客搬家:因为各种原因,我如今的博客将首发于blog.mojijs.com, 能够百度搜索 "姜哥的墨迹技术博客" , 或者 点击这里 本文地址 http://blog.mojij ...
- Field 'id' doesn't have a default value问题解决方法
Field 'id' doesn't have a default value问题解决方法 突然想温习温习对数据库的读写.于是就用mysql建了一张单独的表(见代码1),用Hibernate写了个应用 ...