• K-Piggy-Bank


    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
    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 。
     
     1 #include <stdio.h>
     2 
     3 #define MAX 10005
     4 #define inf 0xfffffff
     5 
     6 int f[MAX];
     7 
     8 int min(int a,int b)
     9 {return a<b?a:b;}
    10 
    11 
    12 int main()
    13 {
    14     int t,w1,w2,n,W,V,i,j,weight;
    15     scanf("%d",&t);
    16     while (t--)
    17     {
    18         scanf("%d%d",&w1,&w2);
    19         weight=w2-w1;
    20 
    21         for (i=1;i<=weight;i++) f[i]=inf;
    22 
    23         f[0]=0; //初始化刚好装满的情况,保证刚好装满能更新数据
    24 
    25         scanf("%d",&n);
    26 
    27         for (i=1;i<=n;i++)
    28         {
    29             scanf("%d%d",&V,&W);
    30             for (j=W;j<=weight;j++)
    31                 f[j]=min(f[j],f[j-W]+V);
    32         }
    33 
    34         if (f[weight]==inf)
    35         printf("This is impossible.
    ");
    36         else
    37             printf("The minimum amount of money in the piggy-bank is %d.
    ",f[weight]);
    38     }
    39     return 0;
    40 }
    View Code
  • 相关阅读:
    dedecms自定义表单提交获取时间跟ip地址
    JQuery购物车多物品数量的加减+总价计算
    jquery手机触屏滑动拼音字母城市选择器代码
    js实现图片上传实时显示
    js实现发送验证码倒计时效果
    JS 仿支付宝input文本输入框放大组件
    js实现倒计时效果
    jquery统计输入文字的个数并对其进行判断
    【Linux】Linux系统安全设置
    java泛型(generics)
  • 原文地址:https://www.cnblogs.com/haoabcd2010/p/5735646.html
Copyright © 2020-2023  润新知