CodeForces-1155D Beautiful Array
Description
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⋅10^5,−100≤x≤100)\) — the length of array \(a\) and the integer \(x\) respectively.
The second line contains \(n\) integers \(a_1,a_2,…,a_n\) \((−10^9≤ai≤10^9)\) — the array \(a\).
Output
Print one integer — the maximum possible beauty of array \(a\) after multiplying all values belonging to some consecutive subarray \(x\).
Examples
Input
5 -2
-3 8 -2 1 -6
Output
22
Input
12 -3
1 3 3 7 1 3 3 7 1 3 3 7
Output
42
Input
5 10
-1 -2 -3 -4 -5
Output
0
Solution
- \(d_1[i]\):以\(a[i]\)结尾的最大子段和
- \(d_2[i]\):\(a[i]\)被乘以\(x\)且以\(a[i]\)结尾的最大子段和
- \(d_3[i]\):\(a[i]\)没有被乘以\(x\),但在\(a[i]\)之前有一个区间被乘以\(x\),以\(a[i]\)结尾且包含该区间的最大子段和
答案就是\(\max\left(\max_{i=1}^{n}(d_1[i]), \max_{i=1}^{n}(d_2[i]),\max_{i=2}^{n}(d_3[i])\right)\)
转移方式在代码中:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int n, k;
scanf("%d%d", &n, &k);
vector<ll> a(n), d1(n), d2(n), d3(n);
for (int i = 0; i < n; ++i)
scanf("%I64d", &a[i]);
ll ans = 0;
for (int i = 0; i < n; ++i) {
d1[i] = a[i] + (i > 0 && d1[i - 1] > 0 ? d1[i - 1] : 0);
ans = max(ans, d1[i]);
}
for (int i = 0; i < n; ++i) {
d2[i] = k * a[i] + (i > 0 ? max(max(d2[i - 1], d1[i - 1]), 0LL) : 0);
ans = max(ans, d2[i]);
}
if (n > 1) ans = max(ans, d3[1] = a[0] * k + a[1]);
for (int i = 2; i < n; ++i) {
d3[i] = a[i] + max(d3[i - 1], d2[i - 1]);
ans = max(ans, d3[i]);
}
printf("%I64d\n", ans);
return 0;
}
CodeForces-1155D Beautiful Array的更多相关文章
- Codeforce 1155D Beautiful Array(DP)
D. Beautiful Array You are given an array aa consisting of nn integers. Beauty of array is the maxim ...
- [Educational Codeforces Round 63 ] D. Beautiful Array (思维+DP)
Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array time limit per test 2 seconds ...
- Educational Codeforces Round 63 D. Beautiful Array
D. Beautiful Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces 55D Beautiful Number
Codeforces 55D Beautiful Number a positive integer number is beautiful if and only if it is divisibl ...
- Codeforces 482B Interesting Array(线段树)
题目链接:Codeforces 482B Interesting Array 题目大意:给定一个长度为N的数组,如今有M个限制,每一个限制有l,r,q,表示从a[l]~a[r]取且后的数一定为q,问是 ...
- [Swift]LeetCode932. 漂亮数组 | Beautiful Array
For some fixed N, an array A is beautiful if it is a permutation of the integers 1, 2, ..., N, such ...
- 漂亮数组 Beautiful Array
2019-04-06 16:09:56 问题描述: 问题求解: 本题还是挺有难度的,主要是要考虑好如何去进行构造. 首先考虑到2 * A[i] = A[j] + A[k],那么j,k就必须是同奇同偶, ...
- CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)
传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...
- Codeforces 1077C Good Array 坑 C
Codeforces 1077C Good Array https://vjudge.net/problem/CodeForces-1077C 题目: Let's call an array good ...
- LeetCode - Beautiful Array
For some fixed N, an array A is beautiful if it is a permutation of the integers 1, 2, ..., N, such ...
随机推荐
- Android 借助Stetho在Chrome上调试Android网络、数据库、Sharedpreferences
Android 借助Stetho在Chrome上调试Android网络.数据库.Sharedpreferences 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/a ...
- Git删除文件
Git基础 Git有三大区(工作区.暂存区.版本库),文件有三个状态(untracked.unstaged.uncommited). (1)打开项目文件夹,除了隐藏的.git文件夹,其他项目文件位于的 ...
- Oracle EBS FTP显示无法与某IP 连接
首先 用root用户登录 如果可以登录 那么应该是权限的问题 这里选择 方法二:修改 /etc/sudoers 文件,找到下面一行,在root下面添加一行,如下所示: ## Allow root to ...
- 红帽7配置samba文件共享服务
samba软件主要功能是为客户机提供共享使用的文件夹. 使用的协议是SMB(TCP 139).CIFS(TCP445). 所需的软件包:samba 系统服务:smb 1.安装samba服务 ~]#yu ...
- windows中使用git和开源中国
现学现卖,学了忘忘了学. 非常感谢OSC提供了这么好的一个国内的免费的git托管平台.这里简单说下TortoiseGit操作的流程.很傻瓜了首先你要准备两个软件,分别是msysgit和tortoise ...
- XtraEditors四、TextEdit、ButtonEdit、PictureEdit、RadioGroup、PopupContainerEdit
TextEdit控件 以文本框的形式绑定各种形式的选择框: 文本框设置 输入 密码 字符 时, 要有 * 号掩盖输入的字符, 代码如下: textEdit1.Properties.PasswordCh ...
- cJSON 使用详解
由于c语言中,没有直接的字典,字符串数组等数据结构,所以要借助结构体定义,处理json.如果有对应的数据结构就方便一些, 如python中用json.loads(json)就把json字符串转变为内建 ...
- 阿里八八Alpha阶段Scrum(1/12)
任务分配 叶文滔:整体框架UI设计.作为组长进行任务协调 俞鋆:后端服务器及数据库搭建 王国超:日程模块多日显示部分设计 黄梅玲:日程模块单日显示部分设计 林炜鸿:日程模块文本添加部分设计 张岳.刘晓 ...
- Ecstore Linux服务器环境基本配置
Nginx基本配置(另存为nginx.conf直接可以使用): #user nobody; worker_processes 1; error_log logs/error.log; #error_l ...
- python spawnv用法
test.py import os import string def run(program, *args): file = program result = os.spawnv(os.P_WAIT ...