【动态规划】codeforces1155D Beautiful Array

D. Beautiful Array
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given an array a consisting of n integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0.

You may choose at most one consecutive subarray of a and multiply all values contained in this subarray by x. You want to maximize the beauty of array after applying at most one such operation.

Input
The first line contains two integers n and x (1≤n≤3⋅105,−100≤x≤100) — the length of array a and the integer x respectively.

The second line contains n integers a1,a2,…,an (−109≤ai≤109) — the array a.

Output
Print one integer — the maximum possible beauty of array a after multiplying all values belonging to some consecutive subarray x.


 最近生活越来越忙碌了。集训队由要组织春训,加上和ZWR,QZZ他们要备战南昌邀请赛,这样的话基本上没有什么周末可言了,不过这或许真是我中意的状态,毕竟畏惧自己懒惰下来。
 这次Edu本来想借机上黄,没想到又崩了。很多人轻松秒掉的D题,居然把我卡掉了。事后被KSJ嘲讽了老久。
 刚看到这题时,因为最大子线段是老题了,就按着老方法做前缀和最小值,然后根据x的值分类讨论,如果x是正数,那么我就求出最大子线段的值乘以x,如果x<=0,那么我先求最小子线段再把区间中每个数乘以x,再求一次最大子线段。后来发现x<=0时我的做法可以有很多反例。然后就开始自闭了。
 我是真心一点都没往dp方面想,看来自己dp的敏感度还不够。其实最大字段和本身也有dp做法,就是dp[i]=max(0,a[i]+dp[i−1])dp[i]=max(0,a[i]+dp[i-1])dp[i]=max(0,a[i

### 关于Codeforces平台上的动态规划问题 在Codeforces这样的编程竞赛平台上,动态规划(Dynamic Programming, DP)是一类非常重要的算法技术。这类题目通常涉及优化子结构和重叠子问题两个特性。 #### 动态规划示例解析 考虑一个典型的DP问题,在给定条件下求解最优方案的数量或具体路径等问题。例如,在某些情况下,可能需要计算达到特定状态所需的最少步数或是最大收益等[^1]。 对于具体的例子而言,假设有一个序列`a[]`,目标是从左到右遍历此序列并决定是否选取当前元素加入集合中,最终目的是让所选元素之和尽可能大而不超过某个上限值M。这个问题可以通过定义二维数组dp[i][j]表示从前i个物品里挑选若干件放入容量为j的背包可以获得的最大价值来建模: - 如果不取第i项,则`dp[i][j]=dp[i−1][j]`; - 若选择第i项且其重量w不超过剩余空间j,则更新为`max(dp[i−1][j], dp[i−1][j-w]+v)`其中v代表该项的价值; 最后的结果保存在`dp[n][m]`处(n为总项目数量,m为目标体积)[^2]。 ```cpp #include <iostream> using namespace std; const int N = 1e3 + 5; int w[N]; // weights of items int v[N]; // values of items long long f[N][N]; void knapsack(int n, int m){ for (int i = 1; i <= n; ++i) { for (int j = 0; j <= m; ++j) { f[i][j] = f[i - 1][j]; if(j >= w[i]) f[i][j] = max(f[i][j],f[i - 1][j - w[i]] + v[i]); } } } ``` 上述代码展示了如何利用记忆化搜索的方式实现简单的0/1背包问题解决方案,这同样适用于其他形式更复杂的动态规划挑战。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值