zoukankan      html  css  js  c++  java
  • HDU

    先上题目:

    Piggy-Bank

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


    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.
     
      题意:有一个存钱罐,给你存钱罐在空的时候和满的时候的质量,然后告诉你里面可能存在的硬币种类以及它们的价值和质量,求出这个存钱罐里面可能存在的最最小金钱价值。这是个明显的完全背包的模板题。
      总结一下完全背包,最原始的思路就是:因为硬币的数目是无限的,对于某一种硬币i,我们可以看成存在如下硬币种类(p是价值,w是质量):p[i]*k,w[i]*k 其中w[i]*k<=j也就是说有(k+1)种有硬币i衍生出来的硬币,然后就可以将问题转化为01背包了。但是这样的话时间复杂度会变得很大。
      改进:
      ① 对于两种硬币i,i',如果p[i]>=p[i'] && w[i]<=w[i']成立那么就说明选择i比选择i性价比绝对更高'(价值大但是代价小的东西谁都喜欢)这样就可以把值得选择的硬币的范围缩小了,对于随机数据来说这是一个不错的方法,但是对于精心设计的数据来说这方法很难发挥优化的效果。
      ② 对于枚举的个数k以2的次幂数进行枚举,同样可以在一定程度上进行优化,但是总感觉这方法还是不好,有点难控制。
      ③ 将01背包的代码
    for(int i=1;i<=n;i++){
                for(int j=m;j>=0;j--){
                    if(w[i]<=j) dp[j]=min(dp[j-w[i]]+p[i],dp[j]);
                }
    }

      改为:

    for(int i=1;i<=n;i++){
                for(int j=0;j<=m;j++){
                    if(w[i]<=j) dp[j]=min(dp[j-w[i]]+p[i],dp[j]);
                }
    }

      也就是对背包容量进行枚举的方向改为从0~m,对于01背包的方向为从m~0是因为这是保证dp[j-w[i]]在被使用前不被新的数据覆盖;而对于完全背包,因为每一个物品的个数是无限的,所以从正向开始扫描,那么就意味着当前调用的dp[j-w[i]]可能是由前面的物品放入多个以后得到的最佳结果。也就是说:

      对于01背包   dp[i][j]=max{ dp[i-1][j-w[i]]+p[i] , dp[i-1][i]}

      对于完全背包 dp[i][j]=max{ dp[i-1][j-w[i]]+p[i] , dp[i][j-w[i]]+p[i] , dp[i-1][i]} (第二项的意思就是考虑可能放入了 i 以后再继续放i的情况)

    上代码:

     1 #include <cstdio>
     2 #include <cstring>
     3 #define min(x,y) (x < y ? x : y)
     4 #define MAX 10001
     5 #define INF 1000000000
     6 #define LL long long
     7 #define DEBUG(x) printf("Line: %d
    ",x)
     8 using namespace std;
     9 
    10 int p[MAX*5],w[MAX*5],n,m;
    11 LL dp[MAX];
    12 
    13 int main()
    14 {
    15     int t,e,f;
    16     //freopen("data.txt","r",stdin);
    17     scanf("%d",&t);
    18     while(t--){
    19         scanf("%d %d",&e,&f);
    20         m=f-e;
    21         scanf("%d",&n);
    22         for(int i=1;i<=n;i++){
    23             scanf("%d %d",&p[i],&w[i]);
    24         }
    25         dp[0]=0;
    26         for(int i=1;i<=m;i++){
    27             dp[i]=INF;
    28         }
    29 
    30         for(int i=1;i<=n;i++){
    31             for(int j=0;j<=m;j++){
    32                 if(w[i]<=j) dp[j]=min(dp[j-w[i]]+p[i],dp[j]);
    33                 //printf("%I64d ",dp[j]);
    34             }
    35             //printf("
    ");
    36         }
    37         if(dp[m]<INF) printf("The minimum amount of money in the piggy-bank is %I64d.
    ",dp[m]);
    38         else printf("This is impossible.
    ");
    39     }
    40     return 0;
    41 }
    1114
  • 相关阅读:
    趁热讲讲skin.xml支持的标签和attributes
    如何配置和编译ogre 1.7.0 + cegui 0.7.1
    关于OGRE基础教程6中CEGUI的layout文件can not locate的问题
    skin.xml皮肤配置讲解
    OCX控件注册相关(检查是否注册,注册,反注册)
    重回博客园继续我的 GUI库
    窗口类的定义
    UI库需要完成的任务
    屏幕截图代码
    深入C++的默认构造函数1
  • 原文地址:https://www.cnblogs.com/sineatos/p/3525253.html
Copyright © 2011-2022 走看看