题目传送门

 /*
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. CodeForces - 812C Sagheer and Nubian Market 二分

    On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friend ...

  2. Parent and son

    Give you a tree with N vertices and N‐ 1 edges, and then ask you Q queries on “which vertex is Y's s ...

  3. Codeforces 651B Beautiful Paintings【贪心】

    题意: 给定序列,重新排序,使严格上升的子序列最多.求这些子序列总长度. 分析: 贪心,统计每个元素出现次数,每次从剩余的小的开始抽到大的,直到不再剩余元素. 代码: #include<iost ...

  4. Eclipse编辑YAML插件-YEdit

    官网:https://github.com/oyse/yedit 离线版本:(链接: https://pan.baidu.com/s/1b1j2gQ 密码: wyyb) 安装方法:直接复制JAR包到P ...

  5. python: filter, map, reduce, lambda

    filter built-in function filter(f,sequence) filter can apply the function f to each element of seque ...

  6. linux网络编程中的shutdown()与close()函数

    1.close()函数 int close(int sockfd); //返回成功为0,出错为-1 close 一个套接字的默认行为是把套接字标记为已关闭,然后立即返回到调用进程,该套接字不能再由cl ...

  7. zabbix学习系列之基础概念

    触发器 概念 "监控项"仅负责收集数据,而通常收集数据的目的还包括在某指标对应的数据超出合理范围时给相关人员发送警告信息,"触发器"正式英语为监控项所收集的数据 ...

  8. C#.NET 如何在系统变量中加入新的环境变量

    比如我要将C:\Windows\Microsoft.NET\Framework\v3.5这个目录加入环境变量 则在系统的环境变量中点击Path,编辑,然后加入一个分号";",然后粘 ...

  9. 算法基础:正整数指定规则排序问题(Golang实现)

    给定字符串内有非常多正整数,要求对这些正整数进行排序.然后返回排序后指定位置的正整数 排序要求:依照每一个正整数的后三位数字组成的整数进行从小到大排序 1)假设不足三位,则依照实际位数组成的整数进行比 ...

  10. Android ListView的item点击无响应的解决方法

    假设listitem里面包含button或者checkbox等控件,默认情况下listitem会失去焦点,导致无法响应item的事件,最经常使用的解决的方法 是在listitem的布局文件里设置des ...