(动态规划 01背包 打印路径) CD --UVA --624
链接:
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87813#problem/G
每个CD的时间不超过 20
没有哪个CD的时间是超过N的
CD不能重复
每个长度和N都是一个整数
代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;
#define N 2000
#define INF 0xfffffff int v[N], dp[N][N]; void Path(int n, int W)
{
if(n==)
return ;
if(dp[n][W]==dp[n-][W])
Path(n-, W);
else
{
Path(n-, W-v[n]);
printf("%d ", v[n]);
}
} int main()
{
int W, n, i, j;
while(scanf("%d%d", &W, &n)!=EOF)
{
memset(v, , sizeof(v));
memset(dp, , sizeof(dp)); for(i=; i<=n; i++)
scanf("%d", &v[i]); for(i=; i<=n; i++)
for(j=; j<=W; j++)
{
if(j<v[i])
dp[i][j] = dp[i-][j];
else
{
dp[i][j] = max(dp[i-][j], dp[i-][j-v[i]]+v[i]);
}
} Path(n, W); printf("sum:%d\n", dp[n][W]); }
return ;
}
DP思路:dp[i] 代表第 i 个箱子在最上方的时候所摞起来的最大高度。
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<cmath>
using namespace std;
#define INF 0x3fffffff
#define maxn 100200
typedef long long LL;
struct Box
{
int L, W, H;///箱子的长宽高
Box(int LL=,int WW=,int HH=):L(LL), W(WW), H(HH)
{
if(L<W)swap(L,W);
}
bool friend operator < (Box A, Box B)
{
if(A.L != B.L)
return A.L > B.L;
if(A.W != B.W)
return A.W > B.W;
return A.H > B.H;
}
}P[maxn];
int dp[maxn];
int main()
{
int i, j, cas = , n;
while(scanf("%d",&n), n)
{
int k = , ans = ;
for(i=; i<n; i++)
{
int L, W, H;
scanf("%d %d %d",&L, &W, &H);
P[k ++] = Box(L, W, H);
P[k ++] = Box(L, H, W);
P[k ++] = Box(W, H, L);
}
sort(P, P+k); for(i=; i<k; i++)
{
dp[i] = P[i].H;
for(j=; j<i; j++)
{
if(P[i].L < P[j].L && P[i].W < P[j].W)
dp[i] = max(dp[i], dp[j] + P[i].H);
}
ans = max(ans, dp[i]);
} printf("Case %d: maximum height = %d\n", cas ++, ans);
}
return ;
}
(动态规划 01背包 打印路径) CD --UVA --624的更多相关文章
- UVA624 CD,01背包+打印路径,好题!
624 - CD 题意:一段n分钟的路程,磁带里有m首歌,每首歌有一个时间,求最多能听多少分钟的歌,并求出是拿几首歌. 思路:如果是求时常,直接用01背包即可,但设计到打印路径这里就用一个二维数组标记 ...
- uva 624 CD 01背包打印路径
// 集训最终開始了.来到水题先 #include <cstdio> #include <cstring> #include <algorithm> #includ ...
- UVA624(01背包记录路径)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)
传送门 Description Hasan and Bahosain want to buy a new video game, they want to share the expenses. Ha ...
- Codeforces Gym-102219 2019 ICPC Malaysia National E. Optimal Slots(01背包+输出路径)
题意:给你一个体积为\(T\)的背包,有\(n\)个物品,每个物品的价值和体积都是是\(a_{i}\),求放哪几个物品使得总价值最大,输出它们,并且输出价值的最大值. 题解:其实就是一个01背包输出路 ...
- UVA 624 - CD (01背包 + 打印物品)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVA 624 (0 1背包 + 打印路径)
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<ctype.h> #i ...
- HDU 6083 度度熊的午饭时光(01背包+记录路径)
http://acm.hdu.edu.cn/showproblem.php?pid=6083 题意: 思路: 01背包+路径记录. 题目有点坑,我一开始逆序枚举菜品,然后一直WA,可能这样的话路径记录 ...
- vijos 1071 01背包+输出路径
描述 过年的时候,大人们最喜欢的活动,就是打牌了.xiaomengxian不会打牌,只好坐在一边看着. 这天,正当一群人打牌打得起劲的时候,突然有人喊道:“这副牌少了几张!”众人一数,果然是少了.于是 ...
随机推荐
- Imageen 图像切割 (JpegLosslessTrans)
procedure CutAFile(FileName: string; qry: TQuery);var i: Cardinal; FromStream, ToStream: TMemorySt ...
- struts2 防止表单重复提交--令牌机制
jsp: action: 配置文件:
- 疯狂JAVA——第五章 面向对象(上)
5.1类和对象 构造器是一个类创建对象的根本途径,如果一个类没有构造器,这个类通常无法创建实例.通过new关键字来调用构造器,从而返回该类的实例. 类名:每个单词首字母大写,其他字母小写,单词之间不要 ...
- 单点登录(SSO)解决方案之 CAS客户端与Spring Security集成
接上篇:单点登录(SSO)解决方案之 CAS服务端数据源设置及页面改造 Spring Security Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制 ...
- 【英宝通Unity4.0公开课学习 】(二)场景创建
本讲共四节,貌似讲课老师的速度变快了,2倍速听不清了...调成了1.7倍...老师果然越来越熟练了啊! 而且最开始的萌妹纸也不再出现在视频里了,我当时还想着完全可以换成老师自己提问嘛! 不过有妹纸声音 ...
- RigidBody组件的Is Kinematic
RigidBody组件的Is Kinematic属性打上勾(设为true,使其不受物理引擎驱动,Wall是为了防止其移动,Person是为了防止其受到力不断旋转—看的心塞=v=) .is kinema ...
- Spring配置连接池
---------------------siwuxie095 Spring 配置连接池 1.Spring 配置内置连接 ...
- 不同的子序列 · Distinct Subsequences
[抄题]: 给出字符串S和字符串T,计算S的不同的子序列中T出现的个数. 子序列字符串是原始字符串通过删除一些(或零个)产生的一个新的字符串,并且对剩下的字符的相对位置没有影响.(比如,“ACE”是“ ...
- Spring框架的AOP技术(注解方式)
1. 步骤一:创建JavaWEB项目,引入具体的开发的jar包 * 先引入Spring框架开发的基本开发包 * 再引入Spring框架的AOP的开发包 * spring的传统AOP的开发的包 * sp ...
- count(distinct) 与group by 浅析
x在传统关系型数据库中,group by与count(distinct)都是很常见的操作.count(distinct colA)就是将colA中所有出现过的不同值取出来,相信只要接触过数据库的同学都 ...