Codeforces Round #105 (Div. 2) E 01背包



链接:戳这里


E. Porcelain
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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排架子,每排架子上都放有价值为vij的物品。公主每次都从边缘拿物品,(左或者右)

问拿m个物品使得价值最大为多少


思路:

由于每次只能拿边缘,设置dp[i][j]表示第i排架子拿j个物品的最大价值。

这里暴力枚举左边连续拿多少个,右边连续拿多少个就行了。

现在就是每排取<=num[i]个的物品时的总物品数为m,然后价值最大

类似背包的思想,当前第i排架子取k个的。堆到m个物品的最大值


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#include<bitset>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
#define INF (1ll<<60)-1
#define Max 1e9
using namespace std;
int n,m;
int num[110];
int a[110];
int L[110],R[110];
int dp[110][110]; /// 第i行架子取j个物品
int f[100100]; /// 取了i个物品的最大价值
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",&num[i]);
        for(int j=1;j<=num[i];j++) scanf("%d",&a[j]);
        L[0]=R[0]=0;
        for(int j=1;j<=num[i];j++) L[j]=L[j-1]+a[j];
        for(int j=1;j<=num[i];j++) R[j]=R[j-1]+a[num[i]-j+1];
        //for(int j=1;j<=num[i];j++) cout<<L[j]<<" ";cout<<endl;
        //for(int j=1;j<=num[i];j++) cout<<R[j]<<" ";cout<<endl;
        for(int j=0;j<=num[i];j++) {
            for(int k=0;k<=j;k++){
                dp[i][j]=max(dp[i][j],L[k]+R[j-k]);
            }
        }
        //for(int j=0;j<=num[i];j++) cout<<dp[i][j]<<" ";cout<<endl;
    }
    for(int i=1;i<=n;i++){
        for(int j=m;j>=0;j--){
            for(int k=0;k<=num[i];k++){
                if(j-k<0) continue;
                f[j]=max(f[j],f[j-k]+dp[i][k]);
            }
        }
    }
    cout<<f[m]<<endl;
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值