UVa 10564 DP Paths through the Hourglass

从下往上DP,d(i, j, k)表示第(i, j)个格子走到底和为k的路径条数。

至于字典序最小,DP的时候记录一下路径就好。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5
 6 int n, sum;
 7 int a[50][25];
 8 long long d[50][50][550];
 9 int p[50][50][550][2];
10
11 int main()
12 {
13     while(scanf("%d%d", &n, &sum) == 2 && n)
14     {
15         memset(d, 0, sizeof(d));
16         memset(p, 0, sizeof(p));
17         for(int i = 1; i <= n; i++)
18             for(int j = 1; j <= n - i + 1; j++) scanf("%d", &a[i][j]);
19         for(int i = n + 1; i < n * 2; i++)
20             for(int j = 1; j <= i - n + 1; j++) scanf("%d", &a[i][j]);
21
22         for(int i = 1; i <= n; i++)
23         {
24             int t = a[n*2-1][i];
25             d[n*2-1][i][t] = 1;
26         }
27
28         for(int i = n*2-2; i >= n; i--)
29             for(int j = 1; j <= i - n + 1; j++)
30                 for(int k = a[i][j]; k <= sum; k++)
31                 {
32                     int t = k - a[i][j];
33                     if(d[i+1][j][t]) { d[i][j][k] += d[i+1][j][t]; p[i][j][k][0] = 1; }
34                     if(d[i+1][j+1][t]) { d[i][j][k] += d[i+1][j+1][t]; p[i][j][k][1] = 1; }
35                 }
36
37         for(int i = n - 1; i >= 1; i--)
38             for(int j = 1; j <= n - i + 1; j++)
39                 for(int k = a[i][j]; k <= sum; k++)
40                 {
41                     int t = k - a[i][j];
42                     if(d[i+1][j-1][t]) { d[i][j][k] += d[i+1][j-1][t]; p[i][j][k][0] = 1; }
43                     if(d[i+1][j][t]) { d[i][j][k] += d[i+1][j][t]; p[i][j][k][1] = 1; }
44                 }
45
46         long long ans = 0;
47         for(int i = 1; i <= n; i++) ans += d[1][i][sum];
48         printf("%lld\n", ans);
49         if(!ans) { puts(""); continue; }
50
51         int j, now = sum;
52         for(int i = 1; i <= n; i++) if(d[1][i][sum]) { j = i; break; }
53         printf("%d ", j - 1);
54
55         for(int i = 1; i < n * 2 - 1; i++)
56         {
57             if(p[i][j][now][0])
58             {
59                 printf("L");
60                 now -= a[i][j];
61                 if(i < n) j--;
62             }
63             else
64             {
65                 printf("R");
66                 now -= a[i][j];
67                 if(i >= n) j++;
68             }
69         }
70         puts("");
71     }
72
73     return 0;
74 }

代码君

还有一件事想抽自己两巴掌,就是一直困惑我的谜之AC,谜之WA,是因为没关freopen,WTF!

时间: 2024-10-05 04:44:51

UVa 10564 DP Paths through the Hourglass的相关文章

UVA 10564 十 Paths through the Hourglass

Paths through the Hourglass Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 10564 1 #include <stdio.h> 2 #include <string.h> 3 4 int n,s; 5 long long dp[55][56][550]; 6 int a[56][55]; 7 8 i

UVA 10564 Paths through the Hourglass[DP 打印]

UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径. f[i][j][k]从下往上到第i层第j个和为k的方案数 上下转移不一样,分开处理 没必要判断走出沙漏 打印方案倒着找下去行了,尽量往左走 沙茶的忘注释掉文件WA好多次 #include <iostream> #include <cstdio> #include <algor

uva 10564 Paths through the Hourglass (DP)

uva 10564 Paths through the Hourglass Paths through the Hourglass Input: Standard Input Output: Standard Output Time Limit: 2 Seconds In the hourglass to the right a path is marked. A path always starts at the first row and ends at the last row. Each

UVa 10564 - Paths through the Hourglass(DP)

Description Problem F Paths through the Hourglass Input: Standard Input Output: Standard Output Time Limit: 2 Seconds In the hourglass to the right a path is marked. A path always starts at the first row and ends at the last row. Each cell in the pat

01背包(类) UVA 10564 Paths through the Hourglass

题目传送门 1 /* 2 01背包(类):dp[i][j][k] 表示从(i, j)出发的和为k的方案数,那么cnt = sum (dp[1][i][s]) 3 状态转移方程:dp[i][j][k] = dp[i+1][j][k-c] + dp[i+1][j+1][k-c];(下半部分) 上半部分类似 4 因为要输出字典序最小的,打印路径时先考虑L 5 */ 6 /************************************************ 7 * Author :Runni

uva10564 - Paths through the Hourglass(递推)

题目:uva10564 - Paths through the Hourglass(递推) 题目大意:给出这样的两个数塔,然后给出一个值,问你能否从这个数塔中找到路径,路径上的值之和等于这个数,输出这样的路径的总数,如果多条打印路径先挑开始的位置(0..n - 1)最小的,如果这样还是有多条,在比较后面的向左向右字典序最小的. 解题思路:一开始两个数塔一个正着推,一个倒着推,结果数目是出来了,但是路径就难办了,最后把这两个数塔都正着推,只是状态转移方程不一样.这样路径输出就比较好处理. 代码:

uva 10564

Problem FPaths through the HourglassInput: Standard Input Output: Standard Output Time Limit: 2 Seconds In the hourglass to the right a path is marked. A path always starts at the first row and ends at the last row. Each cell in the path (except the

UVa 988 - Many Paths, One Destination

题目:人生有很多选择,现在给你一些选择(0~n-1),和每个选择分支后面的其他选择序号,求选择总数. 分析:dp,图论.如果某状态的后续选择个数是0个则,代表死亡,统计所有到达死亡的路径条数即可. 用一个状态数组记录到达每个选择的路径数,它等于能到达它的前驱节点的路径加和. 稀疏图,使用邻接表储存.初始是节点0的路径条数为1,代表出生. 说明:没有给数据范围略坑啊,RE一次,WA一次,╮(╯▽╰)╭. #include <iostream> #include <cstdlib> #

uva 1401 dp+Trie

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=4147 题意:给定一个字符串,以及若干单词,求有几种方式能用单词组成字符串 我先是dp方程推得有问题不知怎么修改搞得卡了很久,然后就是数组开得太小一直RE trie数组大小=单词个数*单词长度  dp[i]为以str[i]开头的后缀的ans,dp[i]=segma(