为了方便打印路径,考虑从下往上转移。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. Java8 使用 stream().map()提取List对象的某一列值及排重

    List对象类(StudentInfo) public class StudentInfo implements Comparable<StudentInfo> { //名称 privat ...

  2. CF 979D Kuro and GCD and XOR and SUM(异或 Trie)

    CF 979D Kuro and GCD and XOR and SUM(异或 Trie) 给出q(<=1e5)个操作.操作分两种,一种是插入一个数u(<=1e5),另一种是给出三个数x, ...

  3. Node.js 内置模块crypto加密模块(4) Diffie Hellman

    Diffie-Hellman( DH ):密钥交换协议/算法 ( Diffie-Hellman Key Exchange/Agreement Algorithm ) 百科摘录: Diffie-Hell ...

  4. springboot批量导入excel数据

    1 背景 小白今天闲着没事,在公司摸鱼,以为今天有事无聊的一天,突然上头说小子,今天实现一下批量导入Excel数据吧,当时我的内心是拒绝的,然后默默打开idea. 2 介绍 2.1 框架 java本身 ...

  5. [AHOI2009]飞行棋 BZOJ1800

    题目描述 给出圆周上的若干个点,已知点与点之间的弧长,其值均为正整数,并依圆周顺序排列. 请找出这些点中有没有可以围成矩形的,并希望在最短时间内找出所有不重复矩形. 输入输出格式 输入格式: 第一行为 ...

  6. props简单小栗子

    props简单小栗子 可以直接copy查看结果 <!DOCTYPE html> <html lang="en"> <head> <meta ...

  7. P2066 机器分配 (DP+DP输出)

    题目描述 总公司拥有高效设备M台,准备分给下属的N个分公司.各分公司若获得这些设备,可以为国家提供一定的盈利.问:如何分配这M台设备才能使国家得到的盈利最大?求出最大盈利值.其中M≤15,N≤10.分 ...

  8. 自定义滚动条插件 mCustomScrollbar 使用介绍

    引用有心的学士笔记 http://www.wufangbo.com/mcustomscrollbar/ http://www.jianshu.com/p/550466260856 官网地址 http: ...

  9. Jetty项目解压目录设置

    Ubuntu14.04 没有在Jetty的根目录下建立work文件夹时,Jetty会默认将项目解压到/var/cache/jetty/data/下,如果在Jetty的根目录下建立work文件夹,jet ...

  10. Problem Statement

    题目链接:https://vjudge.net/contest/239445#problem/E     E - Problem Statement You are given nn strings ...