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 ...
随机推荐
- PC客户端开发细节记录:保存GUID到VARIANT
有两个 API 可以实现保存 GUID 到 VARIANT InitVariantFromGUIDAsBuffer 以字节数组形式保存,保存类型为 VT_ARRAY | VT_UI1,相当于字节拷贝, ...
- WebAPI返回时间数据不带T
最近一段时间项目里面使用WebAPI比较多,但是在返回时间数据的时候回默认带上T,就像这样子 "2016-04-21T13:26:17.4701811+08:00", 这样的数据在 ...
- LeetCode题解之N-ary Tree Preorder Traversal
1.题目描述 2.问题分析 采用递归方法是标准解法. 3.代码 vector<int> preorder(Node* root) { vector<int> v; preNor ...
- 安卓ADB命令
查看连接的设备 adb devices -l FastBoot常用命令: fastboot erase system #擦除system分区 fastboot erase boot #擦除 ...
- Azure 中虚拟机的备份和还原选项
可以通过定期创建备份来保护数据. 有多个备份选项可用于 VM,具体取决于使用案例. Azure 备份 若要备份运行生产工作负荷的 Azure VM,请使用 Azure 备份. Azure 备份对 Wi ...
- Oracle EBS INV 删除保留
DECLARE p_rsv apps.inv_reservation_global.mtl_reservation_rec_type; p_dummy_sn apps.inv_reservation_ ...
- sqlite 字符串拼接
select path || '%' from t_category where depth = 0 and type = 0 用'||'拼接字符串 比如path是/1001/的话 那结果就是/10 ...
- Grafana是一个可视化面板-安装配置介绍
Grafana是一个可视化面板(Dashboard),有着非常漂亮的图表和布局展示,功能齐全的度量仪表盘和图形编辑器,支持Graphite.zabbix.InfluxDB.Prometheus和Ope ...
- Security Software Engineer
Security Software Engineer Are you excited to be part of the VR revolution and work on cutting edge ...
- windows系统利用任务管理器-资源监视器了解程序的磁盘读写量
场景摘要: 1.刚部署上线一个新功能,增加了日志输出模块 2.本身服务器资源已经紧张了,在增加日志输出,就想了解磁盘读写量 3.从中突然发现oracle的磁盘读写量比较大 4.在展开明细发现是or ...