邮票问题-动态规划

邮票问题

【问题描述】

      给定一个信封,最多只允许粘贴N(N<=100)张邮票,我们现在有m(m<=100)种邮票,面值分别为:x1,x2,…….xm(xi<=255,为正整数),并假设各种邮票都有足够多张.

       要求计算所能获得的邮资最大范围,即求最大值MAX,使在1—MAX之间的每一个邮资值都能得到.

       例如:N=4,2种邮票,面值分别为1,4,于是可以得到1----10,12,13,16分的邮资,由于不能得到11分和15,所有邮资的最大范围是MAX=10.      

 

【样例输入】

       从键盘输入一个文本文件的文件名,该文件第1行为最多粘贴的邮票数N;2行为邮票种数m,以下m行各有一个数字,表示邮票的面值xi.:

 输入:

4

2

1

4

 

【样例输出】

1.       若最大范围为空,则在屏幕上输出MAX=0

2.       若最大范围不为空,则把结果输出到屏幕上.

输出:

MAX=10.


解题思路

   本题可以看成是一个集合问题,即求在贴的邮票不多于n张的可满足条件的邮资集.集合问题的关键在于判定元素是否在集合中,对于本题而言,是判断某个邮资是否在贴不多于n张邮票可满足.设当前考虑的邮资值为max,最多允许贴n张邮票,记为(max,n).如果首先贴一张面值为xi的邮票,那么剩下的问题是(max-xi,n-1)的问题.如果(max-xi,n-1)可解,那么(max,n)问题也可解.

       设f[max]表示邮资为max时所需最少的邮票数,f[MAX]=min{f[MAX-w[i]]+1}.当f[max]<=n,问题(max,n)可解,否则无解.找到第一个不能集齐的邮票面值或者是第一个超过n张邮票的面值即可。

代码

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int w[110];//每个邮票的面值
int f[110];
int max;//枚举最大面值
int n,m,t;
const int inf=0x3f3f3f3f;
int main()
{

    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&w[i]);
    }
    memset(f,inf,sizeof(f));
    f[0]=0;

    while(1)
    {
      for(max=1;;++max)
      {
        for(int i=1;i<=m;i++)
        {
            if(max>=w[i])//能贴第i张邮票
            {
                if(f[max]>f[max-w[i]]+1)//不贴和贴谁用的张数少
                    f[max]=f[max-w[i]]+1;
            }
        }

        if(f[max]==inf||f[max]>n)//第一个达不到的面值,或者超过了能贴的邮票总数
        {
            printf("%d\n",max-1);
            break;//跳出面值循环
        }
      }

      break;//跳出总循环
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值