[回溯]Stamps UVA165



 Stamps 

The government of Nova Mareterrania requires that various legal documents have stamps attached to them so that the government can derive revenue from them. In terms of recent legislation, each class of document is limited in the number of stamps that may be attached to it. The government wishes to know how many different stamps, and of what values, they need to print to allow the widest choice of values to be made up under these conditions. Stamps are always valued in units of $1.

This has been analysed by government mathematicians who have derived a formula for n(h,k), where h is the number of stamps that may be attached to a document, k is the number of denominations of stamps available, and n is the largest attainable value in a continuous sequence starting from $1. For instance, if h=3, k=2 and the denominations are $1 and $4, we can make all the values from $1 to $6 (as well as $8, $9 and $12). However with the same values of h and k, but using $1 and $3 stamps we can make all the values from $1 to $7 (as well as $9). This is maximal, so n(3,2) = 7.

Unfortunately the formula relating n(h,k) to hk and the values of the stamps has been lost--it was published in one of the government reports but no-one can remember which one, and of the three researchers who started to search for the formula, two died of boredom and the third took a job as a lighthouse keeper because it provided more social stimulation.

The task has now been passed on to you. You doubt the existence of a formula in the first place so you decide to write a program that, for given values of h and k, will determine an optimum set of stamps and the value of n(h,k).

Input

Input will consist of several lines, each containing a value for h and k. The file will be terminated by two zeroes (0 0). For technical reasons the sum of h and k is limited to 9. (The President lost his little finger in a shooting accident and cannot count past 9).

Output

Output will consist of a line for each value of h and k consisting of the k stamp values in ascending order right justified in fields 3 characters wide, followed by a space and an arrow (->) and the value of n(h,k) right justified in a field 3 characters wide.

Sample input

3 2
0 0

Sample output

  1  3 ->  7

题意:

假设某国家发行了n种不同面值的邮票并且规定每张信封上最多只允许贴m张.连续邮资问题要求对于给定的n和m的值,给出邮票面值的最佳设计,使得可在1张信封上贴出从邮资1开始,增量为1的最大连续邮资区间.例如:当n=5,m=4时,面值为(1,3,11,15,32)的五种邮票可以贴出邮资的最大连续邮资区间是1到70.
分析与总结:
连续邮资问题算是一个比较典型的问题了,我参考了另外一个博主的解题思路: 连续邮资问题
这是他的思路:

首先开一个数组stampVal【0...i】来保存各个面值,再开一个maxVal[0...i]来保存当前所有面值能组成的最大连续面值。

那么,我们可以确定stampVal[0] 一定是等于1的。因为如果没有1的话,很多数字都不能凑成。

然后相应的,maxVal[0] = 1*h.   h为允许贴邮票的数量

接下去就是确定第二个,第三个......第k个邮票的面值了,这个该怎么确定呢?

对于stampVal[i+1],  它的取值范围是stampVal[i]+1 ~maxVal[i]+1. 

stampVal[i]+1好理解, 这一次取的面值肯定要比上一次的面值大,  而这次取的面值的上限是上次能达到的最大连续面值+1, 是因为如果比这个更大的话, 那么

就会出现断层, 即无法组成上次最大面值+1这个数了。 举个例子, 假设可以贴3张邮票,有3种面值,前面2种面值已经确定为1,2, 能达到的最大连续面值为6, 那么接下去第3种面值的取值范围为3~7。如果取得比7更大的话会怎样呢? 动手算下就知道了,假设取8的话, 那么面值为1,2,8,将无法组合出7 !!

说了这么多,总的概况就是对于下一张邮票的面值选择它的取值范围是 stampVal[i]+1 ~maxVal[i]+1这是重中之重
知道这个后写代码就非常好写了。这位博主还有更加优化的方法。
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdio>

using namespace std;

int m,n,mval;
int ans[200],val[200],maxval[200],tag[200];

void dfs(int pos,int s,int sum)
{
    tag[sum]=1;
    if(pos>=m) return;
    for(int i=0;i<=s;i++)
    {
        dfs(pos+1,s,sum+val[i]);
    }
}

void search(int pos)
{
    if(pos>=n)
    {
        if(maxval[pos-1]>mval)
        {
            mval=maxval[pos-1];
            memcpy(ans,val,sizeof(val));
        }
        return;
    }
    for(int i=val[pos-1]+1;i<=maxval[pos-1]+1;i++)//面值只能从这个范围里面选择
    {
        val[pos]=i;
        memset(tag,0,sizeof(tag));
        dfs(0,pos,0);
        int num=0,j=1;
        while(tag[j++]) num++;
        maxval[pos]=num;
        search(pos+1);
    }
}

int main()
{
    while(cin>>m>>n&&(m!=0||n!=0))
    {
        val[0]=1;
        maxval[0]=m;
        mval=0;
        search(1);
        for(int i=0; i<n; ++i)
            cout<<setw(3)<<ans[i];
        cout<<"->"<<setw(3)<<mval<<endl;
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值