算法训练 邮票 Java

给定一定数量和面值的邮票,求是否存在从1开始的连续邮资序列并输出其最大值。使用动态规划求解,通过状态转移方程dp[i] = min(dp[i - j]) + 1,其中j为小于等于i的邮票面值。
摘要由CSDN通过智能技术生成

问题描述

给定一个信封,有N(1≤N≤100)个位置可以贴邮票,每个位置只能贴一张邮票。我们现在有M(M<=100)种不同邮资的邮票,面值为X1,X2….Xm分(Xi是整数,1≤Xi≤255),每种都有N张。

显然,信封上能贴的邮资最小值是min(X1, X2, …, Xm),最大值是 N*max(X1, X2, …,  Xm)。由所有贴法得到的邮资值可形成一个集合(集合中没有重复数值),要求求出这个集合中是否存在从1到某个值的连续邮资序列,输出这个序列的最大值。

例如,N=4,M=2,面值分别为4分,1分,于是形成1,2,3,4,5,6,7,8,9,10,12,13,16的序列,而从1开始的连续邮资序列为1,2,3,4,5,6,7,8,9,10,所以连续邮资序列的最大值为10分。

输入格式

第一行:最多允许粘贴的邮票张数N;第二行:邮票种数M;第三行:空格隔开的M个数字,表示邮票的面值Xi。注意:Xi序列不一定是大小有序的!

输出格式

从1开始的连续邮资序列的最大值MAX。若不存在从1分开始的序列(即输入的邮票中没有1分面额的邮票),则输出0.

样例输入

样例一:
4
2
4 1
样例二:
10
5
2 4 6 8 10

样例输出

样例一:
10
样例二:
0

思路

  • 本题采用动态规划解决
  • 解题过程:
    • 对于N=4,M=2,面值分别为4分,1分:
      
    •  可得最大邮资值为4 * 4 = 16
      
    • 创建数组dp,dp[i] = j表示构成邮资值i最少需要的邮票数
      初始dp[0] = 0
      
    •  对于1--3,只能由1分的邮票构成,
       即dp[1] = 1,dp[2] = 2,dp[3] = 3
      
    • 对于4,可选面值1和4:
      使用面值1:
      在邮资为3的基础上选一张1,即dp[4] = dp[4 -  1] + 1 = dp[3] + 1 = 4
      使用面值4:在邮资为0的基础上选一张4,即dp[4] = dp[4 -  4] + 1 = dp[0] + 1 = 1  
      取之间的最小值即dp[4] = 1
      
    • 对于5,可选面值1和4:
      选面值1:
      在邮资为4的基础上选一张1,即dp[5] = dp[5 -  1] + 1 = dp[4] + 1 = 2
      选面值4:
      在邮资为1的基础上选一张4,即dp[5] = dp[5 -  4] + 1 = dp[1] + 1 = 2  
      
    • 以此类推,直到dp[i]的值大于给定的贴邮票的最大值,
      说明无法用这些邮票构成此邮资值
      例子中当i = 11时,dp[11] = min(dp[10] + 1, dp[7] + 1) = 5
      超过4,则返回上一个符合要求的数10
      即连续邮资序列最大到10
      
  • 通过对例题的解释,可得动态规划的状态转移方程为:
    dp[i] = min(dp[i - j] ) + 1
    j为小于等于i的邮票面值

代码

import java.util
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
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.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值