题意:

给出一个存钱罐的重量和没存钱之前存钱罐的重量,然后给出几种硬币的重量和币值,计算存钱罐里至少有多少钱。

题目:

Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action comes from Irreversibly Bound Money (IBM). The idea behind is simple. Whenever some ACM member has any small money, he takes all the coins and throws them into a piggy-bank. You know that this process is irreversible, the coins cannot be removed without breaking the pig. After a sufficiently long time, there should be enough cash in the piggy-bank to pay everything that needs to be paid.

But there is a big problem with piggy-banks. It is not possible to determine how much money is inside. So we might break the pig into pieces only to find out that there is not enough money. Clearly, we want to avoid this unpleasant situation. The only possibility is to weigh the piggy-bank and try to guess how many coins are inside. Assume that we are able to determine the weight of the pig exactly and that we know the weights of all coins of a given currency. Then there is some minimum amount of money in the piggy-bank that we can guarantee. Your task is to find out this worst case and determine the minimum amount of cash inside the piggy-bank. We need your help. No more prematurely broken pigs!

Input

The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing two integers E and F. They indicate the weight of an empty pig and of the pig filled with coins. Both weights are given in grams. No pig will weigh more than 10 kg, that means 1 <= E <= F <= 10000. On the second line of each test case, there is an integer number N (1 <= N <= 500) that gives the number of various coins used in the given currency. Following this are exactly N lines, each specifying one coin type. These lines contain two integers each, Pand W (1 <= P <= 50000, 1 <= W <=10000). P is the value of the coin in monetary units, W is it’s weight in grams.

Output

Print exactly one line of output for each test case. The line must contain the sentence “The minimum amount of money in the piggy-bank is X.” where X is the minimum amount of money that can be achieved using coins with the given total weight. If the weight cannot be reached exactly, print a line “This is impossible.”.

Sample Input

3
10 110
2
1 1
30 50
10 110
2
1 1
50 30
1 6
2
10 3
20 4

Sample Output

The minimum amount of money in the piggy-bank is 60.
The minimum amount of money in the piggy-bank is 100.
This is impossible.

分析:

(1)、关于恰好装满问题
是否恰好装满的解法不同只在于初始值的不同
恰好装满:1、求最大值时,除了dp[0] 为0,其他都初始化为无穷小 -inf;2、求最小值时,除了dp[0] 为0,其他都初始化为无穷大inf(求最小值时原状态为inf【状态为不存在此种情况】+一个数一定大于inf,此时选择小值为inf,状态不变);
不必恰好装满: 全初始化为0.
(2)首先满的猪的重量减去空的猪的重量这样就可以获得能装多少硬币的重量。然后用完全背包,看是否能恰好装满且价值最小。
这里推荐一篇写的好的博文写挺好的,就是太监了的一篇博客,狗头保命

AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int M=5e2+10;
int t,a,b,n;
int x[M],y[M],dp[10010];
int main()
{scanf("%d",&t);while(t--){memset(dp,inf,sizeof(dp));scanf("%d%d%d",&a,&b,&n);dp[0]=0;//恰好装满,只有背包容量为0时满足,其他初始化为无穷表示未被定义int ma=b-a;for(int i=1; i<=n; i++)scanf("%d%d",&x[i],&y[i]);for(int i=1; i<=n; i++)for(int j=y[i]; j<=ma; j++)//正序,放入第i物品,之前的状态需要进行改变,故需要正序dp[j]=min(dp[j],dp[j-y[i]]+x[i]);if(dp[ma]==inf)printf("This is impossible.\n");elseprintf("The minimum amount of money in the piggy-bank is %d.\n",dp[ma]);}return 0;
}

Piggy-Bank POJ - 1384(完全背包+背包放满)相关推荐

  1. 动态规划 背包问题小结 0-1背包(采药 九度第101题) 完全背包(Piggy-Bank POJ 1384) 多重背包(珍惜现在,感恩生活 九度第103题)

    本小结介绍0-1背包.完全背包以及多重背包问题 记忆要点: 0-1背包:二维数组情况下,顺序遍历体积或者倒序均可以                降维情况下需倒序遍历体积 完全背包:数组降维+顺序遍历 ...

  2. 背包问题_(DP经典),一,(01背包,填满背包)

    背包系列已更新 一,(01背包,填满背包) 二,(多重背包) 三,(完全背包) 目录 一,01背包(对于一个物品,你要么全拿,要么不拿) 核心dp方程,dp[i][j]=max(dp[i-1][j], ...

  3. POJ 1384 Piggy-Bank 背包DP

    所谓的全然背包,就是说物品没有限制数量的. 怎么起个这么intimidating(吓人)的名字? 事实上和一般01背包没多少差别,只是数量能够无穷大,那么就能够利用一个物品累加到总容量结尾就能够了. ...

  4. piggy bank 完全背包

    题目描述 Before ACM can do anything, a budget must be prepared and the necessary financial support obtai ...

  5. poj 1384 完全背包

    记住这个公式就OK了.for i=1..N for v=w[i]..V f[v]=max{f[v],f[v-w[i]]+v[i]}.这样就转换成为了0/1背包问题是一样的,只不过第二个循环的顺序不一样 ...

  6. poj 2063 Investmen 完全背包

    这个题的想法不难,两个点: 1 是完全背包 2 是考虑/1000,降低复杂度 但是提交的时候反复的wa,最后找问题原来是dp开小了,可是dp本来开1005,后来开到100030过了.哎,如果没有时间计 ...

  7. POJ - 2392 朴素多重背包 + 贪心 WA与AC代码细节分析

    我们先来看下普通的朴素多重背包(拆成01背包求解) n种物品,背包大小w,每种物品重量 wi,价值 vi,个数 ci dp[j] 表示 大小为 j 的背包含有的最大价值,即 物品重量和 小于等于 j ...

  8. poj 2923(状态压缩+背包)

    比较巧妙的一道题目,拿到题目就想用暴力直接搜索,仔细分析了下发现复杂度达到了2^n*n! ,明显不行,于是只好往背包上想. 于是又想二分找次数判断可行的方法,但是发现复杂度10^8还是很悬... 然后 ...

  9. DP专题 4 | 骨头收集爱好者 - POJ 1458( 0-1背包)

    背包问题是DP里面变化比较多的问题,可以参考网上的<背包9讲>,另外还是阅读<算竞入门>和<算竞进阶>,讲的最全的肯定是背包9讲,基本上把所有变形都讲了一遍,但是把 ...

最新文章

  1. 你和你的好友,正在免费帮微信训练神经网络
  2. python nlp文本摘要_理解文本摘要并用python创建你自己的摘要器
  3. php curl ob start,curl - php中开启缓冲压缩 ob_start('ob_gzhandler') 之后是在什么时候开始的压缩?...
  4. 01-密码学基础-前言
  5. 简单地发布EJB程序的过程
  6. 如何使用PyTorch的量化功能?
  7. protobuf的安装和使用
  8. mysql outfile csv_sql-MySQL导出到outfile:CSV转义字符
  9. Mysql学习总结(6)——MySql之ALTER命令用法详细解读
  10. 计算机维修 主板 打印机,一台电脑带电拨打印机接口 ,烧坏主板,不能开机
  11. CentOS 禁用Ctrl+Alt+Del重启功能
  12. Spring.net 容器注入是替换(后处理器appConfigPropertyHolder)
  13. 80个超详细的Python入门实例
  14. 50道经典计算机网络面试题梳理
  15. 自己动手编译nodemcu固件(ESP8266)
  16. Windows用户名中文修改英文无感实现操作方法
  17. DIY_红外计数模块
  18. 6.5趣味逻辑之委派任务
  19. Android 4.4 kitkat以上及以下根据uri获取路径的方法
  20. Android 开源1:获取并解析网页信息(Jsoup)

热门文章

  1. Android之解决webview加载第三方网页点击弹不出下拉框(html页面里面的select标签)
  2. linux之通过grep使用or、and、not操作
  3. linux之通过tail命令动态跟踪日志文件里面的末尾信息
  4. React Native之(var和let区别 )(简单解构)(map对象遍历)(可变顺序参数和不可以变顺序参数函数)
  5. linux之man命令用法入门
  6. mysql怎么改字体编码_mysql怎么改字符编码?
  7. python链接mysql报错2003_Python连接Mysql报错问题解决
  8. 这6个地方不去简直太可惜!
  9. 每日一笑 | 今天是植树节,我想在你心里种点逼树
  10. 弹性式分布数据集RDD——Pyspark基础 (二)