UVa 11517 – Exact Change

Problem Link : http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2512

Idea:
1. This is a classical coin change problem. here dp array represent the minimum coin need to make an amount. example – dp[100] means we need at least dp[100] coins to make 100 cents.

2. At first calculate the dp[] table as described. then start a linear search from money to forward for finding first amount of money which value is not infinity as we set before dp calculation and when we find a such value that value is the payable money and dp value of that money is the minimum number of coins.


/*
MMP""MM""YMM   db      `7MN.   `7MF'`7MMM.     ,MMF' .g8""8q.`YMM'   `MM'
P'   MM   `7  ;MM:       MMN.    M    MMMb    dPMM .dP'    `YM.VMA   ,V
     MM      ,V^MM.      M YMb   M    M YM   ,M MM dM'      `MM VMA ,V
     MM     ,M  `MM      M  `MN. M    M  Mb  M' MM MM        MM  VMMP
     MM     AbmmmqMA     M   `MM.M    M  YM.P'  MM MM.      ,MP   MM
     MM    A'     VML    M     YMM    M  `YM'   MM `Mb.    ,dP'   MM
   .JMML..AMA.   .AMMA..JML.    YM  .JML. `'  .JMML. `"bmmd"'   .JMML.
*/

#include <bits/stdc++.h>

#define pii pair <int,int>
#define sc scanf
#define pf printf
#define Pi 2*acos(0.0)
#define ms(a,b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define MP make_pair
#define oo 1<<29
#define dd double
#define ll long long
#define EPS 10E-10
#define ff first
#define ss second
#define MAX 10000
#define CIN ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a) (int)a.size()
#define si(a) scanf("%d",&a)
#define sii(a,b) scanf("%d%d",&a,&b)
#define siii(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define loop(i,n) for(int i=0;i<n;i++)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define TEST_CASE(t) for(int z=1;z<=t;z++)
#define PRINT_CASE printf("Case %d: ",z)
#define all(a) a.begin(),a.end()
#define intlim 2147483648
#define inf 1000000
#define rtintlim 46340
#define llim 9223372036854775808
#define rtllim 3037000499
#define ull unsigned long long
#define I int


using namespace std;

int dp[20100];

int coin[110];

int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    int t;
    si(t);
    TEST_CASE(t)
    {
        int money;
        si(money);
        int n;
        si(n);
        loop(i,n)
        {
            si(coin[i]);
        }
        //sort(coin,coin+n,test);
        ms(dp,inf);
        dp[0]=0;
        for(int i=0; i<n; i++)
        {
            for(int j=money; j>-1; j--)
            {
                dp[j+coin[i]]=min(dp[j]+1,dp[j+coin[i]]);
            }
        }

        for(int i=money; i<=30100; i++)
        {
            if(dp[i]<10000)
            {
                pf("%d %d\n",i,dp[i]);
                break;
            }
        }
    }
    return 0;
}


Another Solution using same idea. this one is from revise 😛 .



/*
         +-+ +-+ +-+
         |R| |.| |S|
         +-+ +-+ +-+
 */

#include <bits/stdc++.h>

#define pii             pair <int,int>
#define sc              scanf
#define pf              printf
#define Pi              2*acos(0.0)
#define ms(a,b)         memset(a, b, sizeof(a))
#define pb(a)           push_back(a)
#define MP              make_pair
#define db              double
#define ll              long long
#define EPS             10E-10
#define ff              first
#define ss              second
#define sqr(x)          (x)*(x)
#define D(x)            cout<<#x " = "<<(x)<<endl
#define VI              vector <int>
#define DBG             pf("Hi\n")
#define MOD             100007
#define MAX             10000
#define CIN             ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a)           (int)a.size()
#define sf(a)           scanf("%d",&a)
#define sfl(a)          scanf("%lld",&a)
#define sff(a,b)        scanf("%d %d",&a,&b)
#define sffl(a,b)       scanf("%lld %lld",&a,&b)
#define sfff(a,b,c)     scanf("%d %d %d",&a,&b,&c)
#define sfffl(a,b,c)    scanf("%lld %lld %lld",&a,&b,&c)
#define loop(i,n)       for(int i=0;i<n;i++)
#define REP(i,a,b)      for(int i=a;i<b;i++)
#define TEST_CASE(t)    for(int z=1;z<=t;z++)
#define PRINT_CASE      printf("Case %d: ",z)
#define all(a)          a.begin(),a.end()
#define intlim          2147483648
#define ull             unsigned long long

using namespace std;


/*----------------------Graph Moves----------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*------------------------------------------------*/

/*-----------------------Bitmask------------------*/
//int Set(int N,int pos){return N=N | (1<<pos);}
//int reset(int N,int pos){return N= N & ~(1<<pos);}
//bool check(int N,int pos){return (bool)(N & (1<<pos));}
/*------------------------------------------------*/

int dp[1000007];
int coin[110];

int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);

    int t;
    sf(t);
    TEST_CASE(t)
    {
        ms(dp,1000000);
        int money,n;
        sf(money);
        sf(n);
        loop(i,n) sf(coin[i]);
        dp[0]=0;
        for(int i=0; i<n; i++)
        {
            for(int j=money; j>=0; j--)
            {
                dp[j+coin[i]]=min(dp[j]+1,dp[j+coin[i]]);
            }
        }

        for(int i=money;; i++)
        {
            if(dp[i]<1000000 && dp[i])
            {
                pf("%d %d\n",i,dp[i]);
                break;
            }
        }

    }

    return 0;
}



Advertisements

UVa 166 – Making Change


/*
User ID: turing_13
Link : http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=102
*/
 
#include <bits/stdc++.h>
 
#define pii pair <int,int>
#define sc scanf
#define pf printf
#define Pi 2*acos(0.0)
#define ms(a,b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define MP make_pair
#define oo 1<<29
#define dd double
#define ll long long
#define EPS 10E-10
#define ff first
#define ss second
#define MAX 200
#define SZ(a) (int)a.size()
#define getint(a) scanf("%d",&a)
#define loop(i,n) for(int i=0;i<n;i++)
#define all(a) a.begin(),a.end()
#define intlim 2147483648
#define inf 1000000
#define rtintlim 46340
#define llim 9223372036854775808
#define rtllim 3037000499
#define ull unsigned long long
#define I int
 
using namespace std;
 
int values[6]={1,2,4,10,20,40};//Multiply the original value by 20 to convert in integer.
int coins[6],dp[MAX],change[MAX];
double amount;
 
int shop_keeperChange(int v)
{
    for(int i=5;i>-1;i--)
    {
        if(v>=values[i])
            return 1+shop_keeperChange(v-values[i]);
    }
    return 0;
}
 
 
int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
 
    while(cin>>coins[0]>>coins[1]>>coins[2]>>coins[3]>>coins[4]>>coins[5])
    {
        if(!coins[0] && !coins[1] && !coins[2] && !coins[3] && !coins[4] && !coins[5])
            break;
        sc("%lf",&amount);
        ms(dp,inf);
        dp[0]=0;
        for(int c=0;c<6;c++)
        {
            while(coins[c])
            {
                for(int i=MAX-coins[c]-1;i>-1;i--)
                {
                    if(dp[i]<inf)
                    {
                        dp[i+values[c]]=min(dp[i]+1,dp[i+values[c]]);
                    }
                }
                coins[c]--;
            }
        }
 
        int money=amount*20;
        int ans=inf;
        for(int i=money;i<MAX;i++)
        {
            ans=min(ans,dp[i]+shop_keeperChange(i-money));
        }
        pf("%3d\n",ans);
    }
    return 0;
}

Another solution of the problem is this. But this one slower than upper one. 🙂



#include <bits/stdc++.h>

#define pii pair <int,int>
#define sc scanf
#define pf printf
#define Pi 2*acos(0.0)
#define ms(a,b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define MP make_pair
#define oo 1<<29
#define dd double
#define ll long long
#define EPS 10E-10
#define ff first
#define ss second
#define MAX 200
#define SZ(a) (int)a.size()
#define getint(a) scanf("%d",&a)
#define loop(i,n) for(int i=0;i<n;i++)
#define all(a) a.begin(),a.end()
#define intlim 2147483648
#define inf 1000000
#define rtintlim 46340
#define llim 9223372036854775808
#define rtllim 3037000499
#define ull unsigned long long
#define I int

using namespace std;

int values[6]={1,2,4,10,20,40};//Multiply the original value by 20 to convert in integer.
int coins[6],dp[MAX],change[MAX];
double amount;

void func()
{
    ms(change,inf);
    change[0]=0;
    for(int c=0;c<6;c++)
    {
        for(int i=0;i<MAX-values[c]-1;i++)
        {
            change[i+values[c]]=min(change[i]+1,change[i+values[c]]);
        }
    }
}

int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    func();
    while(cin>>coins[0]>>coins[1]>>coins[2]>>coins[3]>>coins[4]>>coins[5])
    {
        if(!coins[0] && !coins[1] && !coins[2] && !coins[3] && !coins[4] && !coins[5])
            break;
        sc("%lf",&amount);
        ms(dp,inf);
        dp[0]=0;
        for(int c=0;c<6;c++)
        {
            while(coins[c])
            {
                for(int i=MAX-coins[c]-1;i>-1;i--)
                {
                    if(dp[i]<inf)
                    {
                        dp[i+values[c]]=min(dp[i]+1,dp[i+values[c]]);
                    }
                }
                coins[c]--;
            }
        }

        int money=amount*20;
        int ans=inf;
        for(int i=money;i<MAX;i++)
        {
            ans=min(ans,dp[i]+shop_keeperChange(i-money));
        }
        pf("%3d\n",ans);
    }
    return 0;
}

UVa 147 – Dollars

This one is very tricky input problem. If u take input in a float then somehow convert it in int then the original value doesn’t exist.
For Example:
if we write:
double a = 1.15;
int x=floor(a);
int b=(a-x)*100;
then the value of b is 14 😀 .
Because 1.15 exist in double a like 1.149999999999999999.

So this process is not capable to solve the problem.


/*
User ID: turing_13
Problem Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=83
*/

#include <bits/stdc++.h>

#define pii pair <int,int>
#define sc scanf
#define pf printf
#define Pi 2*acos(0.0)
#define ms(a,b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define MP make_pair
#define oo 1<<29
#define dd double
#define ll long long
#define EPS 10E-10
#define ff first
#define ss second
#define MAX 30500
#define SZ(a) (int)a.size()
#define getint(a) scanf("%d",&a)
#define loop(i,a) for(int i=0;i<a;i++)
#define all(a) a.begin(),a.end()
#define intlim 2147483648
#define rtintlim 46340
#define llim 9223372036854775808
#define rtllim 3037000499
#define ull unsigned long long
#define I int

using namespace std;

int coins[11]= {5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000 };
unsigned long long dp[30500];

int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    int a,b;
    dp[0]=1;
    for(int i=0;i<11;i++)
        for(int j=coins[i];j<MAX;j++)
            dp[j]+=dp[j-coins[i]];
    while(sc("%d.%d",&a,&b))
    {
        int amount=a*100+b;
        if(amount==0)
            break;
        pf("%3d.%d%d%17llu\n",a,b/10,b%10,dp[amount]);
    }
    return 0;
}