Piggy-Bank
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2940 Accepted Submission(s): 1452
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.
此题就是最简单的完全背包,顺序!!!
for i=1..N
for v=0..V
f[v]=max{f[v],f[v-cost]+weight}
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114
代码:
#include <iostream>
#include <stdio.h>
#include <memory.h>
using namespace std;
const int INF = 99999999;
int c[50005], w[10005];
int bag[10005];
int N, V, Z;
void _com_bag() //完全背包:顺序!
{
int i, j;
for(i = 0; i <= Z; i++) bag[i] = INF;
bag[0] = 0;
for(i = 0; i < N; i++)
{
for(j = c[i]; j <= Z; j++)
{
bag[j] = min(bag[j], bag[j-c[i]] + w[i]);
}
}
}
int main()
{
int i, t, E, F;
scanf("%d", &t);
while(t--)
{
scanf("%d %d", &E, &F);
Z = F - E;
scanf("%d", &N);
for(i = 0; i < N; i++)
scanf("%d %d", &w[i], &c[i]);
_com_bag();
if(bag[Z] != INF)
printf("The minimum amount of money in the piggy-bank is %d.\n", bag[Z]);
else
printf("This is impossible.\n");
}
return 0;
}
分享到:
相关推荐
在本话题中,我们关注的是一个名为"HDU-EID-V2-核心板1"的特定设计,它涉及到STM32的核心组件、接口、电源管理和调试配置。 首先,核心板设计中提到了F103和F207两个型号的STM32芯片。F103和F207属于STM32的不同...
这个压缩包文件“HDU 2000-2099 解题报告”显然包含了在这个题号范围内的一些问题、答案以及解题思路,对于学习算法和提升编程能力具有很高的价值。 在这个范围内,我们可以预见到涵盖的算法类型可能包括但不限于:...
《2019-hdu-multi-(2019杭电多校第二场数据与标程).zip》是一个专门针对编程竞赛爱好者和ACM(国际大学生程序设计竞赛)参赛者的资源包。这个压缩文件的核心内容是2019年杭州电子科技大学(HDU)主办的多校联合...
【标题】"HDU-2000-2099.zip_hdu2000" 是一个包含杭电(Hangzhou Dianzi University)ACM竞赛题目解题报告的压缩包,覆盖了编号从2000到2099的题目。这个资源对于学习算法、提高编程技巧以及准备ACM/ICPC(国际大学...
HDU(杭州电子科技大学在线评测系统)是一个知名的编程竞赛平台,为编程爱好者提供了大量的算法题目进行练习和比赛。这个名为"HDU-2000-2099.rar_hdu"的压缩包包含了该平台从2000到2099共100道题目的源代码。这些...
1. **动态规划**:动态规划是一种用于解决最优化问题的常用方法,例如背包问题、最长公共子序列、矩阵链乘法等。解题报告会解释如何定义状态、设计状态转移方程,并给出具体代码实现。 2. **图论算法**:包括最短...
hdu-OS-simple-shell,Linux_的_Shell_命令窗口_demo_版实现_shell-demo
标题中的"HDU-1535-.zip_多源点"表明这是一个关于解决 ACM (国际大学生程序设计竞赛)问题的程序代码包,问题编号为 HDU 1535,且该问题涉及到多源点的最短路径计算。描述中提到的"求多源点到单终点的最短路(反向...
这些题目来自于杭州电子科技大学的在线评测系统HDU的题集,涵盖了从2000到2009的编号。这些题目旨在测试编程者的基本算法理解、数学计算能力以及问题解决技巧。以下是对这些题目中涉及知识点的详细解释: 1. **2000...
HDU-EID-V2-扩展板1是一款专为STM32微控制器设计的硬件扩展板,主要用于增强核心板的功能。该扩展板具有多种接口和功能,包括ADC(模数转换器)、电位器、DAC(数模转换器)输出以及TEST_V跳线,能够满足用户在模拟...
本资料"HDU Page 11 Answer"正是针对杭电OJ中第11页的题目,提供了一系列题解,旨在帮助初学者快速掌握基础算法,并提升编程能力。 一、ACM入门基础知识 ACM竞赛强调的是团队协作和快速解决问题的能力,其核心是...
2. **HDU Online Judge**:HDU OJ是杭州电子科技大学维护的一个在线编程竞赛平台,提供丰富的题目供参赛者练习和提交代码,支持多种编程语言,并有自动评分机制。 3. **解题策略**:报告中可能包含不同题目的解题...
从第 1 秒开始,每 一秒最 开始 时,每个数据服务器都会检查它是否收到了主服务器的回复信息(包括新发出信息的回复信息):如果还没收到任何回复信息,那么该服务器
示例 1:输出:[1,2,3,7,8,11,12,9,10,4,5,6]输入的多级列表如下图所示:扁平化后的链表如下图:示例 2:输出:[1,3,2]解释:输入
2016. 增量元素之间的最大差值题目描述:给你一个下标从 0 开始的整数数组 nums ,该数组的大小为 n ,请你计算 nums[j] - nums[i]
其中一类查询要求 更新 数组 nums 下标对应的值另一类查询要求返回数组 nums 中索引 left 和索引 right 之间( 包含 )的nums元素的 和
杭州电子科技大学ACM Steps中Chapter One-Section One的答案集。不要直接抄哦~~ 如需题解请上我的博客~ 博客地址呈上:http://blog.csdn.net/xu_zh