Piggy-Bank

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

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.
 
 

//dp入门题。
第一行一个整数 t 代表 t 组数据,数据第一行两个整数 1 <= E <= F <= 10000,代表小猪存钱罐空的质量和放满的质量,第二行是整数 N 代表有几种钱币
然后 N 行每行两个整数,分别代表钱币的价值和质量,求将存钱罐放满的最小价值,放不满输出 This is impossible.
 
 
//用一个一维数组就可以了,f [ i ] 代表将重量为 i 的存钱罐放满的最小价值,读一次钱币,就循环一次,从W开始循环,一定 可能放进去,如果放进去价值更小,更新。
初始化很重要,都设为 inf ,然后 f[0] 初始化为 0 很重要,不然怎么都是 inf 。
 
 #include <stdio.h>

 #define MAX 10005
#define inf 0xfffffff int f[MAX]; int min(int a,int b)
{return a<b?a:b;} int main()
{
int t,w1,w2,n,W,V,i,j,weight;
scanf("%d",&t);
while (t--)
{
scanf("%d%d",&w1,&w2);
weight=w2-w1; for (i=;i<=weight;i++) f[i]=inf; f[]=; //初始化刚好装满的情况,保证刚好装满能更新数据 scanf("%d",&n); for (i=;i<=n;i++)
{
scanf("%d%d",&V,&W);
for (j=W;j<=weight;j++)
f[j]=min(f[j],f[j-W]+V);
} if (f[weight]==inf)
printf("This is impossible.\n");
else
printf("The minimum amount of money in the piggy-bank is %d.\n",f[weight]);
}
return ;
}

K-Piggy-Bank的更多相关文章

  1. ACM Piggy Bank

    Problem Description Before ACM can do anything, a budget must be prepared and the necessary financia ...

  2. Codeforces Round #408 (Div. 2) C. Bank Hacking

    http://codeforces.com/contest/796/problem/C Although Inzane successfully found his beloved bone, Zan ...

  3. CF796C Bank Hacking 思维

    Although Inzane successfully found his beloved bone, Zane, his owner, has yet to return. To search f ...

  4. codeforce 796C - Bank Hacking(无根树+思维)

    题目 Although Inzane successfully found his beloved bone, Zane, his owner, has yet to return. To searc ...

  5. CF796C Bank Hacking 题解

    洛谷链接 题目 Although Inzane successfully found his beloved bone, Zane, his owner, has yet to return. To ...

  6. [Swift]LeetCode433. 最小基因变化 | Minimum Genetic Mutation

    A gene string can be represented by an 8-character long string, with choices from "A", &qu ...

  7. Android开发训练之第五章第五节——Resolving Cloud Save Conflicts

    Resolving Cloud Save Conflicts IN THIS DOCUMENT Get Notified of Conflicts Handle the Simple Cases De ...

  8. Codeforces Round #408 (Div. 2) A B C 模拟 模拟 set

    A. Buying A House time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  9. CF-796C

    C. Bank Hacking time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  10. luogu P3420 [POI2005]SKA-Piggy Banks

    题目描述 Byteazar the Dragon has NN piggy banks. Each piggy bank can either be opened with its correspon ...

随机推荐

  1. 关于RBAC(Role-Base Access Control)的理解

    基于角色的访问控制(Role-Base Access Control) 有两种正在实践中使用的RBAC访问控制方式:隐式(模糊)的方式和显示(明确)的方式. 今天依旧有大量的软件应用是使用隐式的访问控 ...

  2. JAVA之继承的必要性

    //说明继承的必要性package com.test; public class test { /**     * @param args     */    public static void m ...

  3. 修改kafka broker.id

    kafka.common.InconsistentBrokerIdException: Configured broker.id 1 doesn't match stored broker.id 0 ...

  4. C#设计模式---观察者模式简单例子

    在开发过程中经常遇到一个模块中的 一个方法调用了其他模块中相关的方法 比如说在一个系统中,如果出现了错误,就调用专门进行错误处理的模块中的方法进行错误处理 而因为错误处理的操作有很多,所以将这些具体的 ...

  5. hdu 5365 Run(BC 50 B题)(求四边形的个数)

    本来准备睡觉.结果还是忍不住想把它A了.由于已经看了题解了, 题意:就是给你一些坐标.都是整数,求一些正多边形的数目,官方题讲解是地球人都知道整数坐标构不成正三角形.正五边形和正六边形的... 然而我 ...

  6. java.lang.NoSuchMethodError: ognl.SimpleNode.isEvalChain(Lognl/OgnlContext;)Z解决方法

    执行JavaEE项目时出现例如以下错误: java.lang.NoSuchMethodError: ognl.SimpleNode.isEvalChain(Lognl/OgnlContext;)Z a ...

  7. react-native 常用组件的用法(一)

    1.View组件 View是一个支持Flexbox布局.样式.一些触摸处理.和一些无障碍功能的容器,并且它可以放到其它的视图里,也可以有任意多个任意类型的子视图. View的设计初衷是和StyleSh ...

  8. 【BIEE】01_下载安装BIEE(Business Intelligence)11g 11.1.1.9.0

    环境准备 安装文件 如果操作系统是64位,则下载64位版本,我安装的系统是64位的 1.下载所有安装文件 1.1 Oracle Database 11g R2 下载地址: http://www.ora ...

  9. 代理工具Charles使用

    代理工具Charles使用 分类: MAC 2014-03-27 20:41 7810人阅读 评论(2) 收藏 举报 手机开发 一.跟踪HTTPS 1.下载官方的证书ssl.zip证书,解压成*.cr ...

  10. UINavigationController改变动画效果

    @interface UINavigationController (CustomTransition) - (void) pushWithCustomAnimation:(UIViewControl ...