题目链接

题解

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

首先考虑一个贪心结论,选择区间端点一定是两个最大值,因此 \(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. Android——共享参数SharedPreferences

    4数据存储 共享参数SharedPreferences.数据库SQLite.SD卡文件.App的全局内存 4.1共享参数SharedPreferences SharedPreferences是一个轻量 ...

  2. Go-错误栈信息

    Go中错误栈信息 .\main.go:22:2: number1 declared but not used .\main.go 错误发生的文件 22:2 文件的22行第2列 number1 decl ...

  3. Kubeadm 安装支持IPV6 K8S1.28.x的简单过程

    Kubeadm 安装支持IPV6 K8S的简单过程 背景 手贱 找了一个晚上想尝试安装一个K8S集群 并且可以支持IPV6 协议栈的 然后就开始各种百度. 各种处理 找到了一堆歪门邪道. 但是还不知道 ...

  4. [转帖]使用 TiDB 读取 TiFlash

    https://docs.pingcap.com/zh/tidb/stable/use-tidb-to-read-tiflash 本文档介绍如何使用 TiDB 读取 TiFlash 副本. TiDB ...

  5. [转帖]tidb 如何对 TiDB 进行 TPC-C 测试

    https://docs.pingcap.com/zh/tidb/stable/benchmark-tidb-using-tpcc TPC-C 是一个对 OLTP(联机交易处理)系统进行测试的规范,使 ...

  6. [转帖]MySQL ALTER TABLE: ALTER vs CHANGE vs MODIFY COLUMN

    https://www.cnblogs.com/pachongshangdexuebi/p/5029152.html ALTER COLUMN 语法: ALTER [COLUMN] col_name ...

  7. [转帖]py_innodb_page_info.py工具使用

    目录 一.Linux安装Python3 1. 解压包 2. 安装环境 3. 生成编译脚本 4. 检查python3.10的编译器 5. 建立Python3和pip3的软链 6. 添加到PATH 7.  ...

  8. 疯狂GC的第二种处理方式-ChatGPT的学习之四

    疯狂GC的第二种处理方式-ChatGPT的学习之四 摘要 上一个脚本太复杂了. 而且要改启动脚本. 课间休息跟人扯淡聊起来 chatGPT 发现他的语法很有用 但是思路不太对. 不过突然根据文档里写的 ...

  9. [转帖]浅谈Redis大Key与热Key

    https://www.cnblogs.com/jelly12345/p/16424080.html 如何定义大 Key 和 热 Key 如何定义大 Key 如何定义热 Key 大 Key 和 热 K ...

  10. [转帖]APIServer dry-run and kubectl diff

    https://kubernetes.io/blog/2019/01/14/apiserver-dry-run-and-kubectl-diff/ Monday, January 14, 2019 A ...