Educational Codeforces Round 63 D. Beautiful Array
2 seconds
256 megabytes
standard input
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.
The first line contains two integers nn and xx (1≤n≤3⋅105,−100≤x≤100) — the length of array aa and the integer xx respectively.
The second line contains nn integers a1,a2,…,an (−109≤ai≤109) — the array aa.
Print one integer — the maximum possible beauty of array aa after multiplying all values belonging to some consecutive subarray x.
5 -2
-3 8 -2 1 -6
22
12 -3
1 3 3 7 1 3 3 7 1 3 3 7
42
5 10
-1 -2 -3 -4 -5
0
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.
果然还是太菜了,简单dp都想不出来。
dp[0] 记录不使用x的最大值
dp[1] 记录允许使用x的情况下的最大值
dp[2]记录在前面的区间中已经使用了x的情况下的最大值(尽管可能使用x的区间对最大值并没有贡献)
#include<cstdio>
#include<algorithm>
using namespace std; long long arr[]; int main(){
int n, x;
long long dp[] = {},ans=;
scanf("%d%d",&n,&x);
for(int i=;i<n;i++){
scanf("%I64d",&arr[i]);
dp[] = max(0ll,dp[]+arr[i]);
dp[] = max(dp[],dp[]+arr[i]*x);
dp[] = max(dp[],dp[]+arr[i]);
ans = max(ans,dp[]); }
printf("%I64d\n",ans);
return ; }
Educational Codeforces Round 63 D. Beautiful Array的更多相关文章
- [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 (Rated for Div. 2) 题解
Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...
- Educational Codeforces Round 63部分题解
Educational Codeforces Round 63 A 题目大意就不写了. 挺简单的,若果字符本来就单调不降,那么就不需要修改 否则找到第一次下降的位置和前面的换就好了. #include ...
- Educational Codeforces Round 63 选做
D. Beautiful Array 题意 给你一个长度为 \(n\) 的序列.你可以选择至多一个子段,将该子段所有数乘上给定常数 \(x\) .求操作后最大的最大子段和. 题解 考虑最大子段和的子段 ...
- Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array (简单DP)
题目:https://codeforces.com/contest/1155/problem/D 题意:给你n,x,一个n个数的序列,你可以选择一段区间,区间的数都乘以x,然后求出最大字段和 思路: ...
- Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array 分类讨论连续递推dp
题意:给出一个 数列 和一个x 可以对数列一个连续的部分 每个数乘以x 问该序列可以达到的最大连续序列和是多少 思路: 不是所有区间题目都是线段树!!!!!! 这题其实是一个很简单的dp 使用的是分 ...
- Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array(动态规划.递推)
传送门 题意: 给你一个包含 n 个元素的序列 a[]: 定义序列 a[] 的 beauty 为序列 a[] 的连续区间的加和最大值,如果全为负数,则 beauty = 0: 例如: a[] = {1 ...
- Educational Codeforces Round 12 E. Beautiful Subarrays 字典树
E. Beautiful Subarrays 题目连接: http://www.codeforces.com/contest/665/problem/E Description One day, ZS ...
- Educational Codeforces Round 11 A. Co-prime Array 水题
A. Co-prime Array 题目连接: http://www.codeforces.com/contest/660/problem/A Description You are given an ...
随机推荐
- SSAS 度量值中的distinct count局聚合方式会数为null的值
我们来看一个例子 Analysis Services: For Distinct Count measure NULL = 0 If you are to look at the table of v ...
- ./redis-trib.rb 报错:/usr/local/rvm/gems/ruby-2.4.2/gems/redis-4.0.1/lib/redis/client.rb:119:in `call': ERR Slot 0 is already busy (Redis::CommandError)
错误提示是 slot插槽被占用了(这是 搭建集群前时,以前redis的旧数据和配置信息没有清理干净.) 解决方案是 用redis-cli 登录到每个节点执行 flushall 和 cluster ...
- web性能优化之GZIP压缩
从服务端优化来说,通过对服务端做压缩配置可以大大减小文本文件的体积,从而使加载文本的速度成倍的加快.目前比较通用的压缩方法是启用gzip压缩.它会把浏览器请求的页面,以及页面中引用的静态资源以压缩包的 ...
- docker_Dockerfile_docker hub简单使用搭建nginx
Docker for Windows是Docker for Microsoft Windows 的Community Edition(CE).要下载Docker for Windows,请前往Dock ...
- mininet+floodlight使用(一)
目录 一 .安装Floodlight 1.下载源和相关工具 2.安装(注意,需要java1.8,否则报错) 3.尝试游览器访问管理界面 4.修复查看不了floodlight 二.Mininet自定义拓 ...
- Linux之CentOS7.5安装及克隆
官网下载地址:https://www.centos.org/,点击Get CentOS Now,点击DVD ISO,选择本国资源Actual Country ,一般选择列表中第一行就可以下载. 这里我 ...
- 【visual studio code 的python开发环境搭建 】
打开vs code,按按F1或者Ctrl+Shift+P打开命令行,然后输入ext install 输入Python,选第一个,这个用的最多,支持自动补全代码等功能,点击安装按钮,即可安装 下面试着编 ...
- 宏观看restframework序列化
序列化 序列化意义 web有两种应用模式,一种是前后端不分离,一种是前后端分离,当前后端分离的时候,后端只需要向前端传输数据即可,不需要进行其他的操作,一般如果是中大型公司,都是前后端分离,这也是目前 ...
- 运维与架构—Nginx的优缺点
Nginx的优点是: 1.工作在网络的7层之上,可以针对http应用做一些分流的策略,比如针对域名.目录结构 它的正则规则比HAProxy更为强大和灵活,这也是它目前广泛流行的主要原因之一 Nginx ...
- python迭代器生成器
1.生成器和迭代器.含有yield的特殊函数为生成器.可以被for循环的称之为可以迭代的.而可以通过_next()_调用,并且可以不断返回值的称之为迭代器 2.yield简单的生成器 #迭代器简单的使 ...