关闭

UVa 165 Stamps 【DFS】

标签: dfs
226人阅读 评论(0) 收藏 举报
分类:


 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 h, k 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


恩,题目大意就是说,不同面额的邮票,输入数据 h 和 k ,意思是有 k 类不同面额的邮票,然后拿 h 张组成面额,求的是能组成的连续的面额的最大值。例如 3 张 2 种,若这两种面额为1 和 4 可以组成从1 到 6 虽然也可以组成8但是不能组成7所有这里的值为6 但是当这两种面额为 1 和 3 时 ,就能组成从1 到7 ,所以输入数据 3 和 2 输出数据应该为 7 。恩,看了人家的题解才懂,要递归遍历面额的不同值,在不同面额的情况下要遍历不同的张数判断能否组成连续的金额,最后找到连续的最大值。然后可以想到 staval[0]为 1 即是面额种类中必有 1 因为很多金额的组成都要用到 1 ,然后maxval [0] 为 h 。然后就是下一种类的面额值怎么找的问题,首先要比前一个面额值大,不能比前一个面额所能组成的最大金额大,因为一旦比它大,就不能组成连续的金额了。例如现有面额值为 1  2  3  下一种就要大于 3 而不能大于9 一旦大于9例如10那么就不能组成9而发生断层了。


#include<cstdio>
#include<cstring>
#define maxn 1001
using namespace std;
int h,k,msum;
int maxval[maxn],staval[maxn],ans[maxn];
bool vis[maxn];
void dfs(int c,int n,int sum)//张数为c种类为n组成的金额为sum
{
    vis[sum]=true;
    if(c>=h)
        return ;
    for(int i=0;i<=n;++i)
        dfs(c+1,n,sum+staval[i]);
}
void rsearch(int c)
{
    if(c>=k)
    {
        if(maxval[c-1]>msum)
        {
            msum=maxval[c-1];
            memcpy(ans,staval,sizeof(staval));
        }
        return ;
    }
    for(int i=staval[c-1]+1;i<=maxval[c-1]+1;++i)
    {
        staval[c]=i;
        memset(vis,false,sizeof(vis));
        dfs(0,c,0);
        int num=0,j=1;
        while(vis[j++])
            ++num;
        maxval[c]=num;
        rsearch(c+1);
    }
}
int main()
{
    while(scanf("%d%d",&h,&k),h+k)
    {
        staval[0]=1;
        maxval[0]=h;
        msum=-1;
        rsearch(1);
        for(int i=0;i<k;++i)
            printf("%3d",ans[i]);
        printf(" ->%3d\n",msum);
    }
    return 0;
}

上面那种递归当数据很大时就会超时了,下面看人家题解是用了动态规划的思想,可以转化为求组成金额 k 所要的(在已有面额种类中)最少的张数。cnt[k] 表示当前情况下组成金额为 k 所需要的最少的张数。http://blog.csdn.net/shuangde800/article/details/7755254这个转载的文章解释的感觉挺好


#include<cstdio>
#include<cstring>
#include<algorithm>
#define inf 0x3f3f3f3f
#define maxn 1001
using namespace std;
int h,k,msum;
int ans[maxn],staval[maxn],maxval[maxn],cnt[maxn];
bool vis[maxn];
void rsearch(int c)
{
    if(c>=k)
    {
        if(msum<maxval[c-1])
        {
            msum=maxval[c-1];
            memcpy(ans,staval,sizeof(staval));
        }
        return ;
    }
    int tem[maxn];
    memcpy(tem,cnt,sizeof(cnt));
    for(int i=staval[c-1]+1;i<=maxval[c-1]+1;++i)
    {
        staval[c]=i;
        for(int j=0;j<staval[c-1]*h;++j)//这里动态规划思想
        {
            if(cnt[j]<h)
            {
                for(int l=1;l<=h-cnt[j];++l)
                {
                    if(cnt[j]+l<cnt[j+l*i]&&j+l*i<maxn)
                        cnt[j+l*i]=cnt[j]+l;
                }
            }
        }
        int r=maxval[c-1];
        while(cnt[r+1]<inf)
            r++;
        maxval[c]=r;
        rsearch(c+1);
        memcpy(cnt,tem,sizeof(tem));
    }
}
int main()
{
    while(scanf("%d%d",&h,&k),h+k)
    {
        staval[0]=1;
        maxval[0]=h;
        msum=-1;
        int i;
        for(i=0;i<=h;++i)
            cnt[i]=i;
        while(i<maxn)
            cnt[i++]=inf;
        rsearch(1);
        for(int i=0;i<k;++i)
            printf("%3d",ans[i]);
        printf(" ->%3d\n",msum);
    }
    return 0;
}


0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

UVA 165 Stamps (DFS深搜回溯)

UVA 165 stamps The government of Nova Mareterrania requires that various legal documents have stamps attached to them so that the government can ...
  • u011217342
  • u011217342
  • 2013-08-04 22:54
  • 847

uva - 165 - Stamps(回溯、模拟)

题意: 输入两个数字, h(贴邮票的位置数)、k(邮票的面额数)。输出可以组成的最大、连续、面额。
  • u013545222
  • u013545222
  • 2014-02-03 19:37
  • 495

UVa 165 - Stamps 解题报告(暴力)

Stamps  The government of Nova Mareterrania requires that various legal documents have stamps attached to them so that the government ...
  • kbdwo
  • kbdwo
  • 2014-07-30 10:26
  • 493

UVa 165 - Stamps(连续邮资问题)

题意: 某国家发行k种不同面值的邮票,并且规定每张信封上最多只能贴h张邮票。 公式n(h,k)表示用从k中面值的邮票中选择h张邮票,可以组成面额为连续的1,2,3,……n, n是能达到的最大面值之和。 例如当h=3,k=2时, 假设两种面值取值为1,4, 那么它们能组成连续的1……6, ...
  • HelloWorld10086
  • HelloWorld10086
  • 2015-04-22 17:01
  • 419

UVA439深搜和宽搜区别

这题其实就是一个变种的宽搜,很简单,可是后来发现自己把宽搜和深搜给记反了,这题就一直写的是深搜, 但是显然宽搜的题用深搜做事不对的,也学可以,反正本弱不会,但是前面几个深搜的题目,我都是用宽搜 做的,事实证明深搜的题目是可以用宽搜来做的,而深搜和宽搜的最主要区别就是深搜用栈来实现,而宽搜用 队...
  • ONE_PIECE_HMH
  • ONE_PIECE_HMH
  • 2015-09-02 14:02
  • 650

UVa 10129 - Play on Words(DFS判连通+欧拉回路)

给出n个单词,问能否将这n个单词连成串,使得每个单词最后一个字母和下个单词第一个字母相同。 首先对单词预处理,变为从首字母到最后一个字母的有向边,将单词集合变为有向图。 然后统计每个点的入度和出度,至多有两个点出度和入度不相等,且一点出度比入度大1,另一点入度比出度大1。 再判断连通性,所有出现过的...
  • wcr1996
  • wcr1996
  • 2014-11-30 19:11
  • 1274

习题4-6 莫尔斯电码 UVa508

算法竞赛入门经典(第2版)第4章 函数和递归 习题4-6 莫尔斯电码  UVa508 感悟。 1、阅读书中题目,从网站下载英文原题,重点在看输入输出数据与格式。 2、
  • mrcrack
  • mrcrack
  • 2016-11-05 21:35
  • 838

uva 165(暴力求解)

题意: 题解:
  • u013392752
  • u013392752
  • 2014-08-15 01:00
  • 503

uva 165 卡住了一下午!

最近上网发现自己写的文章出现在很多人的博客上面,还不注明是转载,还是原创!面对这种行为我无力吐槽,转载还请注明出处。谢谢! http://blog.csdn.net/monkeyduck 这道题题意不太好懂,说的是给定h和k两个数,h是邮票的张数,k是邮票面额的种类,h+k 这个题最开始我...
  • u011613729
  • u011613729
  • 2013-11-01 21:09
  • 994

习题9-1 最长的滑雪路径 UVa10285

1.题目描述:点击打开链接 2.解题思路:本题利用DAG上最长路模型解决,将所有符合高度严格减小的相邻点保存,由于最多只有四个,用稀疏图的存储方法即可。 3.代码: #define _CRT_SECURE_NO_WARNINGS #include #include #include #inc...
  • u014800748
  • u014800748
  • 2015-02-14 22:56
  • 725
    个人资料
    • 访问:97250次
    • 积分:4552
    • 等级:
    • 排名:第7552名
    • 原创:369篇
    • 转载:7篇
    • 译文:0篇
    • 评论:3条
    最新评论