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. appium python api(转)

    Appium_Python_Api文档 1.contextscontexts(self): Returns the contexts within the current session. 返回当前会 ...

  2. Java程序中的Log文件配置

    log4j.properties文件 log4j.rootLogger=info,stdout,logfile #stdout log4j.appender.stdout=org.apache.log ...

  3. 2017.7.10 (windows)redis的安装

    参考来自:http://www.runoob.com/redis/redis-install.html 1.下载地址 https://github.com/MSOpenTech/redis/relea ...

  4. es创建索引的格式,并初始化数据

    es创建索引的格式,并初始化数据 学习了:https://www.imooc.com/video/15759 1, 创建格式 POST 127.0.0.1:9200/book/novel/_mappi ...

  5. 为windows开启winrm service, 以便进行远程管理

    为windows开启winrm service, 以便进行远程管理   是windows 一种方便远程管理的服务:开启winrm service,便于在日常工作中,远程管理服务器,或通过脚本,同时管理 ...

  6. 载入本地Html文件

    NSString * resousePath = [[NSBundle mainBundle]resourcePath];         NSString * filePath = [resouse ...

  7. How to use Variables in different component

    1. In Script Task component Set Value: Dts.Variables["ErrorMsg"].Value = string.Format(&qu ...

  8. C#自动切换Windows窗口程序,如何才能调出主窗口?

      namespace AutoChangeWindow { partial class Form1 { /// <summary> /// 必需的设计器变量. /// </summ ...

  9. UUID(即GUID)

    UUID(GUID)是Cocoa Touch提供的一种生成唯一标识的机制.类型为CFUUIDRef的对象可以代表UUID,UUID是基于当前时间.计数器和硬件标识(通常是以太网卡的MAC地址)等数据计 ...

  10. Android中多线程编程(三)Handler更新UI的方式

    Handler更新UI的方式和原因以及遇到的问题 1.方式: 仅仅能通过Handler来更新UI. 代码例如以下: package com.chengdong.su.handlerdemo; impo ...