PiggyBank-完全背包

PiggyBank-完全背包Piggy BankTimeLimi 2000 1000MS Java Others nbsp nbsp nbsp MemoryLimit 65536 32768K Java Others TotalSubmiss s 10191 nbsp nbsp nbsp AcceptedSubm s 5134ProblemD

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 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 OutputThe minimum amount of money in the piggy-bank is 60.
The minimum amount of money in the piggy-bank is 100.
This is impossible.

源代码:

#include 
                                     
                                       #define INF (1<<30) #define MAX 10002 int T , N , V , v , pack[MAX]; int value[MAX] , volume[MAX]; int Min( int a , int b ){ return a <= b ? a : b; } int CompletePack( void ); int main( ){ int min; scanf("%d",&T); while( T-- ){ scanf("%d%d",&v,&V); scanf("%d",&N); for( int i=0 ; i 
                                       
                                     

代码分析:题目是一道完全背包的题,很容易,不过要理解模板,完全背包要求物品的个数可以为无数个,所以用体积小的dp值来更新体积大的dp值,体现了物品能够多次选择的原则。注意初始化是i<=V,第一次wa了因为没写等号,囧...






































































版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/207419.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月19日 下午1:52
下一篇 2026年3月19日 下午1:52


相关推荐

  • python画图命令

    python画图命令画两张图importmatplotlib.pyplotaspltplt.rcParams[‘font.sans-serif’]=[‘SimHei’]#用来正常显示中文标签plt.rcParams[‘axes.unicode_minus’]=False#用来正常显示负号plt.figure(1)plt.plot(x_data,y_data,color="red",linewidth…

    2022年5月11日
    42
  • CSS Opacity属性对层叠上下文的影响

    CSS Opacity属性对层叠上下文的影响今天在遇到一个opacity过渡的时候,遇到了一个奇怪的问题:父元素设置了opacity。子元素设置了z-index:-1

    2022年5月25日
    60
  • MySQL性能优化

    MySQL性能优化

    2022年1月26日
    47
  • rabbitmq高可用集群搭建_mongodb高可用架构

    rabbitmq高可用集群搭建_mongodb高可用架构RabbitMQ高可用集群搭建1集群简介1.1集群架构​ 当单台RabbitMQ服务器的处理消息的能力达到瓶颈时,此时可以通过RabbitMQ集群来进行扩展,从而达到提升吞吐量的目的。RabbitMQ集群是一个或多个节点的逻辑分组,集群中的每个节点都是对等的,每个节点共享所有的用户,虚拟主机,队列,交换器,绑定关系,运行时参数和其他分布式状态等信息。一个高可用,负载均衡的RabbitMQ集群架构应类似下图:这里对上面的集群架构做一下解释说明:​ 首先一个基本的Rabbi

    2025年10月21日
    5
  • deepfakes怎么用_deepfakes-FaceSwap使用笔记

    deepfakes怎么用_deepfakes-FaceSwap使用笔记安装过程需要魔法上网,有些包国内下载太慢了conda环境在AnacondaPrompt里,查看所有环境,两个命令都行condaenvlistcondainfo–envs查看安装的包condalist更换环境,前面的conda不写好像也行condaactivatefaceswap提示nomodulenamedcv2进入相应的环境,安装缺少的包condainstallope…

    2022年5月9日
    48
  • 大龙虾OpenClaw的token不够吃了?用免费的 APIKey 续命

    大龙虾OpenClaw的token不够吃了?用免费的 APIKey 续命

    2026年3月13日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号