题目传送门

 /*
01背包(类):dp[i][j][k] 表示从(i, j)出发的和为k的方案数,那么cnt = sum (dp[1][i][s])
状态转移方程:dp[i][j][k] = dp[i+1][j][k-c] + dp[i+1][j+1][k-c];(下半部分) 上半部分类似
因为要输出字典序最小的,打印路径时先考虑L
*/
/************************************************
* Author :Running_Time
* Created Time :2015-8-9 15:45:11
* File Name :UVA_10564.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int MAXN = ;
const int MAXS = 5e2 + ;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + ;
int a[MAXN*][MAXN];
ll dp[MAXN*][MAXN][MAXS];
int n, s; void print(int x, int y, int sum) {
if (x >= * n - ) return ;
int v = a[x][y];
if (x < n) {
if (y > && dp[x+][y-][sum-v]) {
printf ("L"); print (x+, y-, sum - v);
}
else {
printf ("R"); print (x+, y, sum - v);
}
}
else {
if (dp[x+][y][sum-v]) {
printf ("L"); print (x+, y, sum - v);
}
else {
printf ("R"); print (x+, y+, sum - v);
}
}
} int main(void) { //UVA 10564 Paths through the Hourglass
while (scanf ("%d%d", &n, &s) == ) {
if (!n && !s) break;
for (int i=; i<=n; ++i) {
for (int j=; j<=n-i+; ++j) scanf ("%d", &a[i][j]);
}
for (int i=n+; i<=*n-; ++i) {
for (int j=; j<=i-n+; ++j) scanf ("%d", &a[i][j]);
} memset (dp, , sizeof (dp));
for (int i=; i<=n; ++i) {
int c = a[*n-][i];
dp[*n-][i][c] = ;
}
for (int i=*n-; i>=n; --i) {
for (int j=; j<=i-n+; ++j) {
int c = a[i][j];
for (int k=c; k<=s; ++k) {
dp[i][j][k] = dp[i+][j][k-c] + dp[i+][j+][k-c];
}
}
}
ll cnt = ;
for (int i=n-; i>=; --i) {
for (int j=; j<=n-i+; ++j) {
int c = a[i][j];
for (int k=c; k<=s; ++k) {
if (j > ) dp[i][j][k] += dp[i+][j-][k-c];
if (j < n - i + ) dp[i][j][k] += dp[i+][j][k-c];
}
if (i == ) cnt += dp[i][j][s];
}
}
printf ("%lld\n", cnt);
for (int i=; i<=n; ++i) {
if (dp[][i][s]) {
printf ("%d ", i-);
print (, i, s); break;
}
}
puts ("");
} return ;
}

01背包(类) UVA 10564 Paths through the Hourglass的更多相关文章

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

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

  2. 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] 方案 ...

  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. JOISC2014 挂饰("01"背包)

    传送门: [1]:洛谷 [2]:BZOJ 参考资料: [1]:追忆:往昔 •题解 上述参考资料的讲解清晰易懂,下面谈谈我的理解: 关键语句: 将此题转化为 "01背包" 类问题,关 ...

  7. Uva 12563,劲歌金曲,01背包

    题目链接:https://uva.onlinejudge.org/external/125/12563.pdf 题意:n首歌,每首歌的长度给出,还剩 t 秒钟,由于KTV不会在一首歌没有唱完的情况下切 ...

  8. UVA 10564 十 Paths through the Hourglass

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

  9. UVA 624 - CD (01背包 + 打印物品)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

随机推荐

  1. Core java for impatient 笔记 ch8 流

    流stream 使用了数据视图,让你可以在比集合更高的概念上指定操作使用流,你只需要将操作的调度留给实现,例如,假设你要计算某个属性的平均值,你只需要指定数据源和属性,然后流类库会优化计算,比如使用多 ...

  2. sata express接口

    华硕z97主板的sata express接口目前没什么用,但随着电脑接口的发展,可能会占据一席之地. 1.顾名思义,SATA-Express是SATA接口 + PCI-Express的混合体,其理论带 ...

  3. 模拟用户点击弹出新页面不会被浏览器拦截_javascript技巧

    原文:http://www.html5cn.com.cn/article/zxzx/3195.html 相信用过window.open的小伙伴们都遇到过被浏览器拦截导致页面无法弹出的情况:我们换下思路 ...

  4. 在gentoo中打开tomcat的远程调试开关

    在一般象gentoo等发行版中,系统安装tomcat这类软件后会产生一些启动脚本, 比如是/etc/init.d/tomcat-7, 启动方式与原始的tomcat不太一样.在gentoo中,假设须要远 ...

  5. clamav完整查杀linux病毒实战(摘抄)

    http://dadloveu.blog.51cto.com/blog/715500/1882521 Linux服务器一直给我们的印象是安全.稳定.可靠,性能卓越.由于一来Linux本身的安全机制,L ...

  6. 程序员之---C语言细节18(一些奇怪表达式)

    主要内容:一些奇怪表达式 #include <stdio.h> #define N 10 int main() { int a = 1; int *q = &a; int p = ...

  7. Python 获取新浪微博的热门话题 (API)

    Code: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-06-27 @author: guaguastd @name: ...

  8. keepalived + lvs 网站高可用集群

    一 ,四台服务器 master 端 : 192.168.1.3 backup 端: 192.168.1.4 REserver1 端 : 192.168.1.5 REserver2 端: 192.168 ...

  9. 使用 BenchmarkDotnet 测试代码性能 【Win10】单元测试中捕获异步方法的指定异常

    先来点题外话,清明节前把工作辞了(去 tm 的垃圾团队,各种拉帮结派.勾心斗角).这次找工作就得慢慢找了,不能急了,希望能找到个好团队,好岗位吧.顺便这段时间也算是比较闲,也能学习一下和填掉手上的坑. ...

  10. html css 仿微信底部自己定义菜单

    近期几个月一直从事微信开发,从刚開始的懵懂渐渐成长了一点. 今天认为微信底部自己定义菜单,假设能在html的页面上也能显示就好了. 记得曾经看过某个网页有类似效果.查找了该网页的css.  ok如今h ...