uva 242

242 - Stamps and Envelope Size

Time limit: 3.000 seconds

 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":

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 S denominations 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

记忆化搜索
一开始读错题了,以至于想得非常复杂,后来终于读明白了,比较简单的记搜。
d[i][j]表示用i张邮票凑成邮资j。
注意输出的格式。反正我是pe了。

#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define ll long long
#define _cle(m, a) memset(m, a, sizeof(m))
#define repu(i, a, b) for(int i = a; i < b; i++)
#define MAXN 1001
int d[11][MAXN];
int s1[11], s2[11];
int s, n;

int Judge()
{
    for(int i = s1[0]; i > 0; i--)
        if(s1[i] < s2[i]) return true;
        else if(s1[i] < s2[i]) ;
        else return false;
    return false;
}

int dp(int p, int q)
{
    if(p > s) return 0;
    if(d[p][q] != -1) return d[p][q];
    for(int i = s1[0]; i > 0; i--)
        dp(p + 1, q + s1[i]);
    d[p][q] = 1;
}

int get_max()
{
    int flag;
    for(int i = 1; ; i++)
    {
        flag = 0;
        for(int j = 1; j <= s; j++)
            if(d[j][i] == 1) flag = 1;
        if(!flag) return i - 1;
    }
}

void put()
{
    repu(i, 0, s2[0] + 1) printf("%d ", s2[i]);
    puts("");
}
int main()
{
    while(~scanf("%d", &s) && s)
    {
        scanf("%d",&n);
        int maxn = 0;
        repu(i, 0, n)
        {
            _cle(d, -1);
            scanf("%d", &s1[0]);
            repu(j, 1, s1[0] + 1) scanf("%d", &s1[j]);
            dp(0, 0);
            int t = get_max();
            if(i == 0)
            {
                memcpy(s2, s1, sizeof(s1));
                maxn = t;
            }
            else if(maxn == t)
            {
                if(s1[0] < s2[0] || (s1[0] == s2[0] && Judge()))
                {
                    memcpy(s2, s1, sizeof(s1));
                }
            }
            else if(maxn < t)
            {
                memcpy(s2, s1, sizeof(s1));
                maxn = t;
            }
        }
        printf("max coverage = %3d :", maxn);
        repu(i, 1, s2[0] + 1) printf(" %2d", s2[i]);
        puts("");
    }
    return 0;
}

时间: 2024-11-03 01:22:42

uva 242的相关文章

UVa 242 邮票和信封(完全背包)

https://vjudge.net/problem/UVA-242 题意: 输入s(每个信封能粘贴的最多邮票数量)和若干邮票组合,选出最大连续邮资最大的一个组合(最大连续邮资也就是用s张以内的邮票来凑1,2,3,4...n,如果无法凑成n+1,那么最大值也就是n了).如果有多个最大值,则优先考虑邮票数少的,其次考虑邮票面值最大的那个更小的. 思路: 完全背包问题. 完全背包是物品无限,在这里和题意相符合,每种邮票也是可以无限使用的.最大连续邮资就相当于一个背包容量,d[i]表示当最大连续邮资为

UVa 242 Stamps and Envelope Size (无限背包,DP)

题意:信封上最多贴S张邮票.有N个邮票集合,每个集合有不同的面值.问哪个集合的最大连续邮资最 大,输出最大连续邮资和集合元素. 最大连续邮资是用S张以内邮票面值凑1,2,3...到n+1凑不出来了,最大连续邮资就是n.如果不止一个集合结果相 同,输出集合元素少的, 如果仍相同,输出最大面值小的. 析:这个题,紫书上写的不全,而且错了好几次,结果WA好几次. 首先这个和背包问题差不多,我们只用一维就好.dp[i]表示邮资为 i 时的最小邮票数,然后,如果dp[i] > s就该结束了. 其他的就很简

UVa 242 - Stamps and Envelope Size(DP)

给出一个s,然后给出n组邮票,问那一组可以凑出最大连续邮资. 对每一组邮票,求出当邮资为i时需要邮票数的最小值d[i],边界为d[0]=0.d[i]>s时break.类似于背包问题的求法,具体方法见代码. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn=1010; int d[maxn],ans[20],num[20],a[20][

uva 704

自己之前的不见了.. 这题是双向广搜即可过.. 1 // Colour Hash (色彩缤纷游戏) 2 // PC/UVa IDs: 110807/704, Popularity: B, Success rate: average Level: 3 3 // Verdict: Accepted 4 // Submission Date: 2011-08-28 5 // UVa Run Time: 0.048s 6 // 7 // 版权所有(C)2011,邱秋.metaphysis # yeah

Fast Matrix Operations(UVA)11992

UVA 11992 - Fast Matrix Operations 给定一个r*c(r<=20,r*c<=1e6)的矩阵,其元素都是0,现在对其子矩阵进行操作. 1 x1 y1 x2 y2 val 表示将(x1,y1,x2,y2)(x1<=x2,y1<=y2)子矩阵中的所有元素add上val: 2 x1 y1 x2 y2 val 表示将(x1,y1,x2,y2)(x1<=x2,y1<=y2)子矩阵中的所有元素set为val: 3 x1 y1 x2 y2 val 表示输

UVa 568 Just the Facts

A过去后看了一下别人的解法,发现除了打表还有一种数论的方法. 分析一下阶乘后面的0是怎么出现的呢,当然是2乘5得到的. 我们将1~N先放在一个数组里面. 从数组第一个元素开始,先统计一下N!中因子为5的个数记为count,将其除去,然后再除去count个2.这样一来的话把所有元素乘起来后就不会出现10的倍数了. 当然并不是真正的乘起来,那样的话肯定是要溢出的,因为只关心最后一位数,所以每次乘完后求10的余数即可. 我的做法是打表,因为题目里给了N <= 10000的条件限制,所以可以把1~100

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

UVA 10341 Solve It

Problem F Solve It Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and te

UVA 11014 - Make a Crystal(容斥原理)

UVA 11014 - Make a Crystal 题目链接 题意:给定一个NxNxN的正方体,求出最多能选几个整数点.使得随意两点PQ不会使PQO共线. 思路:利用容斥原理,设f(k)为点(x, y, z)三点都为k的倍数的点的个数(要扣掉一个原点O).那么全部点就是f(1),之后要去除掉共线的,就是扣掉f(2), f(3), f(5)..f(n).n为素数.由于这些素数中包括了合数的情况,而且这些点必定与f(1)除去这些点以外的点共线,所以扣掉.可是扣掉后会扣掉一些反复的.比方f(6)在f