codeforces-148E-Porcelain【DP】

解决一个有趣的问题:公主发脾气时会砸碎珍贵的瓷器,每次只从书架的两端取下一件。任务是在给定的次数内找到能造成最大损失的价值组合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


148E-Porcelain


                    time limit per test1 second     memory limit per test256 megabytes

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.

input
2 3
3 3 7 2
3 4 1 5
output
15

input
1 3
4 4 3 1 2
output
9

题目链接:cf-148E

题目大意:有n个书架,每次取书只能从书架两端开始取,取m本书,问价值最大为多少

题目思路:今天比赛写了好久,可惜还差最后一步,少了一个for循环,最后发现已经没时间改了,再加上,我以为会超时orz结果并不会

①求出前缀和用zhi记录

②用sum数组存,sum[i][j]表示独立的第i个货架取j本书能达到的最大值

③dp[i][j]表示到第i个书架,取了j本书的最大值

以下是代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#include<iomanip>
using namespace std;
int dp[205][10005] = {0};
vector <int> ret[205];
int sum[205][205];
int zhi[205][205];
int main()
{
    int n,m;
    cin >> n >> m;
    for( int i=0 ; i<n ; i++ ){
        int t;
        cin >> t;
        for( int j=0 ; j<t ; j++ ){
            int num;
            cin >> num;
            ret[i].push_back(num);
            if( j==0 ) zhi[i][j] = ret[i][j];
            else zhi[i][j] = zhi[i][j-1]+ret[i][j];
        }
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j <= ret[i].size(); j++)  //取j个 
        {
            int len = ret[i].size();
            for (int k = 0; k <= j ; k++){  //前面的长度 
                if(k == 0)
                {
                    sum[i][j]= max(sum[i][j],zhi[i][len - 1] - zhi[i][len - 1 - j]);
                } 
                else
                {

                    sum[i][j] = max(sum[i][j],zhi[i][k - 1] + zhi[i][len - 1] - zhi[i][len - 1 - j + k]);
                }
            }
        } 
    }
    int ans = 0;
    for (int i = 0; i <= m; i++)
    {
        if (i <= ret[0].size()) dp[0][i] = sum[0][i];
        else dp[0][i] = dp[0][ i - 1];
        ans = max(ans,dp[0][i]);
    }
    for (int i = 1; i < n; i++)  //前i个  
    {
        for (int j = 0; j <= m; j++)  
        {
            for (int k = 0; k <= ret[i].size() && k <= j; k++) //第i个取k个 
            {
                dp[i][j] = max(dp[i][j],dp[i - 1][j - k] + sum[i][k]);
                ans = max(ans,dp[i][j]);
            }
        } 
    }
    cout << ans << endl;
    return 0;
}
/*
2 10
3 4 5 6
5 1 2 3 4 5
*/
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值