输入输出:

id=26127" style="color:blue; text-decoration:none">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

id=26127" style="color:blue; text-decoration:none">Sample
Output

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

题意:

最多用S个邮票拼出最大连续数值,每组共同拥有N组例子,每组第一个是数组元素个数,输出字典序最小的一组。

用二维的状态事实上就能够了,i,j,k分别表示取到数组前i位用j个邮票能否拼出k,感觉有点像背包,输入并不都是排好序的,须要排序。

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<cstring>
#include<vector>
#include<algorithm>
#define INF 0X3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std; typedef long long ll;
typedef unsigned long long llu;
const int maxd=10+5;
int n,s;
vector<int> st[maxd];
int dp[maxd][1005]; bool ok(int x)
{
for(int j=0; j<=s; ++j)
if(dp[j][x])
return true;
return false;
} bool cmp(const vector<int> &a ,const vector<int> &b)
{
int lena=a.size(),lenb=b.size();
if(lena==lenb)
for(int i=lena-1;i>=0;--i)
if(a[i]!=b[i]) return a[i]<b[i];
return lena<lenb;
} int solve(int x)
{
mem(dp,0); int len=st[x].size();
int c=st[x][len-1]*s; for(int j=0; j<=s; ++j) dp[j][0]=1;
// for(int i=0; i<=len; ++i) dp[i][0][0]=1; for(int i=1; i<=len; ++i)
{
// memcpy(dp[i],dp[i-1],sizeof(dp[i]));
for(int j=1; j<=s; ++j)
{
int w=j*st[x][i-1]; for(int m=j; m<=s; ++m)
for(int k=w; k<=c; ++k)
if(dp[m-j][k-w])
dp[m][k]=1;
}
} int ans=0;
for(int i=1; i<=c; ++i)
{
if(ok(i)) ans=i;
else break;
} return ans; } int main()
{
freopen("1.txt","r",stdin);
while(scanf("%d",&s)==1 && s)
{
scanf("%d",&n);
for(int i=0; i<n; ++i)
{
int x,tmp;
st[i].clear();
scanf("%d",&x);
for(int j=0; j<x; ++j)
scanf("%d",&tmp),st[i].push_back(tmp);
sort(st[i].begin(),st[i].end());
}
sort(st,st+n,cmp); int ans=0,pos=-1;
for(int i=0; i<n; ++i)
{
int tmp=solve(i);
// cout<<tmp<<endl;
if(tmp>ans)
{
ans=tmp;
pos=i;
}
} printf("max coverage =%4d :",ans);
for(int i=0; i<st[pos].size(); ++i)
printf("%3d",st[pos][i]);
printf("\n");
}
return 0;
}

uva--242(邮资问题 dp)的更多相关文章

  1. UVA.674 Coin Change (DP 完全背包)

    UVA.674 Coin Change (DP) 题意分析 有5种硬币, 面值分别为1.5.10.25.50,现在给出金额,问可以用多少种方式组成该面值. 每种硬币的数量是无限的.典型完全背包. 状态 ...

  2. uva 10817(数位dp)

    uva 10817(数位dp) 某校有m个教师和n个求职者,需讲授s个课程(1<=s<=8, 1<=m<=20, 1<=n<=100).已知每人的工资c(10000 ...

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

    题意:信封上最多贴S张邮票.有N个邮票集合,每个集合有不同的面值.问哪个集合的最大连续邮资最 大,输出最大连续邮资和集合元素. 最大连续邮资是用S张以内邮票面值凑1,2,3...到n+1凑不出来了,最 ...

  4. UVA - 242 线性DP

    题意:给定多种邮票的组合,邮票最多只能用S张,这些邮票能组成许多不同面额,问最大连续面额的长度是多少,如果有多个组合输出组合中邮票数量最少的,如果仍有长度一致的,输出邮票从大到小排序后字典序最大的那个 ...

  5. uva 242

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

  6. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  7. Uva 242 邮票和信封

    题目链接:https://vjudge.net/contest/146179#problem/D 题意: 信封上最多贴S张邮票.有N个邮票集合,每个集合有不同的面值.问哪个集合的最大连续邮资最大,输出 ...

  8. uva 10453 - Make Palindrome(dp)

    题目链接:10453 - Make Palindrome 题目大意:给出一个字符串,通过插入字符使得原字符串变成一个回文串,要求插入的字符个数最小,并且输出最后生成的回文串. 解题思路:和uva 10 ...

  9. uva 10671 - Grid Speed(dp)

    题目链接:uva 10671 - Grid Speed 题目大意:给出N,表示在一个N*N的网格中,每段路长L,如今给出h,v的限制速度,以及起始位置sx,sy,终止位置ex,ey,时间范围st,et ...

  10. uva 1331 - Minimax Triangulation(dp)

    option=com_onlinejudge&Itemid=8&page=show_problem&category=514&problem=4077&mosm ...

随机推荐

  1. HTTP和Socket的区别

    1: HTTP协议即超文本传送协议(Hypertext Transfer Protocol ),是Web联网的基础,也是手机联网常用的协议之一,HTTP协议是建立在TCP协议之上的一种应用. HTTP ...

  2. PHP MySQL -处理语句

    PHP MySQL 预处理语句 预处理语句对于防止 MySQL 注入是非常有用的. 预处理语句及绑定参数 预处理语句用于执行多个相同的 SQL 语句,并且执行效率更高. 预处理语句的工作原理如下: 预 ...

  3. 二叉查找树实现实例(C语言)

    /* search_tree.h */ #ifndef _SEARCH_TREE_H #define _SEARCH_TREE_H struct tree_node; typedef struct t ...

  4. Apache+Tomcat负载均衡集群搭建

    1.所需软件 apache_2.2.4-win32-x86-no_ssl.apacheserver mod_jk-apache-2.2.4连接器,连接apache和tomcat apache-tomc ...

  5. eclipse to avoid the message, disable the...

      标题 CreateTime--2018年5月9日10:38:15 Author:Marydon 1.问题描述 2.问题解析 这是因为eclipse的智能提示超时引起的,将超时间调大即可,如:200 ...

  6. spring mvc json的输入输出

    输入 前台代码: var cfg = { type: 'POST', data: JSON.stringify({userName:'winzip',password:'password',mobil ...

  7. 【laravel5.4+vue.js】laravel 循环三维数组,解决:htmlentities() expects parameter 1 to be string, array given

    laravel循环三维数组   +++   vue.js循环三维数组  (数据均是以三维数组形式存在的) <form-item label="权限名称" prop=" ...

  8. aop注解 事例

    spring.xml中aop的配置 <!-- aop的配置 --> <aop:config> <!-- 切入点表达式 --> <aop:pointcut ex ...

  9. HttpClient中的Timout

    connection timeout和SoTimeout A connection timeout occurs only upon starting the TCP connection. This ...

  10. 常见Web前端开发笔试题

    1.什么是web标准? WEB标准不是某一个标准,而是一系列标准的集合.网页主要由三部分组成:结构(Structure).表现(Presentation)和行为 (Behavior). 对应的标准也分 ...