题目链接

题解

知识点:贪心,前缀和,枚举。

首先考虑一个贪心结论,选择区间端点一定是两个最大值,因此 \(i_1 = l,i_3 = r\) 。

考虑变形式子 \((b_l + l) + b_{i_2} + (b_r - r)\) ,那我们枚举 \(b_{i_2}\) 只需要知道 \(\{ b_i + i \}\) 的前缀最大值,和 \(\{ b_i - i \}\) 的后缀最大值即可。

注意,这样得到的 \(b_{i_2}\) 不一定是真正的 \([l,r]\) 内的 \(b_{i_2}\) ,但可以肯定的是这样不影响最优解。

时间复杂度 \(O(n)\)

空间复杂度 \(O(n)\)

代码

#include <bits/stdc++.h>
using namespace std;
using ll = long long; int a[100007];
int pre[100007], suf[100007];
bool solve() {
int n;
cin >> n;
for (int i = 1;i <= n;i++) {
cin >> a[i];
pre[i] = a[i] + i;
suf[i] = a[i] - i;
}
suf[n + 1] = -n - 1;
for (int i = 1;i <= n;i++) pre[i] = max(pre[i], pre[i - 1]);
for (int i = n;i >= 1;i--) suf[i] = max(suf[i], suf[i + 1]); int ans = 0;
for (int i = 1;i <= n;i++) ans = max(ans, pre[i - 1] + a[i] + suf[i + 1]);
cout << ans << '\n';
return true;
} int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}

CF1826D Running Miles的更多相关文章

  1. 机器学习经典分类算法 —— k-近邻算法(附python实现代码及数据集)

    目录 工作原理 python实现 算法实战 约会对象好感度预测 故事背景 准备数据:从文本文件中解析数据 分析数据:使用Matplotlib创建散点图 准备数据:归一化数值 测试算法:作为完整程序验证 ...

  2. Crystal Clear Applied: The Seven Properties of Running an Agile Project (转载)

    作者Alistair Cockburn, Crystal Clear的7个成功要素,写得挺好. 敏捷方法的关注点,大家可以参考,太激动所以转载了. 原文:http://www.informit.com ...

  3. Running Dubbo On Spring Boot

    Dubbo(http://dubbo.io/) 是阿里的开源的一款分布式服务框架.而Spring Boot则是Spring社区这两年致力于打造的简化Java配置的微服务框架. 利用他们各自优势,配置到 ...

  4. Android PopupWindow Dialog 关于 is your activity running 崩溃详解

    Android PopupWindow Dialog 关于 is your activity running 崩溃详解 [TOC] 起因 对于 PopupWindow Dialog 需要 Activi ...

  5. IntelliJ运行下载的Servlet时报错 Error running Tomcat 8.5.8: Unable to open debugger port (127.0.0.1:49551): java.net.SocketException

    学习Java Servlet时,从Wrox上下载了示例代码,准备run/debug时发现以下错误: Error running Tomcat 8.5.8: Unable to open debugge ...

  6. teamviewer "TeamViewer Daemon is not running

    执行下面的命令问题解决: # teamviewer --daemon enable enable Sat Jan :: CST Action: Installing daemon () for 'up ...

  7. mysql 有报错  ERROR! MySQL is not running, but lock file (/var/lock/subsys/mysql) exists

    sh-4.1# /etc/init.d/mysqld status ERROR! MySQL is not running, but lock file (/var/lock/subsys/mysql ...

  8. Errors occurred during the build. Errors running builder 'JavaScript Validator' on project

    1.问题:Errors occurred during the build. Errors running builder 'JavaScript Validator' on project 2.解决 ...

  9. The network bridge on device VMnet0 is not running

    The network bridge on device VMnet0 is not running. The virtual machine will not be able to communic ...

  10. ERROR! MySQL is running but PID file could not be found

    /etc/init.d/mysql status提示ERROR! MySQL is running but PID file could not be found先打印MYSQL进程ps aux | ...

随机推荐

  1. 小白学标准库之 log

    日常开发中,日志 log 几乎是必不可少.本文旨在介绍 log 的使用和内部实现等. 1. log 使用及实现 package main import ( "fmt" " ...

  2. NodeJS安装指南(Mac)

    nvm,node,npm之间的区别 nvm:nodejs 版本管理工具. 也就是说:一个 nvm 可以管理很多 node 版本和 npm 版本. nodejs:在项目开发时的所需要的代码库 npm:n ...

  3. SpringMVC02——第一个MVC程序-注解版(high版!!!!)

    注解版 新建一个子项目,添加Web支持 在pom.xml文件中引入相关的依赖:主要引入Spring框架核心库.SpringMVC.servlet,JSTL等. 创建实体类Fruit package c ...

  4. CoreDNS -- DNS服务与服务发现

    CoreDNS -- DNS服务与服务发现 DNS服务器 手册:https://coredns.io/manual/toc/ Github:https://github.com/coredns/cor ...

  5. Linux-软件包管理-rpm-yum-apt

  6. [转帖]Grafana+influxdb+ntopng简易网络流量分析展示系统

    Grafana逼格高,所以用它展示ntopng的数据 >_< 一,ntopng 根据官网资料 https://www.ntop.org/ntop/ntopng-influxdb-and-g ...

  7. [转帖]2.2.1 Lightning 工作原理

    https://book.tidb.io/session2/chapter2/lightning-internal.html TiDB Lightning 工具支持高速导入 Mydumper 和 CS ...

  8. [转帖]缓存与存储的一致性策略:从 CPU 到分布式系统

    https://zhuanlan.zhihu.com/p/151745863 在计算机系统设计实践中,我们常常会遇到下图所示架构: 为了解决单个存储器读吞吐无法满足要求的问题,常常需要在存储器上面增加 ...

  9. [转帖]jmeter命令大全(命令行模式)

    jmeter命令 --? 打印命令行选项并退出 -h. --帮助 打印使用信息和退出 -v. --版本 打印版本信息并退出 -p. --propfile<argument> 要使用的jme ...

  10. [转帖]ioping测试

    https://www.cnblogs.com/ishmaelwanglin/p/10839702.html 一个实时显示磁盘io延时的工具,以类似ping 的输出一样展示输出结果 常用参数: -c ...