题目大意:给一些邮票的面值组合,找出在限定的张数范围内能组合出连续最大值得那个组合。

题目分析:状态可以这样定义:dp(k,u)表示u能否用k张邮票组合成。状态转移方程很显然了。

代码如下:

# include<iostream>
# include<cstdio>
# include<cstring>
# include<algorithm>
using namespace std; int a[12][12];
int dp[12][1005]; bool dfs(int p,int s,int u)
{
if(dp[s][u]!=-1) return dp[s][u];
if(!u) return dp[s][u]=1;
if(!s) return dp[s][u]=0; for(int i=1;i<=a[p][0];++i)
if(u>=a[p][i]&&dfs(p,s-1,u-a[p][i]))
return dp[s][u]=1; dp[s][u]=0;
return false;
} int judge(int i,int j)
{
if(a[i][0]<a[j][0]) return i;
if(a[j][0]<a[i][0]) return j; for(int k=a[i][0];k>=1;--k){
if(a[i][k]<a[j][k]) return i;
if(a[j][k]<a[i][k]) return j;
}
return i;
} int main()
{
int s,n;
while(scanf("%d",&s)&&s)
{
scanf("%d",&n); int ans=0,id=0;
for(int i=1;i<=n;++i){
scanf("%d",&a[i][0]);
for(int j=1;j<=a[i][0];++j)
scanf("%d",&a[i][j]); memset(dp,-1,sizeof(dp));
int k=1;
while(dfs(i,s,k)) ++k; if(k-1>ans)
ans=k-1,id=i;
else if(k-1==ans)
id=judge(id,i);
}
printf("max coverage =%4d :",ans);
for(int i=1;i<=a[id][0];++i)
printf("%3d",a[id][i]);
printf("\n");
}
return 0;
}

  

UVA-242 Stamps and Envelope Size (DP)的更多相关文章

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

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

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

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

  3. 【Uva 242】Stamps and Envelope Size

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

  4. Stamps and Envelope Size

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

  5. uva242,Stamps and Envelope Size

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

  6. uva 242

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

  7. 习题9-5 UVA 242

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

  8. UVA 10163 Storage Keepers(两次DP)

    UVA 10163 Storage Keepers(两次DP) http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Ite ...

  9. uva 11584 Partitioning by Palindromes 线性dp

    // uva 11584 Partitioning by Palindromes 线性dp // // 题目意思是将一个字符串划分成尽量少的回文串 // // f[i]表示前i个字符能化成最少的回文串 ...

随机推荐

  1. python之路----socketserver模块

    socketserver import socketserver class MyServer(socketserver.BaseRequestHandler): def handle(self): ...

  2. OpenGL边用边学------2 经典照相机模型

    https://blog.csdn.net/smstong/article/details/50290327 实际照相步骤 1 布置场景和调整照相机位置 3 选择镜头对焦Focus 4 按下快门 5 ...

  3. 20145329 《网络对抗技术》浏览器MS11_050安全漏洞攻击

    两台虚拟机: kali ip:192.168.96.130 windows xp sp3(包含IE7)ip:192.168.96.128 1.在kali终端中开启msfconsole. 2.进入漏洞模 ...

  4. np.random.normal()正态分布

    高斯分布的概率密度函数 numpy中 numpy.random.normal(loc=0.0, scale=1.0, size=None) 参数的意义为: loc:float 概率分布的均值,对应着整 ...

  5. noip 2012 提高组 day2 部分题解

    这道题有多种解法,我用的是扩展欧几里得算法求到的答案 #include<iostream> #include<fstream> #include<cstdio> u ...

  6. TCP客户端【TcpClient】

    一.阻塞模式 1.命名空间 System.Net.Sockets 2.对象声明 TcpClient dpu1TcpClient = null;//dpu1tcp客户端,TcpClient模式 Netw ...

  7. Python3基础 函数 有参数有返回值 对传入的参数加1

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  8. linux下连接无线网出现nl80211: Could not configure driver mode nl80211: deinit ifname=wlan1 disabled_11b_rates=0 wlan1: Failed to initialize driver interface

    一.背景1.1 jello@jello:~$ lsb_release -aNo LSB modules are available.Distributor ID:    UbuntuDescripti ...

  9. P1600 天天爱跑步

    lca真心不太会,这里只介绍60分做法,100的太难辣简单了就不介绍了 n<=1000 zz回溯爆搜 S[i]全部相等 这dfs序都不用lca的,2333,差分,然后输出判断一下是否是0(1到i ...

  10. [微信开发] - 微信支付 JSAPI 形式

    微信官方的JSAPI文档 微信官方的JSAPI支付SDK与DEMO下载 查看JSAPI的API可以从这里看 下载了支付DEMO其实有些地方不对的,比如如果做沙盒测试的时候,需要使用getsignkey ...