Wednesday, January 29, 2014

SPOJ 39. Piggy-Bank

Here is the problem statement.

This is another classic problem of dynamic programming called Knapsack problem with 2 parameters. Read about knapsack probelm with 2 parameters here.


       DOWNLOAD THE FULL SOURCE CODE

4 comments:

  1. Hi David! I love this blog. It's really helpful. I am currently running inot a problem while submitting the solution to this problem on spoj. I downloaded your source code and ran it for a few tezt cases and found that the answers were similar to that of mine. Here is my source code. Please help.

    #include
    using namespace std;
    struct coin
    {
    float v,w,r;
    };
    void mergesort(coin a[],int start,int end)
    {
    int i=start,j=(start+end)/2+1;
    if(start>t;
    while(t--)
    {
    cin>>e>>f;
    cin>>n;
    coin c[n];
    for(i=0;i>c[i].v>>c[i].w;
    c[i].r=c[i].v/c[i].w;
    }
    mergesort(c,0,n-1);
    int total=0,index=0,value=0;
    while(true)
    {
    if(c[index].w<=f-e-total)
    {
    int mult=(int)(f-e-total)/c[index].w;
    value+=c[index].v*mult;
    total+=c[index++].w*mult;
    if(total==f-e)
    {
    cout<<"The minimum amount of money in the piggy-bank is "<>t;
    }

    Thanks in advance.

    ReplyDelete
    Replies
    1. Hi. For making it easier for me to check and for us to keep in contact please send your cpp file to email helpinprogramming@gmail.com

      Delete
    2. I have emailed you the cpp file. Please check it. Thanks in advance.

      Delete
  2. Sorry.The code wasn't posted completely in the previous comment.
    break;
    }
    }
    else
    {
    cout<<"This is impossible.\n";
    break;
    }
    }
    }
    // cin>>t;
    }

    ReplyDelete