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,现在给出金额,问可以用多少种方式组成该面值. 每种硬币的数量是无限的.典型完全背包. 状态 ...
随机推荐
- bzoj 3944: Sum(杜教筛)
3944: Sum Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 4930 Solved: 1313[Submit][Status][Discuss ...
- 返回top写法技巧
HTML<a href="#" class="fixed">top</a> CSS: .fixed{ padding: 20px 15p ...
- [Leetcode]007. Reverse Integer
public class Solution { public int reverse(int x) { long rev=0; while(x!=0){ rev = rev*10+x%10; x=x/ ...
- Vue 3 --安装脚手架、过滤器、生命周期的钩子函数、vue-router基本使用
一.安装脚手架 1.下载node.js,本文下载版本为node-v8.12.0-x64.msi,一键式安装. 2.安装完成后,打开终端,输入node,可进入node环境(两次ctrl+c退出),如下图 ...
- wms-springmvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- rest get post 请求工具_restclient.jar
下载 :restclient.jar 启动命令 : java -jar restclient.jar 界面如图所示:
- spring boot test MockBean
使用spring boot , MockBean @RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class) p ...
- Python3.5 控制台日志输出,区分标准输出与错误输出
import logging import sys class ConsoleHandler(logging.Handler): terminator = '\n' def __init__(self ...
- PHP面试题基础问题
1.对于大流量的网站,您采用什么样的方法来解决访问量问题? 首先,确认服务器硬件是否足够支持当前的流量 其次,优化数据库访问. 第三,禁止外部的盗链. 第四,控制大文件的下载. 第五,使用不同主机分流 ...
- 为什么有人会觉得IT门槛低,工资高?
今天在高铁上,翻着逼乎,被一个话题勾住了,"为什么很多人会觉得IT门槛低?" 我一惊,还真是,身边朋友都觉得"IT赚的多","程序员工资高" ...