UVA 10564 Paths through the Hourglass(背包)
为了方便打印路径,考虑从下往上转移。dp[i][j][S]表示在i行j列总和为S的方案,
dp[i][j][S] = dp[i+1][left][S-x]+dp[i+1][right][S-x]
方案O(2^2*n-1),结果要用long long保存。
#include<bits/stdc++.h>
using namespace std; typedef long long ll;
const int maxn = ,maxs = ;
int hg[maxn<<][maxn];
ll dp[maxn<<][maxn][maxs]; //#define LOCAL
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif
int n, S;
while(scanf("%d%d", &n, &S) ,n){
int rc = ;
for(int i = n; i > ; i--){
int *h = hg[rc++];
for(int j = ; j <= i; j++){
scanf("%d",h+j);
}
}
for(int i = ; i <= n; i++){
int *h = hg[rc++];
for(int j = ; j <= i; j++){
scanf("%d",h+j);
}
}
rc--;
memset(dp,,sizeof(dp));
for(int j = ; j <= n; j++){
dp[rc][j][hg[rc][j]] = ;
}
while(rc>=n){
for(int j = , mj = (rc-- + ) - n; j <= mj; j++){
int x = hg[rc][j];
ll *f = dp[rc][j];
for(int s = S-x; s >= ; s--){
f[s+x] = dp[rc+][j][s]+dp[rc+][j+][s];
}
}
}
for(int i = ; rc--,i <= n; i++){
for(int j = ; j <= i; j++){
int x = hg[rc][j];
ll *f = dp[rc][j];
for(int s = S-x; s >= ; s--){
f[s+x] = dp[rc+][j-][s]+dp[rc+][j][s];
}
}
}
ll ans = ;
int index = ;
for(int j = ; j <= n; j++){
if(dp[][j][S]){
ans += dp[][j][S];
if(!index) index = j;
}
}
printf("%lld\n",ans);
if(ans){
int j = index, s = S ;
printf("%d ",j-);
rc = n;
for(int i = ; i < rc; i++){
s -= hg[i-][j];
if(dp[i][j-][s]){
putchar('L');
j--;
}else {
putchar('R');
} }
rc = *n-;
for(int i = n; i < rc; i++){
s -= hg[i-][j];
if(dp[i][j][s]){
putchar('L');
}else {
j++;
putchar('R');
}
}
}
puts("");
}
return ;
}
UVA 10564 Paths through the Hourglass(背包)的更多相关文章
- 01背包(类) UVA 10564 Paths through the Hourglass
题目传送门 /* 01背包(类):dp[i][j][k] 表示从(i, j)出发的和为k的方案数,那么cnt = sum (dp[1][i][s]) 状态转移方程:dp[i][j][k] = dp[i ...
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
- UVA 10564 - Paths through the Hourglass (dp)
本文出自 http://blog.csdn.net/shuangde800 题目传送门 题意: 给一个相上面的图.要求从第一层走到最下面一层,只能往左下或右下走,经过的数字之和为sum. 问有多少 ...
- UVA - 10564 Paths through the Hourglass
传送门:https://vjudge.net/problem/UVA-10564 题目大意:给你一张形如沙漏一般的图,每一个格子有一个权值,问你有多少种方案可以从第一行走到最后一行,并且输出起点最靠前 ...
- UVA 10564_ Paths through the Hourglass
题意: 由0-9的数字组成一个形如沙漏的图形,要求从第一行开始沿左下或者右下到达最后一行,问有多少种不同的路径,使最后路径上的整数之和为给定的某个数. 分析: 简单计数dp,从最后一行开始,设dp[i ...
- UVA 10564 十 Paths through the Hourglass
Paths through the Hourglass Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & % ...
- uva 10564
Problem FPaths through the HourglassInput: Standard Input Output: Standard Output Time Limit: 2 Seco ...
- UVa 10564 DP Paths through the Hourglass
从下往上DP,d(i, j, k)表示第(i, j)个格子走到底和为k的路径条数. 至于字典序最小,DP的时候记录一下路径就好. #include <cstdio> #include &l ...
- UVA.674 Coin Change (DP 完全背包)
UVA.674 Coin Change (DP) 题意分析 有5种硬币, 面值分别为1.5.10.25.50,现在给出金额,问可以用多少种方式组成该面值. 每种硬币的数量是无限的.典型完全背包. 状态 ...
随机推荐
- Codeforces Round #558 (Div. 2)B(SET,模拟)
#include<bits/stdc++.h>using namespace std;int a[100007];int cnt[100007];int main(){ int n; ...
- PostFX v2后期处理特效包:升级更惊艳的视觉效果
https://mp.weixin.qq.com/s/BMkLLuagbhRSWspzeGhK7g Post-Processing Stack后期处理特效包能够轻松创建和调整高质量视觉效果,实现更为惊 ...
- 线段树 洛谷P3932 浮游大陆的68号岛
P3932 浮游大陆的68号岛 题目描述 妖精仓库里生活着黄金妖精们,她们过着快乐,却随时准备着迎接死亡的生活. 换用更高尚的说法,是随时准备着为这个无药可救的世界献身. 然而孩子们的生活却总是无忧无 ...
- ch8 -- useLK
useLK 光流法跟踪FAST角点 执行 ./useLK ../../data 运行程序. 光流法需要include<opencv2/video/tracking.hpp>,用到列表 ...
- 使用命令安装vue插件
使用命令npm install element-ui --save-dev 安装element-ui. --save-dev表示自动添加配置依赖到package.json文件的devDependenc ...
- DSL与GPL
一.DSL 与 GPL DSL(Domain-Specified Language 领域特定语言),而与 DSL 相对的就是 GPL,最常见的 DSL 包括 Regex 以及 HTML & C ...
- poj2186-Popular Cows(强连通分支)
有N(N<=10000)头牛,每头牛都想成为most poluler的牛,给出M(M<=50000)个关系,如(1,2)代表1欢迎2,关系可以传递,但是不可以相互,即1欢迎2不代表2欢迎1 ...
- js 监听浏览器刷新还是关闭事件
转载大神 http://www.cnblogs.com/gavin0517/p/5827405.html
- Spring Cloud Ribbon负载均衡
目录 一.简介 二.客户端负载均衡 三.RestTemplate详解 GET请求 POST请求 PUT请求 DELETE请求 一.简介 Spring Cloud Ribbon是一个基于HTTP 和 ...
- SpringBoot | 第十章:Swagger2的集成和使用
前言 前一章节介绍了mybatisPlus的集成和简单使用,本章节开始接着上一章节的用户表,进行Swagger2的集成.现在都奉行前后端分离开发和微服务大行其道,分微服务及前后端分离后,前后端开发的沟 ...