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 ...
随机推荐
- 杂记(那些我还容易混淆的c和c++知识)
1: 定义一个对象时先调用基类的构造函数.然后调用派生类的构造函数:析构的时候恰好相反:先调用派生类的析构函数.然后调用基类的析构函数.2: 多态性具体体现在运行和编译两个方面:在程序运行时的多态性 ...
- Js读取Excel
1,使用此代码,一定将浏览器中跟ActiveX有关的功能都开启!同时将站点添加到可信任的站点中. function getCount(filePath) { var cn = new ActiveXO ...
- VMware虚拟机安装Centos7图文教程
CentOS 是一个工业标准的 Linux 发行版,是红帽企业版 Linux 的衍生版本.你安装完后马上就可以使用,但是为了更好地使用你的系统,你需要进行一些升级.安装新的软件包.配置特定服务和应用程 ...
- .net core 实践笔记(二)--EF连接Azure Sql
** 温馨提示:如需转载本文,请注明内容出处.** 本文链接:https://www.cnblogs.com/grom/p/9902098.html 笔者使用了常见的三层架构,Api展示层注入了Swa ...
- 12c数据库重启后自动启动pdb
由于最近管理12c数据库,创建了9个新的pdb.相对于以前11g版本每天一个一个的环境检查,方便了很多. 但是因为一次意外宕机,数据库重启.虽然数据库重启,但是管理的pdb确不能自动重启,所以需要手动 ...
- CORS跨域实现思路及相关解决方案
本篇包括以下内容: CORS 定义 CORS 对比 JSONP CORS,BROWSER支持情况 主要用途 Ajax请求跨域资源的异常 CORS 实现思路 安全说明 CORS 几种解决方案 自定义CO ...
- c#将List转换成DataTable
前面写了一篇List<T>转换成DataTable,这里主要是完善了前面的代码. 同样使用了emit,我把代码整理后上传了git. 另外增加了特性的设计. 设计了三类特性ColumnTyp ...
- python实现用户登录问候
创建一个至少包含 5个用户名的列表,且其中一个用户名为 'admin' .想象你要编写代码,在每位用户登录网站后都打印一条问候消息.遍历用户名列表,并向每位用户打印一条问候消息.添加一条 if 语句, ...
- 完美解决 Cydia 不能上网
国行手机比美版.港版.韩版手机新增了网络授权的功能,iOS 10 及以上系统版本,任何应用首次打开,如果有请求网络的行为,都会提示网络请求授权的对话框. 首次打开 Cydia 并没有提示网络请求授权的 ...
- Easy-UI中datebox的默认显示当前日期的最简单的两种方法
在中有一个Today按钮就是实现显示当前日期,所以我们在src/jquery.datebox.js文件中可以找到currentText:'Today'.所以我们可以使用'currentText'和'T ...