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        :Running_Time
  8  * Created Time  :2015-8-9 15:45:11
  9  * File Name     :UVA_10564.cpp
 10  ************************************************/
 11
 12 #include <cstdio>
 13 #include <algorithm>
 14 #include <iostream>
 15 #include <sstream>
 16 #include <cstring>
 17 #include <cmath>
 18 #include <string>
 19 #include <vector>
 20 #include <queue>
 21 #include <deque>
 22 #include <stack>
 23 #include <list>
 24 #include <map>
 25 #include <set>
 26 #include <bitset>
 27 #include <cstdlib>
 28 #include <ctime>
 29 using namespace std;
 30
 31 #define lson l, mid, rt << 1
 32 #define rson mid + 1, r, rt << 1 | 1
 33 typedef long long ll;
 34 const int MAXN = 22;
 35 const int MAXS = 5e2 + 10;
 36 const int INF = 0x3f3f3f3f;
 37 const int MOD = 1e9 + 7;
 38 int a[MAXN*2][MAXN];
 39 ll dp[MAXN*2][MAXN][MAXS];
 40 int n, s;
 41
 42 void print(int x, int y, int sum)    {
 43     if (x >= 2 * n - 1) return ;
 44     int v = a[x][y];
 45     if (x < n)  {
 46         if (y > 1 && dp[x+1][y-1][sum-v])   {
 47             printf ("L");  print (x+1, y-1, sum - v);
 48         }
 49         else    {
 50             printf ("R");  print (x+1, y, sum - v);
 51         }
 52     }
 53     else    {
 54         if (dp[x+1][y][sum-v])    {
 55             printf ("L");  print (x+1, y, sum - v);
 56         }
 57         else    {
 58             printf ("R");  print (x+1, y+1, sum - v);
 59         }
 60     }
 61 }
 62
 63 int main(void)    {     //UVA 10564    Paths through the Hourglass
 64     while (scanf ("%d%d", &n, &s) == 2) {
 65         if (!n && !s)   break;
 66         for (int i=1; i<=n; ++i)    {
 67             for (int j=1; j<=n-i+1; ++j)    scanf ("%d", &a[i][j]);
 68         }
 69         for (int i=n+1; i<=2*n-1; ++i)  {
 70             for (int j=1; j<=i-n+1; ++j)    scanf ("%d", &a[i][j]);
 71         }
 72
 73         memset (dp, 0, sizeof (dp));
 74         for (int i=1; i<=n; ++i)    {
 75             int c = a[2*n-1][i];
 76             dp[2*n-1][i][c] = 1;
 77         }
 78         for (int i=2*n-2; i>=n; --i)  {
 79             for (int j=1; j<=i-n+1; ++j)    {
 80                 int c = a[i][j];
 81                 for (int k=c; k<=s; ++k)  {
 82                     dp[i][j][k] = dp[i+1][j][k-c] + dp[i+1][j+1][k-c];
 83                 }
 84             }
 85         }
 86         ll cnt = 0;
 87         for (int i=n-1; i>=1; --i)    {
 88             for (int j=1; j<=n-i+1; ++j)    {
 89                 int c = a[i][j];
 90                 for (int k=c; k<=s; ++k)    {
 91                     if (j > 1)  dp[i][j][k] += dp[i+1][j-1][k-c];
 92                     if (j < n - i + 1)  dp[i][j][k] += dp[i+1][j][k-c];
 93                 }
 94                 if (i == 1) cnt += dp[i][j][s];
 95             }
 96         }
 97         printf ("%lld\n", cnt);
 98         for (int i=1; i<=n; ++i)    {
 99             if (dp[1][i][s])    {
100                 printf ("%d ", i-1);
101                 print (1, i, s);    break;
102             }
103         }
104         puts ("");
105     }
106
107     return 0;
108 }
时间: 2024-11-10 11:13:03

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

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

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 = 22,maxs = 501; int hg[maxn<<1][

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

UVA - 10032 Tug of War (二进制标记+01背包)

Description Problem F: Tug of War A tug of war is to be arranged at the local office picnic. For the tug of war, the picnickers must be divided into two teams. Each person must be on one team or the other; the number of people on the two teams must n

uva 624 CD (01背包)

uva 624 CD You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is on CDs. You need to have it on tapes so the problem to solve is: you have a tape N minutes long. How to choose tracks from CD to get most ou

UVA - 10163Storage Keepers(01背包)

题目大意:UVA - 10163Storage Keepers(01背包) 题目大意:现在有m个守店人,和n家店,每个守店人有个能力值,然后一个守护店的人可以守K家店,那么这些店能到的安全度就是Pi / K.店的安全度取决于守护它的人给的安全度中间最低的那个.这些店的最高安全度取决于最低安全度的那家店.现在问如何雇佣这些人使得店的安全度最高的情况下,费用最少. 解题思路: 安全度要求最高,那么就是要判断每个守店的人要不要雇佣,并且雇佣来之后让它守几家店(安全度).dp[i][j]表示:前面的i家

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