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. Ubuntu14.04进行配置符号链接arm-2009q3.tar.bz2

    进入系统终端:cd /usr/local/ 创建文件夹arm:并进去arm,将winshare共享文件夹里面的文件/arm-2009q3复制到arm文件夹中; tar -jxvf  arm-2009q ...

  2. 使用Spring的AbstractRoutingDataSource实现多数据源切换

    https://www.cnblogs.com/softidea/p/7127874.html?utm_source=itdadao&utm_medium=referral https://b ...

  3. linux中grep命令

    grep 是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来. grep常用用法 [root@www ~]# grep [-acinv] [--color=auto] '搜寻字 ...

  4. PHP面试题遇到的几个坑。...面壁ing

    1.指针悬挂问题 $array = [1, 2, 3]; echo implode(',', $array), "\n"; foreach ($array as &$val ...

  5. C#与数据结构--图的遍历

    http://www.cnblogs.com/abatei/archive/2008/06/06/1215114.html 8.2 图的存储结构 图的存储结构除了要存储图中各个顶点的本身的信息外,同时 ...

  6. binder对于boolean类型的传递

    通过写了一个AIDL文件,查看自动生成的java文件代码得出的结论: 假如有:  boolean loadNativeSharedLib(String libPathName); 则对应的binder ...

  7. Solidworks 如何快速完全定义草图

    工具-尺寸标注-完全定义草图

  8. iOS开发 最近开发了蓝牙模块,在此记录总结一下(转载)

    1.基本概念 <1>中心者模式:常用的(其实99.99%)就是使用中心者模式作为开发,就是我们手机作为主机,连接蓝牙外设.由于开发只用到了中心者模式,所以我也只介绍中心者模式. <2 ...

  9. Easy way to change collation of all database objects in SQL Server

    This info is from: http://www.codeproject.com/Articles/302405/The-Easy-way-of-changing-Collation-of- ...

  10. badblocks检测磁盘坏道

    1.检测整个盘如/dev/sdb #badblocks -v /dev/sdb > badsectors.txt 2.检测一个分区#badblocks -v /dev/sdb1 > bad ...