Codeforces 148 E Porcelain dp


During her tantrums the princess usually smashes some collectable porcelain. Every furious shriek is accompanied with one item smashed.

The collection of porcelain is arranged neatly on n shelves. Within each shelf the items are placed in one row, so that one can access only the outermost items — the leftmost or the rightmost item, not the ones in the middle of the shelf. Once an item is taken, the next item on that side of the shelf can be accessed (see example). Once an item is taken, it can't be returned to the shelves.

You are given the values of all items. Your task is to find the maximal damage the princess' tantrum of m shrieks can inflict on the collection of porcelain.

Input

The first line of input data contains two integers n (1 ≤ n ≤ 100) and m (1 ≤ m ≤ 10000). The next n lines contain the values of the items on the shelves: the first number gives the number of items on this shelf (an integer between 1 and 100, inclusive), followed by the values of the items (integers between 1 and 100, inclusive), in the order in which they appear on the shelf (the first number corresponds to the leftmost item, the last one — to the rightmost one). The total number of items is guaranteed to be at least m.

Output

Output the maximal total value of a tantrum of m shrieks.

Examples
input
2 3
3 3 7 2
3 4 1 5
output
15
input
1 3
4 4 3 1 2
output
9
Note

In the first case there are two shelves, each with three items. To maximize the total value of the items chosen, one can take two items from the left side of the first shelf and one item from the right side of the second shelf.

In the second case there is only one shelf, so all three items are taken from it — two from the left side and one from the right side.

题意:n排书,最多拿m本,问最大价值是多少,接下来n行,每行第一个数字为这行书的数量,接下来是每本书的价值。注意,只能从每行的最右端或最左端拿书。

题解:
dp问题。因为拿书有限制条件,所以我们需要先处理一下,w[I][[j]为在第I行取了j本书时的价值,有三种取法:1,一直从最左端取(前缀和);2,一直从后面取(后缀和);3,左右分别取(for循环讨论)。

dp[I][j]表示在第I行取了j本书后的价值,状态转移为:dp[I][j]=max(dp[i-1][j-k]+w[I][k],dp[I][j]);

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <stack>
#include <algorithm>
#define mem(a) memset(a, 0, sizeof(a))
#define eps 1e-5
#define M 100005
#define inf 1000000007
using namespace std;
int n,m;
int d[101],dd[101][101],qian[101][101],hou[101][101],w[101][101],dp[101][10010];
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        mem(d);
        mem(dd);
        mem(qian);
        mem(hou);
        mem(w);
        mem(dp);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&d[i]);
            for(int j=1;j<=d[i];j++)
            {
                scanf("%d",&dd[i][j]);
                qian[i][j]=qian[i][j-1]+dd[i][j];//前缀和
            }
        }
        for(int i=1;i<=n;i++)
        {
            int tem=1;
            for(int j=d[i];j>=1;j--)
            {
                hou[i][tem]=hou[i][tem-1]+dd[i][j];//后缀和
                tem++;
            }

        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=d[i];j++)
            {
                w[i][j]=max(qian[i][j],hou[i][j]);
                for(int k=1;k<=j;k++)
                {
                    w[i][j]=max(qian[i][k]+hou[i][j-k],w[i][j]);//三种取书方式
                }
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<=m;j++)
            {
                for(int k=0;k<=d[i]&&k<=j;k++)
                {

                dp[i][j]=max(dp[i][j],dp[i-1][j-k]+w[i][k]);

                }
            }
        }
        printf("%d\n",dp[n][m]);
    }
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
区间DP是一种动态规划的方法,用于解决区间范围内的问题。在Codeforces竞赛中,区间DP经常被用于解决一些复杂的字符串或序列相关的问题。 在区间DP中,dp[i][j]表示第一个序列前i个元素和第二个序列前j个元素的最优解。具体的转移方程会根据具体的问题而变化,但是通常会涉及到比较两个序列的元素是否相等,然后根据不同的情况进行状态转移。 对于区间长度为1的情况,可以先进行初始化,然后再通过枚举区间长度和区间左端点,计算出dp[i][j]的值。 以下是一个示例代码,展示了如何使用区间DP来解决一个字符串匹配的问题: #include <cstdio> #include <cstring> #include <string> #include <iostream> #include <algorithm> using namespace std; const int maxn=510; const int inf=0x3f3f3f3f; int n,dp[maxn][maxn]; char s[maxn]; int main() { scanf("%d", &n); scanf("%s", s + 1); for(int i = 1; i <= n; i++) dp[i][i] = 1; for(int i = 1; i <= n; i++) { if(s[i] == s[i - 1]) dp[i][i - 1] = 1; else dp[i][i - 1] = 2; } for(int len = 3; len <= n; len++) { int r; for(int l = 1; l + len - 1 <= n; l++) { r = l + len - 1; dp[l][r] = inf; if(s[l] == s[r]) dp[l][r] = min(dp[l + 1][r], dp[l][r - 1]); else { for(int k = l; k <= r; k++) { dp[l][r] = min(dp[l][r], dp[l][k] + dp[k + 1][r]); } } } } printf("%d\n", dp[n]); return 0; } 希望这个例子能帮助你理解区间DP的基本思想和应用方法。如果你还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值