poj1384:Piggy-Bank

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.题解完全背包(恰与不超过的初始化不同)
 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #define maxn 505
 5 #define inf 1<<29
 6 #define maxm 10005
 7 using namespace std;
 8 int T,m1,m2,m,n;
 9 int dp[maxm],v[maxn],val[maxn];
10 int main()
11 {
12     scanf("%d",&T);
13     while(T--)
14     {
15         scanf("%d%d",&m1,&m2);
16         m=m2-m1;
17         scanf("%d",&n);
18         for(int i=1 ; i<=n ; ++i)
19             scanf("%d%d",&val[i],&v[i]);
20         for(int i=1 ; i<=m ; ++i)dp[i]=inf;
21         dp[0]=0;
22         for(int i=1 ; i<=n ; ++i)
23             for(int j=v[i] ; j<=m ; ++j)
24                 dp[j]=min(dp[j],dp[j-v[i]]+val[i]);
25         if(dp[m]==inf)printf("This is impossible.\n");
26         else printf("The minimum amount of money in the piggy-bank is %d.\n",dp[m]);
27     }
28     return 0;
29 } 
时间: 2024-08-11 03:30:33

poj1384:Piggy-Bank的相关文章

程序员去美国工作:City Bank 国内外不同的花旗体验

本文为本博HoneB原创,未经允许不得转载. 八年经验 Eric - 弗罗里达州 老东家 Citi AVP头衔 回来啦!回来啦!回来啦!随便晃晃就已经五月过多半了 Orz,三月活动的纪要拖到现在,也是醉... 自觉去面个壁先.. 木办法,为了方便大家跟已经过去的小伙伴们进行更近距离的交流,新网站上线迫在眉睫,一切只能让让道了.额,出差月真不是盖的,兜兜转转,我都转晕了(>~<).所以想说的是之前若有怠慢,粗现留言回复不及时的情况,还请大家多多包涵. 本期嘉宾所在公司 第四期的活动准备得内容挺多

ACM Piggy Bank

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

SoC嵌入式软件架构设计II:否MMU的CPU虚拟内存管理的设计与实现方法

大多数的程序代码是必要的时,它可以被加载到内存中运行.手术后,可直接丢弃或覆盖其他代码.我们PC然在同一时间大量的应用,能够整个线性地址空间(除了部分留给操作系统或者预留它用),能够觉得每一个应用程序都独占了整个虚拟地址空间(字长是32的CPU是4G的虚拟地址空间),但我们的物理内存仅仅是1G或者2G.即多个应用程序在同一时候竞争使用这块物理内存.其必定会导致某个时刻仅仅存在程序的某个片段在运行,也即是全部程序代码和数据分时复用物理内存空间-这就是内存管理单元(MMU)工作核心作用所在. 处理器

SoC嵌入式软件架构设计之二:虚拟内存管理原理、MMU硬件设计及代码分块管理

程序的大部分代码都可以在必要的时候才加载到内存去执行,运行完后可以被直接丢弃或者被其他代码覆盖.我们PC上同时跑着很多的应用程序,每个应用程序使用的虚拟地址空间几乎可以整个线性地址空间(除了部分留给操作系统或者预留它用),可以认为每个应用程序都独占了整个虚拟地址空间(字长是32的CPU是4G的虚拟地址空间),但我们的物理内存只是1G或者2G.即多个应用程序在同时竞争使用这块物理内存,其必然会导致某个时刻只存在程序的某个片段在执行,也即是所有程序代码和数据分时复用物理内存空间—这就是内存管理单元(

BrandZ:2016年全球最具价值品牌百强榜(完整报告)

https://wppbaz.com/admin/uploads/files/BZ_Global_2016_Report.pdf Millward Brown编制的BrandZ最新排行榜(2016 BrandZ Top 100 Global Brands),全球100个最具价值品牌的品牌价值仍远远领先于其他品牌,今年它们的总价值上升3%,至3.4万亿美元. 过去10年,全球最具价值品牌100强的品牌价值增长一倍多,增幅达到133%.品牌价值是根据收入和盈利能力等财务指标,结合消费者品牌认知调查计

Linux内核中的GPIO系统之(3):pin controller driver代码分析--devm_kzalloc使用【转】

转自:http://www.wowotech.net/linux_kenrel/pin-controller-driver.html 一.前言 对于一个嵌入式软件工程师,我们的软件模块经常和硬件打交道,pin control subsystem也不例外,被它驱动的硬件叫做pin controller(一般ARM soc的datasheet会把pin controller的内容放入GPIO controller的章节中),主要功能包括: (1)pin multiplexing.基于ARM core

SoC软件架构设计之二:内存管理单元的硬件设计实现

程序的大部分代码都可以在必要的时候才加载到内存去执行,运行完后可以被直接丢弃或者被其他代码覆盖.我们PC上同时跑着很多的应用程序,每个应用程序使用的虚拟地址空间几乎可以整个线性地址空间(除了部分留给操作系统或者预留它用),可以认为每个应用程序都独占了整个虚拟地址空间(字长是32的CPU是4G的虚拟地址空间),但我们的物理内存只是1G或者2G.即多个应用程序在同时竞争使用这块物理内存,其必然会导致某个时刻只存在程序的某个片段在执行,也即是所有程序代码和数据分时复用物理内存空间-这就是内存管理单元(

elasticsearch系列六:聚合分析(聚合分析简介、指标聚合、桶聚合)

一.聚合分析简介 1. ES聚合分析是什么? 聚合分析是数据库中重要的功能特性,完成对一个查询的数据集中数据的聚合计算,如:找出某字段(或计算表达式的结果)的最大值.最小值,计算和.平均值等.ES作为搜索引擎兼数据库,同样提供了强大的聚合分析能力. 对一个数据集求最大.最小.和.平均值等指标的聚合,在ES中称为指标聚合   metric 而关系型数据库中除了有聚合函数外,还可以对查询出的数据进行分组group by,再在组上进行指标聚合.在 ES 中group by 称为分桶,桶聚合 bucke

ElasticSearch实践系列(三):探索数据

前言 经过前两篇文章得实践,我们已经了解了ElasticSearch的基础知识,本篇文章让我来操作一些更真实的数据集.我们可以利用www.json-generator.com/生成如下的文档结构: { "account_number": 1, "balance": 39225, "firstname": "Amber", "lastname": "Duke", "age&quo