zoukankan      html  css  js  c++  java
  • UVA-11517 Exact Change(DP)

    题目大意:有n张钞票,面值可能不同。你要买一件东西,可能需要找零钱。问最少付多少钱,并求出最少的钞票张数。

    题目分析:定义状态dp(i,w)表示前i张钞票凑成w元需要的最少钞票张数。则状态转移方程为dp(i,w)=min(dp(i-1,w),dp(i-1,w-a(i))+1)。其中a(i)为第i张钞票的面值。

    代码如下:

    //# define AC
    
    # ifndef AC
    
    # include<iostream>
    # include<cstdio>
    # include<cstring>
    # include<vector>
    # include<queue>
    # include<list>
    # include<cmath>
    # include<set>
    # include<map>
    # include<string>
    # include<cstdlib>
    # include<algorithm>
    using namespace std;
    
    # define mid (l+(r-l)/2)
    
    typedef long long LL;
    typedef unsigned long long ULL;
    
    const int N=10000;
    const int mod=1e9+7;
    const int INF=0x3fffffff;
    const LL oo=0x7fffffffffffffff;
    
    int n,w,a[105];
    int dp[105][N*2+5];
    
    int main()
    {
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        int T;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d%d",&w,&n);
            int m=0;
            for(int i=0;i<n;++i){
                scanf("%d",a+i);
                m+=a[i];
            }
            m=min(m,N<<1);
            fill(dp[0],dp[0]+m+1,INF);
            dp[0][0]=0;
            dp[0][a[0]]=1;
            for(int i=1;i<n;++i){
                dp[i][0]=0;
                for(int j=1;j<=m;++j){
                    if(j<a[i]) dp[i][j]=dp[i-1][j];
                    else{
                        dp[i][j]=INF;
                        if(dp[i-1][j]!=-INF)
                            dp[i][j]=dp[i-1][j];
                        if(dp[i-1][j-a[i]]!=INF)
                            dp[i][j]=min(dp[i][j],dp[i-1][j-a[i]]+1);
                    }
                }
            }
            while(dp[n-1][w]==INF||!dp[n-1][w])
                ++w;
            printf("%d %d
    ",w,dp[n-1][w]);
        }
        return 0;
    }
    
    # endif
    

      

  • 相关阅读:
    PyCharm使用(完全图解(最新经典))
    pg存储过程学习
    sql_to_sqlalchemy
    python中嵌入lua解析器
    Python和lua互相调用
    Lupa
    [cb] Unity Editor 添加右键菜单
    [C#] 委托之Action和Func区别
    [反编译U3D]Decompile Unity Resources
    [cb] Assetbundle打包(一)
  • 原文地址:https://www.cnblogs.com/20143605--pcx/p/6081606.html
Copyright © 2011-2022 走看看