Coins

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

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
该题卡多重背包,数组开的不够大WA,开的太大MLE。
下面是用多重背包做该题的思路。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int A[],C[];
int n,m;
int dp[];
int w[];
int used[];
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
if(!n&&!m)
break;
memset(dp,,sizeof(dp));
memset(used,,sizeof(used));
for(int i=;i<n;i++) scanf("%d",&A[i]);
for(int i=;i<n;i++) scanf("%d",&C[i]); int cnt=;
for(int i=;i<n;i++)
{
int e=;
while(e<C[i])
{
w[cnt++]=e*A[i];
C[i]-=e;
e<<=;
}
if(C[i]>)
w[cnt++]=C[i]*A[i];
} for(int i=;i<cnt;i++)
for(int j=m;j>=w[i];j--)
dp[j]=max(dp[j],dp[j-w[i]]+w[i]); int res=;
for(int i=m;i>=;i--)
if(!used[dp[i]])
{
used[dp[i]]=;
res++;
}
printf("%d\n",res);
} return ;
}
 
下面转化为多重部分和问题求解。
 
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int A[],C[];
int n,m;
int dp[];
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
if(!n&&!m)
break;
memset(dp,-,sizeof(dp));
for(int i=;i<n;i++) scanf("%d",&A[i]);
for(int i=;i<n;i++) scanf("%d",&C[i]);
dp[]=;
for(int i=;i<n;i++)
for(int j=;j<=m;j++)
if(dp[j]>=)
dp[j]=C[i];
else if(j<A[i]||dp[j-A[i]]<)
dp[j]=-;
else
dp[j]=dp[j-A[i]]-;
int res=;
for(int i=;i<=m;i++)
if(dp[i]>=)
res++;
printf("%d\n",res);
} return ;
}

最新文章

  1. AngularJS(1)
  2. 前端-SEO
  3. 【图像处理】【SEED-VPM】4.串口调试信息
  4. Jenkins用HTTP Request Plugin插件进行网站的监控(运维监控)
  5. Windows7下面exe寄宿WCF:Http无法注册URL{0} ,进程不具有此命名空间的访问权限问题
  6. 百度VS高德:LBS开发平台ios SDK对比评测
  7. gearman安装及初次使用
  8. 数往知来C#面向对象〈三〉
  9. 001.android初级篇之ToolBar
  10. PowerShell 中的目录文件管理
  11. Handling Captcha | Webdriver
  12. 编译TensorFlow源码
  13. 20162321王彪-实验二-Java面向对象程序设计
  14. C#中Windows窗体工具栏
  15. Docker Online - Web Lab
  16. 1.单件模式(Singleton Pattern)
  17. MATLAB中floor、round、ceil、fix区别
  18. Delphi动态添加菜单
  19. F - 回转寿司 (权值线段树)
  20. 【刷题】LOJ 6122 「网络流 24 题」航空路线问题

热门文章

  1. 百科知识 scm文件如何打开
  2. Odoo(OpenERP)开发实践:通过XML-RPC接口訪问Odoo数据库
  3. 在kubernetes 集群运行 odoo
  4. iOS GCD使用
  5. HAProxy简单使用
  6. 谈谈加载(Loading)的那点事
  7. 【BZOJ3052】[wc2013]糖果公园 带修改的树上莫队
  8. 关于Darwin接入私有协议、私有SDK码流的讨论
  9. Win10升级.NET Framework 3.5或2.0遇到错误0x800f081f
  10. Create a /etc/yum.repos.d/mongodb-org-4.0.repo