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

1826D Running Miles

时间:2023-09-08 09:46:10浏览次数:42  
标签:pre suf int 最大值 1826D 100007 long Running Miles

题目链接

题解

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

首先考虑一个贪心结论,选择区间端点一定是两个最大值,因此 \(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;
}

标签:pre,suf,int,最大值,1826D,100007,long,Running,Miles
From: https://www.cnblogs.com/BlankYang/p/17686682.html

相关文章

  • 牛客——SQL254 统计salary的累计和running_total
    描述按照salary的累计和running_total,其中running_total为前N个当前(to_date='9999-01-01')员工的salary累计和,其他以此类推。具体结果如下Demo展示。。CREATETABLEsalaries(emp_noint(11)NOTNULL,salaryint(11)NOTNULL,from_datedateNOTNULL,to_datedate......
  • CF1826D
    原题翻译这题乍一看不太好做,当时还想了单调栈或改变枚举顺序之类的做法,但都不可做但仔细一想,我们发现答案的\(b_l\)和\(b_r\)一定会被选到,否则我们可以把没用的部分去掉,这样\(b_1+b_2+b_3\)的值不会变,\(r-l\)还会变小,这一步是缩小答案的范围然后我们发现这个条件还是很严格,因......
  • A VNC server is already running as :1
    看到这一行字是不是很崩溃。当输入vncserver-kill:1,输出结果是:Can'tfindfile/home/ubuntu/.vnc/VM-0-11-ubuntu:1.pidYou'llhavetokilltheXtightvncprocessmanually是不是更崩溃。 不过,经过热心网友的分享,且最终亲测有效的解决方法就是:删除这两个文件。rm/......
  • a start job is running for udev wait for complete device initialization
    astartjobisrunningforudevwaitforcompletedeviceinitializationreference:https://github.com/AdnanHodzic/displaylink-debian/issues/331diff/etc/init.d/systemd-udevd+systemctlmasksystemd-udev-settleudevadmtrigger--action=addudevadmsett......
  • The MySQL server is running with the LOCK_WRITE_GROWTH option so it cannot execu
    然后百度参考:TheMySQLserverisrunningwiththeLOCK_WRITE_GROWTHoptionsoitcannotexecutethisstatement_冰尘s1的博客-CSDN博客mysql报错TheMySQLserverisrunningwiththeLOCK_WRITE_GROWTHoptionsoitcannotexecutethisstatem_言默夜雨的博客-CSDN博客......
  • 安装unity2022后启动工程提示“Unity is running as administrator.”
    问题背景:如题,最近项目更新到unity2022.3.6f1版本,在部分机器发现会不停提示“Unityisrunningasadministrator.”解决方案:同网上大多数方案雷同,采用调整uac安全级别来避免。1.搜索栏直接搜控制面板,或者win+r键入control,打开控制面板界面;2.选中“系统和安全”后,点击“更改用......
  • 在Windows中运行Filebeat(Running Filebeat in windows)
     我最近使用这些说明在Windows上设置了filebeathttps://www.elastic.co/downloads/beats/filebeat但它迫使我保持​​cmd提示打开运行命令filebeat.exe-cfilebeat.yml我想知道是否有办法将其作为后台进程运行?谢谢。Isetupfilebeatonwindowsrecentlyusingt......
  • Do you already have another mysqld server running on port: 8008 ?
    实现"Doyoualreadyhaveanothermysqldserverrunningonport:8008?"的步骤概述在解决问题之前,我们先了解一下整个问题的流程。下面是解决问题的步骤:步骤操作1检查是否已经有一个mysqld服务运行在8008端口2如果有,关闭该服务3如果没有,继续其他操作......
  • idea启动总是报错Error running 'Tomcat 9.0.6': Unable to open debugger port (127.
    问题:当遇到idea启动报错"Errorrunning'Tomcat9..6':Unabletoopendebuggerport(127.0.0.1:57757):java.net.SocketException"socketclosed""时,很多人可能会尝试改变debugger的端口来解决问题。但是有时候即使改了几次端口,仍然提示端口被占用,但实际上并没有使用该端口。......
  • invalidate the cache in Spark by running 'REFRESH TABLE tableName' command in SQ
    ...1moreCausedby:java.io.FileNotFoundException:Filedoesnotexist:hdfs://ns1/user/hive/warehouse/dw.db/dw_uniswapv3_position_detail/pk_day=1689552000000/part-00000-bbe52b3b-4963-4c76-9ba9-e315305baed7.c000Itispossibletheunderlyingfileshave......