首页 > 其他分享 >D. Running Miles

D. Running Miles

时间:2023-05-06 09:11:13浏览次数:36  
标签:10 run int sights leq Running miles Miles

D. Running Miles

There is a street with $n$ sights, with sight number $i$ being $i$ miles from the beginning of the street. Sight number $i$ has beauty $b_i$. You want to start your morning jog $l$ miles and end it $r$ miles from the beginning of the street. By the time you run, you will see sights you run by (including sights at $l$ and $r$ miles from the start). You are interested in the $3$ most beautiful sights along your jog, but every mile you run, you get more and more tired.

So choose $l$ and $r$, such that there are at least $3$ sights you run by, and the sum of beauties of the $3$ most beautiful sights minus the distance in miles you have to run is maximized. More formally, choose $l$ and $r$, such that $b_{i_1} + b_{i_2} + b_{i_3} - (r - l)$ is maximum possible, where $i_1, i_2, i_3$ are the indices of the three maximum elements in range $[l, r]$.

Input

The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) — the number of test cases.

The first line of each test case contains a single integer $n$ ($3 \leq n \leq 10^5$).

The second line of each test case contains $n$ integers $b_i$ ($1 \leq b_i \leq 10^8$) — beauties of sights $i$ miles from the beginning of the street.

It's guaranteed that the sum of all $n$ does not exceed $10^5$.

Output

For each test case output a single integer equal to the maximum value $b_{i_1} + b_{i_2} + b_{i_3} - (r - l)$ for some running range $[l, r]$.

Example

input

8
1
22
299999996

output

8
1
22
299999996

Note

In the first example, we can choose $l$ and $r$ to be $1$ and $5$. So we visit all the sights and the three sights with the maximum beauty are the sights with indices $1$, $3$, and $5$ with beauties $5$, $4$, and $3$, respectively. So the total value is $5 + 4 + 3 - (5 - 1) = 8$.

In the second example, the range $[l, r]$ can be $[1, 3]$ or $[2, 4]$, the total value is $1 + 1 + 1 - (3 - 1) = 1$.

 

解题思路

  比赛的时候写了个假双指针的做法,然后动态规划又没想到怎么做。其实只要想到把公式变一下就能做出来了。

  首先根据贪心思想,对于区间$[l,r]$,若要让$b_{i_1} + b_{i_2} + b_{i_3} - (r - l)$取到最大值,那么其中两个最大元素必然在区间的两个端点,即$a_l, a_r \in \{ {b_{i_1}, b_{i_2}, b_{i_3}} \}$。否则我们可以通过右移左端点$l$或者左移右端点$r$,使得$[l,r]$中的$b_{i_1}, b_{i_2}, b_{i_3}$不变,而$r-l$变小,那么$b_{i_1} + b_{i_2} + b_{i_3} - (r - l)$就会变大。

  因此,不失一般性,我们假设$i_1 = l$,$i_3 = r$,$i_2 = i \in (l,r)$,那么公式就会变成$b_l + b_i + b_r - (r-l) = a_i + (a_l + l) + (a_r - r)$。可以发现$a_l + l$的取值仅取决于$l$,$a_r - r$的取值仅取决于$r$,因此可以枚举中间元素$a_i$,然后在$l \in [1, i-1]$中求$a_l + l$的最大值,在$r \in [i+1, n]$中求$a_r - r$的最大值。前后缀的最大值可以预处理出来。

  AC代码如下,时间复杂度为$O(n)$:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int N = 1e5 + 10, INF = 0x3f3f3f3f;
 5 
 6 int a[N];
 7 int l[N], r[N];
 8 
 9 void solve() {
10     int n;
11     scanf("%d", &n);
12     l[0] = r[n + 1] = -INF;
13     for (int i = 1; i <= n; i++) {
14         scanf("%d", a + i);
15         l[i] = max(l[i - 1], a[i] + i);
16     }
17     for (int i = n; i; i--) {
18         r[i] = max(r[i + 1], a[i] - i);
19     }
20     int ret = 0;
21     for (int i = 1; i <= n; i++) {
22         ret = max(ret, a[i] + l[i - 1] + r[i + 1]);
23     }
24     printf("%d\n", ret);
25 }
26 
27 int main() {
28     int t;
29     scanf("%d", &t);
30     while (t--) {
31         solve();
32     }
33     
34     return 0;
35 }

 

参考资料

  Codeforces Round #870 (Div. 2) Editorial:https://codeforces.com/blog/entry/115892

标签:10,run,int,sights,leq,Running,miles,Miles
From: https://www.cnblogs.com/onlyblues/p/17375929.html

相关文章

  • Root privileges are required forrunning the Systemback!(转)
    在使用systemback对Linux服务器进行镜像备份时发现无法正常打开,报错显示:RootprivilegesarerequiredforrunningtheSystemback!或者UnsafeWindowauthorization!Pleasedonotuse‘sudo’command.后来发现有两种解决办法:1、获取权限suroot/usr/lib/systemback/sbsustart......
  • nacos报错:Nacos cluster is running with 1.X mode, can't accept gRPC request tempo
    nacos报错:Nacosclusterisrunningwith1.Xmode,can'tacceptgRPCrequesttemporarilynacos报错如下:Causedby:com.alibaba.nacos.api.exception.NacosException:Requestnacosserverfailed:atcom.alibaba.nacos.client.naming.remote.gprc.NamingGrp......
  • MySQL主从复制Slave_IO_Running为No
    主要记录解决问题的过程,为以后发现类似问题提供解决方法的参考。首先查看从机的mysql日志文件:tail/var/log/mysqld.log日志从上往下看,可以很快看到在中间位置上有一个ERROR的标志,后面写得很清楚,我的主机UUID和从机UUID重复了,而这两个UUID在这里要求必须要不相等的,所以我上面......
  • A stop job is running for LSB:start and stop redis_6379
     修改/etc/init.d/redis_6379(stop下红框中内容,格式:$CLIEXEC-a"password" -p$REDISPORTshutdown)  ......
  • 解决 Error running ‘Application‘: Command line is too long.
    一、项目场景:运行刚拉取下来的项目代码,出现下面问题描述的错误提示。二、问题描述Errorrunning'Application':Commandlineistoolong.ShortencommandlineforApplicationoralsoforSpringBootdefaultconfiguration?翻译翻译:运行“Application”时出错:命令行太长......
  • 《Ubuntu — NetworkManager开机提示A start job is running for Network Manager wai
    轉自:https://www.cnblogs.com/zhuangquan/p/13209758.html,僅供參考學習使用1.NetworkManagerUbuntuServer:Ubuntu的Server版本只有终端界面,没有桌面GUI,且Server版本不会安装NetworkManager,所以UbuntuServer网络由配置文件进行配置。由于Server版本一般用作服务器的......
  • 频繁设置CGroup触发linux内核bug导致CGroup running task不调度
    1.说明1>本篇是实际工作中linux上碰到的一个问题,一个使用了CGroup的进程处于R状态但不执行,也不退出,还不能kill,经过深入挖掘才发现是Cgroup的内核bug2>发现该bug后,去年给RedHat提交过漏洞,但可惜并未通过,不知道为什么,这里就发我博客公开了3>前面的2个帖子《极简cfs公平调度算......
  • Ubuntu开机卡“A start job is running for wait for network to be Configured”的解
    问题虚拟机安装ubuntu22.04TLS系统后,开机总会卡在等待网络连接好长时间。卡在AstartjobisrunningforhaitforNetworktobeConfigured(1min40s/no)这里如图所示解决办法进入系统后,打开终端,输入下面命令,cd/etc/systemd/system/network-online.target.wants/......
  • ambari-agent in not running
    ERROR2023-04-0609:24:25,787Controller.py:453-Controllerthreadfailedwithexception:Traceback(mostrecentcalllast): File"/usr/lib/python2.6/site-packages/ambari_agent/Controller.py",line438,inrun   self.actionQueue=ActionQueue(......
  • angular项目启动报Another process, with id 24289, is currently running ngcc.
    在npmbuild时突然停下来,再启动就启不起来了。看报错信息是端口被占用,在任务管理器中也找不到这个端口重启vscode、重启电脑都不好使。。可以通过删除node_modules再重新npminstall解决! ......