• 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
    

      

  • 相关阅读:
    java — 排序算法
    sping框架(3)— 使用spring容器
    java — 线程池
    一致性哈希算法(consistent hashing)
    linux进程调度
    滑动窗口协议
    final和static关键字
    II 3.1 连接到服务器
    intel-hadoop/HiBench流程分析----以贝叶斯算法为例
    Android开发学习之路--基于vitamio的视频播放器(二)
  • 原文地址:https://www.cnblogs.com/20143605--pcx/p/6081606.html
Copyright © 2020-2023  润新知