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[][MAXN];
int s1[], s2[];
int s, n; int Judge()
{
for(int i = s1[]; i > ; 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 ;
if(d[p][q] != -) return d[p][q];
for(int i = s1[]; i > ; i--)
dp(p + , q + s1[i]);
d[p][q] = ;
} int get_max()
{
int flag;
for(int i = ; ; i++)
{
flag = ;
for(int j = ; j <= s; j++)
if(d[j][i] == ) flag = ;
if(!flag) return i - ;
}
} void put()
{
repu(i, , s2[] + ) printf("%d ", s2[i]);
puts("");
}
int main()
{
while(~scanf("%d", &s) && s)
{
scanf("%d",&n);
int maxn = ;
repu(i, , n)
{
_cle(d, -);
scanf("%d", &s1[]);
repu(j, , s1[] + ) scanf("%d", &s1[j]);
dp(, );
int t = get_max();
if(i == )
{
memcpy(s2, s1, sizeof(s1));
maxn = t;
}
else if(maxn == t)
{
if(s1[] < s2[] || (s1[] == s2[] && Judge()))
{
memcpy(s2, s1, sizeof(s1));
}
}
else if(maxn < t)
{
memcpy(s2, s1, sizeof(s1));
maxn = t;
}
}
printf("max coverage = %3d :", maxn);
repu(i, , s2[] + ) printf(" %2d", s2[i]);
puts("");
}
return ;
}
uva 242的更多相关文章
- Uva 242 邮票和信封
题目链接:https://vjudge.net/contest/146179#problem/D 题意: 信封上最多贴S张邮票.有N个邮票集合,每个集合有不同的面值.问哪个集合的最大连续邮资最大,输出 ...
- UVA - 242 线性DP
题意:给定多种邮票的组合,邮票最多只能用S张,这些邮票能组成许多不同面额,问最大连续面额的长度是多少,如果有多个组合输出组合中邮票数量最少的,如果仍有长度一致的,输出邮票从大到小排序后字典序最大的那个 ...
- 习题9-5 UVA 242
Stamps and Enovelope Size 题意: 给你最多贴S张邮票.有N个邮票集合,每个集合有不同的面值.问哪个集合的最大连续邮资最大,输出最大连续邮资和集合元素. 如果不止一个集合结果相 ...
- UVa 242 邮票和信封(完全背包)
https://vjudge.net/problem/UVA-242 题意: 输入s(每个信封能粘贴的最多邮票数量)和若干邮票组合,选出最大连续邮资最大的一个组合(最大连续邮资也就是用s张以内的邮票来 ...
- UVA - 242 Stamps and Envelope Size (完全背包+bitset)
题意:给你一些邮票面值的集合,让你选择其中一个集合,使得“能用不超过n枚集合中的邮票凑成的面值集合S中从1开始的最大连续面值”(即mex(S)-1)最大.如果有多解,输出集合大小最小的一个:如果仍有多 ...
- UVa 242 Stamps and Envelope Size (无限背包,DP)
题意:信封上最多贴S张邮票.有N个邮票集合,每个集合有不同的面值.问哪个集合的最大连续邮资最 大,输出最大连续邮资和集合元素. 最大连续邮资是用S张以内邮票面值凑1,2,3...到n+1凑不出来了,最 ...
- Stamps ans Envelope Sive UVA - 242
( ||{集合x}表示x中元素1||x中元素2||...||x的最后一个元素||(a,b)表示a||b) ans[i][j][k]表示考虑前i种邮票时取j个邮票能否得到面值kans[i][j][k]= ...
- 【Uva 242】Stamps and Envelope Size
[Link]: [Description] 给你n个集合; 每个集合都包含一些不同面额的邮票; (每种邮票都当做有无限张) 然后给你一封信上最多能贴的邮票张数S; 问你,哪一个集合的邮票; 能够贴出来 ...
- appium日志
2020-10-02 00:44:10:672 [Appium] Welcome to Appium v1.16.0 2020-10-02 00:44:10:673 [Appium] Non-defa ...
随机推荐
- servlet&jsp高级:第三部分
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- FLASH CC 2015 CANVAS (二)html中写JS调用flash中的元件、函数、变量
注意 此贴 为个人边“开荒”边写,所以不保证就是最佳做法,也难免有错误! 正式教程会在后续开始更新 当你导出第一个canvas后,你会在保存fla的文件夹里 (每个项目默认位置)看到 如下文件,(请先 ...
- git学习笔记11-git多人协作-实际多人怎么开发
当你从远程仓库克隆时,实际上Git自动把本地的master分支和远程的master分支对应起来了,并且,远程仓库的默认名称是origin. 要查看远程库的信息,用git remote: $ git r ...
- [转]-Gradle使用手册(一):为什么要用Gradle?
原文地址:http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Using-sourceCompatibility-1. ...
- [转载] tcp那些事1
原文: http://coolshell.cn/articles/11564.html TCP是一个巨复杂的协议,因为他要解决很多问题,而这些问题又带出了很多子问题和阴暗面.所以学习TCP本身是个比较 ...
- Web App时代的缓存机制新思路
Web App常见架构 以WebQQ例,WebQQ这个站点的所有内容都是一个页面里面呈现的,我们看到的类似windows操作系统的框架,是它的顶级容器和框架,由AlloyOS的内核负责统筹和管理,然后 ...
- hdu3060Area2(任意多边形相交面积)
链接 多边形的面积求解是通过选取一个点(通常为原点或者多边形的第一个点)和其它边组成的三角形的有向面积. 对于两个多边形的相交面积就可以通过把多边形分解为三角形,求出三角形的有向面积递加.三角形为凸多 ...
- 构件工具Maven----坐标、依赖、仓库、生命周期的简单学习
这篇文章对Maven中几个比较重要的概念坐标.依赖.仓库.生命周期做一个简单的介绍. 1.关于Maven坐标 用来区别Maven世界中任何一个构件,Maven坐标的元素包括groupId.artifa ...
- windows volume related concepts
Volume --- 一个volume就是一个分区. 在该“物理硬盘”上创建一个或多个分区,再创建文件系统,才可以得到一个VOLUME.此时VOLUME相对于主机是一个逻辑设备. https://m ...
- 【Todo】【读书笔记】机器学习实战(Python版)
还是把这本书的读书笔记,单独拎出来吧,因为内容比较多. P38. Logistic 回归. 觉得还蛮实用的.囫囵吞枣看的.要细看.