Stamps and Envelope Size - UVa 242 dp

本博客探讨了邮政服务如何通过编程辅助解决邮票使用与信封容量之间的匹配问题,确保邮件覆盖范围无缝衔接,同时优化邮票打印成本。

Stamps and Envelope Size

Philatelists have collected stamps since long before postal workers were disgruntled. An excess of stamps may be bad news to a country's postal service, but good news to those that collect the excess stamps. The postal service works to minimize the number of stamps needed to provide seamless postage coverage. To this end you have been asked to write a program to assist the postal service.

Envelope size restricts the number of stamps that can be used on one envelope. For example, if 1 cent and 3 cent stamps are available and an envelope can accommodate 5 stamps, all postage from 1 to 13 cents can be ``covered":

tabular21

Although five 3 cent stamps yields an envelope with 15 cents postage, it is not possible to cover an envelope with 14 cents of stamps using at most five 1 and 3 cent stamps. Since the postal service wants maximal coverage without gaps, the maximal coverage is 13 cents.

Input

The first line of each data set contains the integer S, representing the maximum of stamps that an envelope can accommodate. The second line contains the integer N, representing the number of sets of stamp denominations in the data set. Each of the next N lines contains a set of stamp denominations. The first integer on each line is the number of denominations in the set, followed by a list of stamp denominations, in order from smallest to largest, with each denomination separated from the others by one or more spaces. There will be at most Sdenominations on each of the N lines. The maximum value of S is 10, the largest stamp denomination is 100, the maximum value of N is 10.

The input is terminated by a data set beginning with zero (S is zero).

Output

Output one line for each data set giving the maximal no-gap coverage followed by the stamp denominations that yield that coverage in the following format:

max coverage = <value> : <denominations>

If more than one set of denominations in a set yields the same maximal no-gap coverage, the set with the fewest number of denominations should be printed (this saves on stamp printing costs). If two sets with the same number of denominations yield the same maximal no-gap coverage, then the set with the lower maximum stamp denomination should be printed. For example, if five stamps fit on an envelope, then stamp sets of 1, 4, 12, 21 and 1, 5, 12, 28 both yield maximal no-gap coverage of 71 cents. The first set would be printed because both sets have the same number of denominations but the first set's largest denomination (21) is lower than that of the second set (28). If multiple sets in a sequence yield the same maximal no-gap coverage, have the same number of denominations, and have equal largest denominations, then print the set with the lewer second-maximum stamp denomination, and so on.

Sample Input

5
2
4 1 4 12 21
4 1 5 12 28
10
2
5 1 7 16 31 88
5 1 15 52 67 99
6
2
3 1 5 8
4 1 5 7 8
0

Sample Output

max coverage =  71 :  1  4 12 21
max coverage = 409 :  1  7 16 31 88
max coverage =  48 :  1  5  7  8

题意:对于每组有最大的邮票数量限制,然后n组数据中,每组有每个邮票的权值,求出最大的m,使得1-m都可以用不多于s的邮票数量表示出来。然后输出m最大的一组。

思路:dp[i]表示组成i需要的最少的邮票数,每次只需利用背包的思想往从前往后递推,当dp[i]>s的时候break。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int dp[1010],ans[20],val[15][15];
bool check(int a,int b)
{
    if(val[a][0]!=val[b][0])
      return val[a][0]<val[b][0];
    int i=val[a][0];
    for(;i>=1;i--)
       if(val[a][i]!=val[b][i])
         return val[a][i]<val[b][i];
    return true;
}
int main()
{
    int s,T,t,n,i,j,k,pos,f;
    bool flag;
    while(~scanf("%d",&s) && s)
    {
        scanf("%d",&T);
        for(t=1;t<=T;t++)
        {
            scanf("%d",&n);
            val[t][0]=n;
            for(i=1;i<=n;i++)
               scanf("%d",&val[t][i]);
            memset(dp,-1,sizeof(dp));
            dp[0]=0;
            for(i=1;i<=1010;i++)
            {
                for(j=1;j<=n && i-val[t][j]>=0;j++)
                {
                    if(dp[i-val[t][j]]!=-1)
                    {
                        if(dp[i]==-1)
                          dp[i]=dp[i-val[t][j]]+1;
                        else
                          dp[i]=min(dp[i],dp[i-val[t][j]]+1);
                    }
                }
                if(dp[i]>s)
                {
                    ans[t]=i-1;
                    break;
                }
            }
        }
        f=0;
        for(i=1;i<=T;i++)
        {
            if(ans[i]>f)
            {
                f=ans[i];
                pos=i;
            }
            else if(ans[i]==f && check(i,pos))
                pos=i;
        }
        printf("max coverage =%4d :",ans[pos]);
        for(i=1;i<=val[pos][0];i++)
           printf("%3d",val[pos][i]);
        printf("\n");
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值