dp--最大区间和变形-cf-1155D
D. Beautiful Array
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given an array aa consisting of nn 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 aa and multiply all values contained in this subarray by xx. You want to maximize the beauty of array after applying at most one such operation.
Input
The first line contains two integers nn and xx (1≤n≤3⋅105,−100≤x≤1001≤n≤3⋅105,−100≤x≤100) — the length of array aa and the integer xxrespectively.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (−109≤ai≤109−109≤ai≤109) — the array aa.
Output
Print one integer — the maximum possible beauty of array aa after multiplying all values belonging to some consecutive subarray xx.
Examples
input
Copy
5 -2
-3 8 -2 1 -6
output
Copy
22
input
Copy
12 -3
1 3 3 7 1 3 3 7 1 3 3 7
output
Copy
42
input
Copy
5 10
-1 -2 -3 -4 -5
output
Copy
0
Note
In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]).
In the second test case we don't need to multiply any subarray at all.
In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
状态转移方程如下
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <sstream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <iomanip>
#include <stack>
#include <climits>
#include <fstream>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int N = 300005;
const int MOD = 1e9 + 7;
#define F(i, l, r) for(int i = l;i <= (r);++i)
#define RF(i, l, r) for(int i = l;i >= (r);--i)
int main()
{
LL n, m, t, ans = 0, p = 0, sum1 = 0, sum2 = 0;
cin >> n >> m;
F(i, 1, n)
{
cin >> t;
sum1 = max(sum1 + t, 0ll);//d[i][0] = max(0ll,d[i-1][0]+a[i]);
sum2 = max(sum2 + m * t, sum1);//d[i][1] = max( 0ll, max(d[i-1][0],d[i-1][1])+a[i]*x);
p = max(p + t, sum2);// d[i][2] = max( 0ll, max(d[i-1][0], max(d[i-1][1],d[i-1][2]) )+ a[i]);
ans = max(ans, p);
}
cout << ans << endl;
return 0;
}
dp--最大区间和变形-cf-1155D的更多相关文章
- Codeforces Round #354 (Div. 2)-C. Vasya and String,区间dp问题,好几次cf都有这种题,看来的好好学学;
C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input standard ...
- hihocoder1636 Pangu and Stones(区间DP(石子合并变形))
题目链接:http://hihocoder.com/problemset/problem/1636 题目大意:有n堆石头,每次只能合并l~r堆,每次合并的花费是要合并的石子的重量,问你合并n堆石子的最 ...
- Vijos1451圆环取数[环形DP|区间DP]
背景 小K攒足了路费来到了教主所在的宫殿门前,但是当小K要进去的时候,却发现了要与教主守护者进行一个特殊的游戏,只有取到了最大值才能进去Orz教主…… 描述 守护者拿出被划分为n个格子的一个圆环,每个 ...
- poj 2184 Cow Exhibition(dp之01背包变形)
Description "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - ...
- dp之区间:Light oj 1422 Halloween Costumes
http://lightoj.com/volume_showproblem.php?problem=1422 题意:给你n天需要穿的衣服的样式,每次可以套着穿衣服,脱掉的衣服就不能再穿了,问至少要带多 ...
- Codeforces Round #336 (Div. 2)B 暴力 C dp D 区间dp
B. Hamming Distance Sum time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- 区间DP(区间最优解)题目讲解总结
1:给出一个括号字符串,问这个字符串中符合规则的最长子串的长度. [分析]区间DP要覆盖整个区间,那么要求所有情况的并集. 先想出状态方程: dp[i][j]:i ~ j区间内最大匹配数目 输出:dp ...
- 「DP」区间dp
区间dp? 状态设计为描述一段区间的dp. eg:f[i][j]表示从 i 到 j 这个区间上的最优值.
- lightoj 1422【区间DP·分类区间首元素的情况】
题意: 给你n天分别要穿的衣服种类,可以套着穿, 一旦脱下来就不能再穿,求n天至少要几件. 思路: 区间DP dp[i][j]代表i到j需要至少几件衣服 第i天的衣服在第i天穿上了,dp[i][j]= ...
随机推荐
- JAVA array,map 转 json 字符串
public class User { private String username; private String password; public String getUsername() { ...
- [转]Android Parcelable和Serializable的区别
本文主要介绍Parcelable和Serializable的作用.效率.区别及选择,关于Serializable的介绍见Java 序列化的高级认识. 1.作用 Serializable的作用是为了保存 ...
- http头部信息
1.常见的返回码 100: 请服务器端继续返回 200:成功 301:永久重定向 存的地址永久的改变了 301 302 : 暂时重定向 302仍然使用老得url 401 : 无法找到资源file n ...
- CentOS7通过 yum安装路径查询方法
CentOS7通过 yum安装路径查询方法 rpm -qa 然后执行 rpm -ql 软件名称 就可以显示软件的安装路径. 原文博客的链接地址:https://cnblogs.com/qzf/
- tpshop使用中遇到的问题
1.短信配置里:商家发货时是否给客户发短信 配置了 开启 如果购买者个人资料里的电话没填写,商家点击发货时, 程序会挂掉 解决方法:修改application\common\logic\SmsL ...
- VMware安装Ubuntu
1. VMware Ubuntu安装详细过程:http://blog.csdn.net/u013142781/article/details/50529030 2. 怎样在VMware虚拟机中使用安装 ...
- Appium 之处理首次启动手机App时的系统权限弹框
一般首次启动一个手机App时都会有系统权限弹框,如下图所示: 权限弹窗上面的按钮都是固定的,只需要定位到“ALLOW”按钮,点击就可以了,代码如下: 这里主要用selenium里面的显示等待模块(We ...
- B-spline Curves 学习之B样条基函数的定义与性质(2)
B-spline Basis Functions 本博客转自前人的博客的翻译版本,前几章节是原来博主的翻译内容,但是后续章节博主不在提供翻译,后续章节我在完成相关的翻译学习. (原来博客网址:http ...
- handsontable-developer guide-cell function
renderer 展示的数据不是来自于数据源,而是先把DOM和其他信息传给renderer,然后展示. //五种展示函数 TextRenderer: default NumericRenderer A ...
- ie11 兼容的问题
碰到一个问题 下拉列表点不了. 测试后,只在ie11下有这个问题. 先是在head 加<meta http-equiv=”X-UA-Compatible” content="IE=8& ...