九度笔记之 1209最小邮票数

题目1209:最小邮票数

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:1176

解决:358

题目描述:

    有若干张邮票,要求从中选取最少的邮票张数凑成一个给定的总值。
    如,有1分,3分,3分,3分,4分五张邮票,要求凑成10分,则使用3张邮票:3分、3分、4分即可。

输入:

    有多组数据,对于每组数据,首先是要求凑成的邮票总值M,M<100。然后是一个数N,N〈20,表示有N张邮票。接下来是N个正整数,分别表示这N张邮票的面值,且以升序排列。

输出:

      对于每组数据,能够凑成总值M的最少邮票张数。若无解,输出0。

样例输入:
10
5
1 3 3 3 4
样例输出:
3

算法分析

动态规划问题,和之后的两船载物、今年暑假不AC、招聘会、热爱生活(发大米)、DOTA等均为同一类型题目,背包问题。
在该题中采用动态规划,计算从1到m面值的最小邮票数 。更新如下
		for(int j = m;j>=num[i];j--){ //must from m to num[i]
			dp[j] = std::min(dp[j],dp[j-num[i]]+1);
		}
表示在第i个邮票加入后,从m值到num[i]值的最小邮票数, 一定要从m到num[i],由多到少,因为邮票的数量是固定的。
因为更新时
std::min(dp[j],dp[j-num[i]]+1);
min里面的dp[ j ],  dp[ j-num[i] ]都是只有前i-1个邮票的情况下组成的最小数,如果从num[i]开始更新,那么随着i增加到后面dp[ j-num[i] ]表示的就是前i个邮票的情况下组成的最小数,再+1,第i个邮票就重复了。

如果是从num[i]到m更新,表示不同面值邮票数是无限的,具体会在以后相关的算法问题中说明。(DOTA问题)
std::min(dp[j],dp[j-num[i]]+1);

当前的最小数 是j总值下前i-1个邮票所能组成的最小数,j-num[i]总值前i-1个邮票所能组成的最小数+1  的最小值,也就是前i-1个邮票的最小个数  和  包含第i个邮票的最小个数 的最小值。


           类似题目


源程序

//============================================================================
// Name        : judo1209.cpp
// Author      : wdy
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
 
#include <iostream>
#include <cmath>
using namespace std;
int INF = 1<<30;
void minNum(int m,int n){
    int *dp = new int[m+1];
    dp[0] = 0;
    for(int i = 1;i<=m;i++)
        dp[i] = INF;
    int *num = new int[n];
    for(int i = 0;i<n;i++)
        std::cin>>num[i];
    for(int i = 0;i<n;i++){
        for(int j = m;j>=num[i];j--){ //must from m to num[i]
            dp[j] = std::min(dp[j],dp[j-num[i]]+1);
        }
    }
    if(dp[m]<INF)
        std::cout<<dp[m]<<std::endl;
    else
        std::cout<<0<<std::endl;
}
void minNumnew(int m,int n){
    int *dp = new int[m+1];
    dp[0] = 0;
    for(int i = 1;i<=m;i++)
        dp[i] = INF;
 
    int num;
    for(int i = 0;i<n;i++){
        std::cin>>num;
        for(int j = m;j>=num;j--){ //must from m to num[i]
            dp[j] = std::min(dp[j],dp[j-num]+1);
        }
    }
    if(dp[m]<INF)
        std::cout<<dp[m]<<std::endl;
    else
        std::cout<<0<<std::endl;
}
void judo(){
    int m = 0;
    int n = 0;
    while(std::cin>>m>>n){
        minNumnew(m,n);
    }
}
 
int main() {
     judo();
    return 0;
}
/**************************************************************
    Problem: 1209
    User: KES
    Language: C++
    Result: Accepted
    Time:160 ms
    Memory:3632 kb
****************************************************************/




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DESCRIPTION: 1.Analyze Problem A : sorted stamps array A={ai} ai: one stamp element in array n: array size, that is number of elements in array r: desired value of stamps F(n,r):expected result, minimum number of stamps for a given value r from n size array. S: selected stamps array S={si} 2.Choose Algorithm a.Greedy algorithm seems to be a good choice, try to solve it in O(n), i try divide array into subarry B={bi}, r should larger than every elemnt in B that is r>bi and suppose bk is the smallest element in B, so that r= bk%r, f(i,r)=(bk/r), F(n,r)=∑f(i,r). The main idea is to choose the last element who larger than desired value each time. However,it can not give us optimal solution in some condition, like A={8,5,4,1}, if r=10, this algoritm will give a solution F(n,r)=3, S={8,1,1},but the optimal solution should be F(n,r)=2, S={5,5}. b.Full search so the straight forwards algorithm is to search for every solution in A for desired value directly.However, it will always take O(n!) to go through every combination. c.Dynamic programming, at last, I decide to choose dynamic programming. analyze optimal structure, suppose in A={ai}, for a specific stamp ak,there will be two cases it is choosen so that f(i,r)=1+f(i,r-ak) , 1<=i<=k, r>=ak it is not valid so that f(i,r)=f(i-1,r) 3.Design Dynamic programming optimal structure: Compute-opt(r)= 1 + Compute-opt(r-ai) value: Compute-opt(r) = ∞ (r < 0) Compute-opt(r) = 0 (r = 0) Compute-opt(r) = 1+{Compute-opt(r-ai)} ( 1=<i<=n, r>ai>0 ) Complexity :O(nr) Memory cost:O(n+r) Compute in a bottom-up style to recursive every desired value and array. store value of Compute-opt in memory for future use, so that we can easily get value from those memory in future recursive call, and avoid compute again, that is if the array is not change, you can easily fetch result any desired value j (j < r, r is the value using for compute ). 2.For User totally, I design a small command line for this machine list below 1.Manual Operation 2.Self Auto Testing 3.Check Results q.Quit Manual Operation: when select this machine will turn to be manual mode, ask person to input stamps and desired value; Self Auto Testing:when select this machine will turn to be auto mode, show the test case already design in code after that machine will quit automatically. Check Results: only be visiable in Manual Operation, people can check desired value for the array input before, the desired value should be no more than first time input. Quit, clean all the memory and quit system.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值