为了方便打印路径,考虑从下往上转移。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(背包)的更多相关文章

  1. 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 ...

  2. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

  3. UVA 10564 - Paths through the Hourglass (dp)

    本文出自   http://blog.csdn.net/shuangde800 题目传送门 题意: 给一个相上面的图.要求从第一层走到最下面一层,只能往左下或右下走,经过的数字之和为sum. 问有多少 ...

  4. UVA - 10564 Paths through the Hourglass

    传送门:https://vjudge.net/problem/UVA-10564 题目大意:给你一张形如沙漏一般的图,每一个格子有一个权值,问你有多少种方案可以从第一行走到最后一行,并且输出起点最靠前 ...

  5. UVA 10564_ Paths through the Hourglass

    题意: 由0-9的数字组成一个形如沙漏的图形,要求从第一行开始沿左下或者右下到达最后一行,问有多少种不同的路径,使最后路径上的整数之和为给定的某个数. 分析: 简单计数dp,从最后一行开始,设dp[i ...

  6. UVA 10564 十 Paths through the Hourglass

     Paths through the Hourglass Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & % ...

  7. uva 10564

    Problem FPaths through the HourglassInput: Standard Input Output: Standard Output Time Limit: 2 Seco ...

  8. UVa 10564 DP Paths through the Hourglass

    从下往上DP,d(i, j, k)表示第(i, j)个格子走到底和为k的路径条数. 至于字典序最小,DP的时候记录一下路径就好. #include <cstdio> #include &l ...

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

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

随机推荐

  1. laravel 常见操作

    1.url()

  2. 使用js页面添加或删除标签

    // 添加var container = document.getElementById('divAudio');container.appendChild(audio); // 删除var cont ...

  3. Java基础--正则表达式的规则

    注意:正则表达式只关注格式是否正确,不关注内容是否有效. 一.字符集合, []表示一个字符. 1.[abc] :指a,b,c中的任意一个字符. 2.[^abc]:指除了a,b,c外的任意字符. 3.[ ...

  4. chapter03

    import scala.collection.mutable.ArrayBuffer /** * Created by EX-CHENZECHAO001 on 2018-03-29. */class ...

  5. Nologging操作对standby的影响

    1.primary 首先要设置为 force_log mode ,然后再做备份,在应用到备库上. 2.switch over 之前需要检查v$database_block_corruption  视图 ...

  6. rancher中级(二)(rancher中添加证书及操作虚拟主机)

    制作一个ssl证书 首先了解关于ssl证书的背景知识:http://www.cnblogs.com/zxj015/p/4458066.html SSL证书包括: 1,CA证书,也叫根证书或者中间级证书 ...

  7. 牛客网Java刷题知识点之四种不同的方式创建线程

    不多说,直接上干货! 有4种方式可以用来创建线程: 第一种:继承Thread类,重写run方法 第二种:实现Runnable接口,并实现该接口的run方法(一般我们在编程的时候推荐用这种) 第三种:实 ...

  8. ElasticSearch 全文检索— ElasticSearch 安装部署

    ElasticSearch 规划-集群规划 ElasticSearch 规划-集群规划 ElasticSearch 规划-用户规划 ElasticSearch 规划-目录规划 ElasticSearc ...

  9. Java规则引擎drools:drt动态生成规则并附上具体项目逻辑

    一 整合 由于本人的码云太多太乱了,于是决定一个一个的整合到一个springboot项目里面. 附上自己的项目地址https://github.com/247292980/spring-boot 以整 ...

  10. asp.net后台调用前台js代码

    为了通过ajax异步获取数据,我通过使用jquery的$(function{})方法,在页面加载时发送ajax请求,获取相关的数据.但是遇到了一个问题,当我发送的ajax请求需要根据传过来的URL参数 ...