您的位置:首页 > 编程语言 > Go语言

Dreamoon and WiFi

2015-03-11 16:24 344 查看


Description

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.

Sample Input

Input

2 3

3 3 7 2

3 4 1 5

Output

15

Input

1 3

4 4 3 1 2

Output

9

题目大意:

输入n, m表示有n个架子,一共可以去m件物品

然后接下来描述每个架子,第一个数字表示架子物品数,接下来描述每个物品的价值4 3 1 2

然后你可以在这个架子上拿东西,必须满足只能拿两端点的物品。

然后问最后所有架子拿走m个物品 能获得的最大价值

两次dp ,首先对每个架子做一次dp 利用前缀和 sum[len];

然后dp1[i]表示拿走i件物品,dp1[0] = 0, dp1[i] = max(dp[i], sum[l] + sum[len] - sum[len - (r - l)]

接下来 dp2[i][j]表示 前i个架子,拿走j件

有dp2[i][j] = max(dp2[i-1][j-k], dp2[i][j]);

代码如下:

//

// Create by Running Photon on 2015-03-08

//
#include <iostream>

#include <cstdio>

#include <cstring>

#include <cmath>

#include <cstdlib>

#include <iomanip>

#include <algorithm>

#include <cctype>

#include <stack>

#include <queue>

#include <map>

#include <string>

#include <set>

#include <vector>

using namespace std;
#define CLR(x) memset(x,0,sizeof x)

#define ll long long

const int inf=0x3f3f3f3f;

const int maxn=1e4+5;

const int MOD=5e5+5;

int n, m;

int dp[maxn];

int num[maxn];

int txt[maxn];

int main()

{

#ifdef LOCAL

 freopen("in.txt","r",stdin);

 //freopen("out.txt","w",stdout);

#endif

 ios_base::sync_with_stdio(0);
 while(scanf("%d%d", &n, &m)!=EOF){

        CLR(dp);

        int ans = 0;

        for(int i = 1; i <= n; i++){

            CLR(num);

            CLR(txt);

            int len;

            scanf("%d", &len);

            for(int j = 1; j <= len; j++){

                int tmp;

                scanf("%d", &tmp);

                txt[j] = txt[j-1] + tmp;

            }

            for(int j = 1; j <= len; j++){

                int tmp = 0;

                for(int k = 0; k <= j; k++){

                    tmp = max(tmp, txt[k] + txt[len] - txt[len - (j - k)]);//左边加k个数,右边加 j-k个数

                }

                num[j] = tmp;

            }

            for(int j = m; j >= 1; j--){

                for(int k = 1; k <= len && k <= j; k++){

                    dp[j] = max(dp[j], dp[j-k] + num[k]);

                    ans = max(ans, dp[j]);

                }

            }

        }

        printf("%d\n", ans);

 }
 return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  algorithm dp