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. 关于#include文件包含

    1.对于函数头文件: #include <filename> 一般对于标准库文件以一个.h后缀结尾: 2.对于本地文件: #include "filename.h" 对 ...

  2. Qt之QStyledItemDelegate类

    主要用于自定义项的display和编辑: 通常有两个重载函数: // 决定该单元格的推荐大小 virtual QSize sizeHint(const QStyleOptionViewItem &am ...

  3. Scrapy的介绍和用法

    转载:https://www.toutiao.com/i6493421606306578958/ Scrapy是爬虫必须学会的一个框架!他确实很难搞的透彻!今天就不给大家全部介绍了!还是介绍其中的Cr ...

  4. 利用eolinker实现api接口mock测试(mock server)

    转载:http://blog.csdn.net/naicha_qin/article/details/78276172 前后端分离或者是进行单元测试的时候,必须要用mock api替换掉第三方调用或者 ...

  5. hadoop2.4 支持snappy

    我们hadoop2,4集群默认不支持snappy压缩,可是近期有业务方说他们的部分数据是snappy压缩的(这部分数据由另外一个集群提供给他们时就是snappy压缩格式的)想迁移到到我们集群上面来进行 ...

  6. Angular 学习笔记——run

    <!DOCTYPE html> <html lang="en" ng-app="myApp"> <head> <met ...

  7. 标准库Queue的实现

    跟上篇实现stack的思路一致,我增加了一些成员函数模板,支持不同类型的Queue之间的复制和赋值. 同时提供一个异常类. 代码如下: #ifndef QUEUE_HPP #define QUEUE_ ...

  8. 3D版翻页公告效果

    代码地址如下:http://www.demodashi.com/demo/12830.html 前言: 在逛小程序蘑菇街的时候,看到一个2D版滚动的翻页公告效果.其实看到这个效果的时候,一点都不觉得稀 ...

  9. Redis 数据恢复方法,redis-port 工具将自建 redis 的 rdb文件同步到云数据库

    1. Redis 恢复的机制 如果只配置 AOF ,重启时加载 AOF 文件恢复数据: 如果同时配置了 RDB 和 AOF ,启动是只加载 AOF 文件恢复数据: 如果只配置 RDB,启动是将加载 d ...

  10. Atitit.struts排除url 的设计and 原理 自定义filter 排除特定url

    Atitit.struts排除url 的设计and 原理 自定义filter 排除特定url 1.1. 原理流程1 2. Invoke1 3. StrutsX2 1.1. 原理流程 读取struts配 ...