题意:信封上最多贴S张邮票。有N个邮票集合,每个集合有不同的面值。问哪个集合的最大连续邮资最 大,输出最大连续邮资和集合元素。

最大连续邮资是用S张以内邮票面值凑1,2,3...到n+1凑不出来了,最大连续邮资就是n。如果不止一个集合结果相 同,输出集合元素少的,

如果仍相同,输出最大面值小的。

析:这个题,紫书上写的不全,而且错了好几次,结果WA好几次。

首先这个和背包问题差不多,我们只用一维就好。dp[i]表示邮资为 i 时的最小邮票数,然后,如果dp[i] > s就该结束了。

其他的就很简单了,主要是我没理解题意。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 15 + 5;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
int a[maxn][maxn];
int dp[1024];
int ans[maxn]; int main(){
while(scanf("%d", &n) == 1 && n){
scanf("%d", &m);
for(int i = 0; i < m; ++i){
scanf("%d", &a[i][0]);
for(int j = 1; j <= a[i][0]; ++j)
scanf("%d", &a[i][j]);
fill(dp, dp+1024, INF);
dp[0] = 0;
for(int j = 1; j < 1024; ++j){
for(int k = 1; k <= a[i][0] && j-a[i][k] >= 0; ++k){
dp[j] = Min(dp[j], dp[j-a[i][k]]+1);
}
if(dp[j] > n){ ans[i] = j-1; break; }
}
} int anss = -1, cnt = 0;
for(int i = 0; i < m; ++i){
if(ans[i] > anss){ anss = ans[i]; cnt = i; }
else if(ans[i] == anss && a[i][0] < a[cnt][0]) cnt = i;
else if(ans[i] == anss && a[i][0] == a[cnt][0]){
bool ok = false;
for(int j = a[i][0]; j >= 0; --j)
if(a[i][j] < a[cnt][j]){ ok = true; break; }
else if(a[i][j] > a[cnt][j]) break;
if(ok) cnt = i;
}
}
printf("max coverage =%4d :", anss);
for(int i = 1; i <= a[cnt][0]; ++i) printf("%3d", a[cnt][i]);
printf("\n");
}
return 0;
}

UVa 242 Stamps and Envelope Size (无限背包,DP)的更多相关文章

  1. UVA - 242 Stamps and Envelope Size (完全背包+bitset)

    题意:给你一些邮票面值的集合,让你选择其中一个集合,使得“能用不超过n枚集合中的邮票凑成的面值集合S中从1开始的最大连续面值”(即mex(S)-1)最大.如果有多解,输出集合大小最小的一个:如果仍有多 ...

  2. 【Uva 242】Stamps and Envelope Size

    [Link]: [Description] 给你n个集合; 每个集合都包含一些不同面额的邮票; (每种邮票都当做有无限张) 然后给你一封信上最多能贴的邮票张数S; 问你,哪一个集合的邮票; 能够贴出来 ...

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

    https://vjudge.net/problem/UVA-242 题意: 输入s(每个信封能粘贴的最多邮票数量)和若干邮票组合,选出最大连续邮资最大的一个组合(最大连续邮资也就是用s张以内的邮票来 ...

  4. uva242,Stamps and Envelope Size

    这题紫薯上翻译错了 应该是:如果有多个并列,输出邮票种类最少的那个,如果还有并列,输出最大面值最小的那个 坑了我一个下午 dp[p][q]==1表示可以用不超过q张组成面额p 结合记忆化,p从1开始枚 ...

  5. Stamps and Envelope Size

    题意: 容量为s的信封,给n组邮票的面值,求哪一组能组成的连续的面值的最大值最大,若有多组答案,输出面值数量最小的一组,若数量相等,输出最大面值最小的一组,若最大面值相等,输出第二大面值最小的一组,依 ...

  6. UVA-242 Stamps and Envelope Size (DP)

    题目大意:给一些邮票的面值组合,找出在限定的张数范围内能组合出连续最大值得那个组合. 题目分析:状态可以这样定义:dp(k,u)表示u能否用k张邮票组合成.状态转移方程很显然了. 代码如下: # in ...

  7. uva 242

    242 - Stamps and Envelope Size Time limit: 3.000 seconds  Stamps and Envelope Size  Philatelists hav ...

  8. 习题9-5 UVA 242

    Stamps and Enovelope Size 题意: 给你最多贴S张邮票.有N个邮票集合,每个集合有不同的面值.问哪个集合的最大连续邮资最大,输出最大连续邮资和集合元素. 如果不止一个集合结果相 ...

  9. BZOJ 1677 [Usaco2005 Jan]Sumsets 求和:dp 无限背包 / 递推【2的幂次方之和】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1677 题意: 给定n(n <= 10^6),将n分解为2的幂次方之和,问你有多少种方 ...

随机推荐

  1. [概率dp] hdu 5378 Leader in Tree Land

    题意: 给你一颗以1位根节点的树.我们定义对于每一个子树,节点权值最大的权值记为这个子树的权值,为你将1~n放到这个树里 满足最大权值仅仅有k个的组合数是多少. 思路: 我们能够知道以每一个节点为子树 ...

  2. Canvas学习笔记——动画中的三角学

    示例1,跟随鼠标的键头:   需要掌握一个重要的公式,这个方法返回从 x 轴到点 (x,y) 之间的角度 Math.atan2(dy,dx); 关键代码: function Arrow() { thi ...

  3. 二进制安装Mysql 5.6(免编译)

    安装系统基础软包 yum install -y make bc perl gcc openssl openssl-devel ncurses ncurses-devel 安装方式:二进制免编译安装 查 ...

  4. LookAround开元之旅

    http://blog.csdn.net/lancees/article/details/17696805

  5. 【BZOJ3197】[Sdoi2013]assassin 树同构+动态规划+KM

    [BZOJ3197][Sdoi2013]assassin Description Input Output Sample Input 4 1 2 2 3 3 4 0 0 1 1 1 0 0 0 Sam ...

  6. live555 RTSP推送到Darwin出现404错误的解决

    我们将Darwin部署到公网,接收live555 RTSP/RTP推送的时候,经常会出现在SETUP步骤Darwin返回404错误,经过查找原因,主要是Darwin对live555推送的sdp信息中的 ...

  7. java手写单例模式

    1 懒汉模式 public class Singleton { private Singleton singleton = null; private Singleton() { } public S ...

  8. java中两字符串比较--compareTo方法

    java.lang.String.compareTo() 方法比较两个字符串的字典,比较是基于字符串中的每个字符的Unicode值 String n1 = "1"; String ...

  9. WIN7系统设置wifi

    *&->20170302 112700 WIN7系统设置wifi, 开启win7的隐藏功能,即虚拟wifi功能和虚拟无线AP功能,即可实现将电脑变成wifi 供无线上网, 1.开始-命令 ...

  10. Machine Learning in Action(6) AdaBoost算法

    Adaboost也是一种原理简单,但很实用的有监督机器学习算法,它是daptive boosting的简称.说到boosting算法,就不得提一提bagging算法,他们两个都是把一些弱分类器组合起来 ...