Coins

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

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
 
一看题,以为普普通通一道模版题,结果卡时间卡到哭。。。
 
注意:当val[i]*num[i]>m时,使用完全背包更快,最开始每晚i注意到这点,通通使用二进制优化,结果超时超哭,然后发现完全背包比转化为二进制优化的01背包更快。
 
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std; int val[],num[];
int dp[],m; int main()
{
int n;
while(~scanf("%d%d",&n,&m)&&(n+m))
{
memset(dp,,sizeof(dp));
//dp[0]=1;
int i;
for(i=; i<n; i++)
scanf("%d",&val[i]);
for(i=; i<n; i++)
scanf("%d",&num[i]);
for( i=; i<n; i++)
{
int j;
if(val[i]*num[i]>=m)
{
for( j=val[i];j<=m;j++)
dp[j]=max(dp[j],dp[j-val[i]]+val[i]);
continue;
}
int tmp=;
while(num[i]>=tmp)
{
for( j=m;j>=val[i]*tmp;j--)
dp[j]=max(dp[j],dp[j-val[i]*tmp]+val[i]*tmp);
num[i]-=tmp;
tmp=tmp<<;
}
for( j=m;j>=val[i]*num[i];j--)
dp[j]=max(dp[j],dp[j-val[i]*num[i]]+val[i]*num[i]);
}
int cnt=;
for(i=; i<=m; i++)
if(dp[i]==i)
cnt++;
printf("%d\n",cnt);
}
return ;
}

HDU_2844_(多重背包)的更多相关文章

  1. 洛谷P1782 旅行商的背包[多重背包]

    题目描述 小S坚信任何问题都可以在多项式时间内解决,于是他准备亲自去当一回旅行商.在出发之前,他购进了一些物品.这些物品共有n种,第i种体积为Vi,价值为Wi,共有Di件.他的背包体积是C.怎样装才能 ...

  2. HDU 2082 找单词 (多重背包)

    题意:假设有x1个字母A, x2个字母B,..... x26个字母Z,同时假设字母A的价值为1,字母B的价值为2,..... 字母Z的价值为26.那么,对于给定的字母,可以找到多少价值<=50的 ...

  3. Poj 1276 Cash Machine 多重背包

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26172   Accepted: 9238 Des ...

  4. poj 1276 Cash Machine(多重背包)

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33444   Accepted: 12106 De ...

  5. (混合背包 多重背包+完全背包)The Fewest Coins (poj 3260)

    http://poj.org/problem?id=3260   Description Farmer John has gone to town to buy some farm supplies. ...

  6. (多重背包+记录路径)Charlie's Change (poj 1787)

    http://poj.org/problem?id=1787   描述 Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie dri ...

  7. 单调队列优化DP,多重背包

    单调队列优化DP:http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列优化多重背包:http://blog.csdn ...

  8. POJ1742 Coins[多重背包可行性]

    Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 34814   Accepted: 11828 Descripti ...

  9. POJ1276Cash Machine[多重背包可行性]

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32971   Accepted: 11950 De ...

随机推荐

  1. C#.NET 无法直接启动带有类库输出类型的项目怎么办

    我把Driver.cs文件去掉了一行注释,发现报错   右击这个解决方案,选择属性,然后再启动项目中改成MySample    

  2. 查看yarn当前执行任务列表

    Author: kwu 查看yarn当前执行任务列表.可使用例如以下命令查看: yarn application -list 如需杀死当前某个作业,使用kill application-id的命令例如 ...

  3. hbase shell经常使用命令

    hbase经常使用命令 /usr/local/cloud/hbase/bin/hbase shell 用shell来连接hbase exit 退出hbase shell version 查看hbase ...

  4. jquery中怎样防止冒泡事件

    jquery中怎样防止冒泡事件 1.利用event.stopPropagation() 2.利用return false 3.利用event.preventDefault()

  5. poj 3263 Tallest Cow(线段树)

    Language: Default Tallest Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1964   Ac ...

  6. Python爬虫【第3篇】【多线程】

    一.多线程 Python标准库提供2个模块,thread是低级模块,threading是高级模块 1.threading模块创建多线程 方式1:把1个函数传入并创建Thread实例,然后调用start ...

  7. 6个变态的C语言Hello World程序 之 雷人的程序语言

    以下的六个程序片段主要完毕这些事情: 输出Hello, World 混乱C语言的源码 以下的全部程序都能够在GCC下编译通过,仅仅有最后一个须要动用C++的编译器g++才干编程通过. hello1.c ...

  8. android MediaRecorder start failed:-38【转】

    本文转载自:http://blog.csdn.net/fnuwfnh/article/details/46698509 最近在学习android 录音方面的知识,发现在部分手机正常运行的APP,在华为 ...

  9. shell脚本自动更新git

    gitpull.sh #!/bin/bash cd /home/wwwroot/default/mouse && git pull cd /home/wwwroot/default/s ...

  10. JFreeChart自我总结

    想飞就别怕摔 大爷的并TM骂人 JFreeChart自我总结 1.饼图.柱状图.折线图生成的工具类   1 package com.text.util;  2   3 import java.awt. ...