Piggy-Bank

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23646    Accepted Submission(s): 11973

Problem Description

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.
 

Source

 
  1. //2017-04-04
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. const int inf = 0x3f3f3f3f;
  10. int dp[], p[], w[];//dp[i]表示重量为i时最少的钱
  11. //状态转移方程:dp[i] = min{dp[i-w[j]]+p[j] | 0<j<n}
  12.  
  13. int main()
  14. {
  15. int T, n, E, F, W;
  16. scanf("%d", &T);
  17. while(T--)
  18. {
  19. scanf("%d%d", &E, &F);
  20. W = F-E;
  21. memset(dp, inf, sizeof(dp));
  22. scanf("%d", &n);
  23. for(int i = ; i < n; i++){
  24. scanf("%d%d", &p[i], &w[i]);
  25. if(p[i] < dp[w[i]])//考虑两种硬币重量相等时,选价值小的
  26. dp[w[i]] = p[i];
  27. }
  28. for(int i = ; i <= W; i++){
  29. for(int j = ; j < n; j++){
  30. if(w[j] > i)continue;
  31. if(dp[i-w[j]]+p[j] < dp[i])
  32. dp[i] = dp[i-w[j]]+p[j];
  33. }
  34. }
  35. if(dp[W] == inf)printf("This is impossible.\n");
  36. else printf("The minimum amount of money in the piggy-bank is %d.\n", dp[W]);
  37. }
  38.  
  39. return ;
  40. }

HDU1114(KB12-F DP)的更多相关文章

  1. hdu1114 Piggy-Bank (DP基础 完全背包)

    链接:Piggy-Bank 大意:已知一只猪存钱罐空的时候的重量.现在的重量,已知若干种钱的重量和价值,猪里面装着若干钱若干份,求猪中的钱的价值最小值. 题解: DP,完全背包. g[j]表示组成重量 ...

  2. Educational Codeforces Round 58 (Rated for Div. 2) F dp + 优化(新坑) + 离线处理

    https://codeforces.com/contest/1101/problem/F 题意 有n个城市,m辆卡车,每辆卡车有起点\(s_i\),终点\(f_i\),每公里油耗\(c_i\),可加 ...

  3. Codeforces Round #543 (Div. 2) F dp + 二分 + 字符串哈希

    https://codeforces.com/contest/1121/problem/F 题意 给你一个有n(<=5000)个字符的串,有两种压缩字符的方法: 1. 压缩单一字符,代价为a 2 ...

  4. 【BZOJ4953】lydsy七月月赛 F DP

    [BZOJ4953]lydsy七月月赛 F 题面 题解:设f[i][j]表示第i个强度取为j时的最小误差.那么每次转移时,我们只计算j'和j之间的像素点带来的误差,于是有: $f[i][j]=min( ...

  5. hdu 4389 X mod f(x) 数位DP

    思路: 每次枚举数字和也就是取模的f(x),这样方便计算. 其他就是基本的数位Dp了. 代码如下: #include<iostream> #include<stdio.h> # ...

  6. HDU - 4389 X mod f(x)(数位dp)

    http://acm.hdu.edu.cn/showproblem.php?pid=4389 题意 为[A,B] 区间内的数能刚好被其位数和整除的数有多少个. 分析 典型的数位dp...比赛时想不出状 ...

  7. BZOJ 3400 [Usaco2009 Mar]Cow Frisbee Team 奶牛沙盘队:dp【和为f的倍数】

    题目链接:http://begin.lydsy.com/JudgeOnline/problem.php?id=1375 题意: 给你n个数,你可以从中选任意多个,但不能不选.问你所选数字之和为f的倍数 ...

  8. F. Wi-Fi(线段树实现dp)

    题:http://codeforces.com/contest/1216/problem/F dp[i][0]:表示第i个位置不装的最小代价 dp[i][1]:表示第i个位置装的最小代价 T1的线段树 ...

  9. UVALive 6908---Electric Bike(DP或记录型深搜)

    题目链接 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  10. coderforces #384 D Chloe and pleasant prizes(DP)

    Chloe and pleasant prizes time limit per test 2 seconds memory limit per test 256 megabytes input st ...

随机推荐

  1. 预防和避免死锁的方法及银行家算法的java简单实现

    预防死锁 (1) 摒弃"请求和保持"条件 基本思想:规定所有进程在开始运行之前,要么获得所需的所有资源,要么一个都不分配给它,直到所需资源全部满足才一次性分配给它. 优点:简单.易 ...

  2. vs2017配置pthread.h的方法

    一.背景(以下为走不通的配置方法!) 笔者最开始配置pthread.h,采用的是vs自动安装的方法,如图所示. 点击完“管理NuGet程序包”之后,弹出一个页面,如下,在“浏览”中输入pthread. ...

  3. 对于opencv全面貌的认识和理解

    1.opencv其实最开始只有源码,也就是sources中的代码,sources中有个modules,进入里面是各个我们平常使用的模块,如下图. 进入任意一个模块,比如calib3d,其中有inclu ...

  4. POJ 2612

    #include<iostream> #include<stdio.h> #include<algorithm> #define MAXN 11 using nam ...

  5. 域名直接访问应用程序-不加端口号&不加路径名

    当访问域名的时候,端口默认指定的是80: 而80端口在Linux系统下,1024之前的端口是只有root用户才能使用的, 因此将,系统层面将端口80 映射到8080, iptables -t nat ...

  6. C#集成FastDFS断点续传

    C#集成FastDFS断点续传 参考 .net版本FastDFS客户端v5.05. https://github.com/zhouyh362329/fastdfs.client.net FastDFS ...

  7. ASP.NET状态管理的总结

    阅读目录 开始 hidden-input QueryString Cookie ApplicationState ViewState,ControlState Session Profile 各种状态 ...

  8. wordpress中常用的一些php代码

    <?php the_author(); ?> 显示文章的作者 <?php the_author_description(); ?> 显示文章作者的描述(作者个人资料中的描述) ...

  9. Vue2.0-token权限处理

    token一种身份的验证,在大多数网站中,登录的时候都会携带token,去访问其他页面,token就想当于一种令牌.可以判断用户是否登录状态.本次页面是通过Element-ui搭建的登录界面 当用户登 ...

  10. 利用cygwin创建windows下的crontab定时任务

    要求 必备知识 熟悉基本编程环境搭建. 运行环境 windows 7(64位); Cygwin-1.7.35 下载地址 环境下载 什么是Cygwin Cygwin是一个在windows平台上运行的类U ...