题目传送门

题目大意

给出一个长度为 \(n\) 的单调不减的序列,每次可以选出一个点,产生贡献 \(a_k\),我们可以得知我们需要找到的数是否大于 \(k\)。问找到要找到的数最小花费。

\(n\le 7100\)

思路

\(\Theta(n^3)\) 的 \(\text{dp}\) 显然,可以设 \(f_{l,r}\) 表示答案在 \([l,r]\) 区间时找到答案最小贡献。可以得到转移式:

\[f_{l,r}=\min_{l\le k\le r}\{\max(f_{l,k},f_{k+1,r})+a_k\}
\]

然后我们经过思考,发现以下事情:

  • \(f(l,r)\ge f(l,r-1),f(l,r)\ge f(l+1,r)\)

  • 我们如果设 \(w_{l,r}\) 表示第一个满足 \(f_{l,k}\ge f_{k+1,r}\) 的 \(k\),那么可以发现对于相同的 \(r\),\(w_{l,r}\) 随着 \(l\) 的减小而减小。

于是,我们可以把问题拆成两个部分进行考虑。

  1. \(k< w_{l,r}\)

对于这一部分,我们观察到式子可以改写为:

\[f_{l,r}=\min_{l\le k<w_{l,r}}\{f_{k+1,r}+a_k\}
\]

然后你发现这个式子可以使用单调队列进行优化。

  1. \(k\ge w_{l,r}\)

对于这一部分你发现式子可以改写为:

\[f_{l,r}=\min_{w_{l,r}\le k\le r}\{f_{l,k}+a_k\}
\]

然后你发现 \(f_{l,k}+a_k\) 随着 \(k\) 的增大而增大,所以最优贡献点一定是在 \(w_{l,r}\)。


综上,可以枚举 \(r\) 然后使用单调队列优化即可,时间复杂度 \(\Theta(n^2)\)。

\(\texttt{Code}\)

#include <bits/stdc++.h>
using namespace std; #define Int register int
#define int long long
#define MAXN 7105 template <typename T> inline void read (T &t){t = 0;char c = getchar();int f = 1;while (c < '0' || c > '9'){if (c == '-') f = -f;c = getchar();}while (c >= '0' && c <= '9'){t = (t << 3) + (t << 1) + c - '0';c = getchar();} t *= f;}
template <typename T,typename ... Args> inline void read (T &t,Args&... args){read (t);read (args...);}
template <typename T> inline void write (T x){if (x < 0){x = -x;putchar ('-');}if (x > 9) write (x / 10);putchar (x % 10 + '0');} int n,head,tail,a[MAXN],q[MAXN],f[MAXN][MAXN]; void ins (int k,int r){
while (head <= tail && f[r][q[tail] + 1] + a[q[tail]] >= f[r][k + 1] + a[k]) -- tail;
q[++ tail] = k;
} signed main(){
int T;read (T);
while (T --> 0){
read (n);
for (Int i = 1;i <= n;++ i) read (a[i]);
for (Int r = 2;r <= n;++ r){
q[head = tail = 1] = r - 1,f[r][r - 1] = a[r - 1];int res = r;
for (Int l = r - 2;l >= 1;-- l){
while (f[res - 1][l] > f[r][res] && res > l) -- res;
while (head <= tail && q[head] >= res) ++ head;
f[r][l] = f[res][l] + a[res];
if (head <= tail) f[r][l] = min (f[r][l],f[r][q[head] + 1] + a[q[head]]);
ins (l,r);
}
}
write (f[n][1]),putchar ('\n');
}
return 0;
}

题解 [SBCOI2020] 一直在你身旁的更多相关文章

  1. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

  2. noip2016十连测题解

    以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...

  3. BZOJ-2561-最小生成树 题解(最小割)

    2561: 最小生成树(题解) Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1628  Solved: 786 传送门:http://www.lyd ...

  4. Codeforces Round #353 (Div. 2) ABCDE 题解 python

    Problems     # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring P ...

  5. 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解

    题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...

  6. 2016ACM青岛区域赛题解

    A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  7. poj1399 hoj1037 Direct Visibility 题解 (宽搜)

    http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...

  8. 网络流n题 题解

    学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...

  9. CF100965C题解..

    求方程 \[ \begin{array}\\ \sum_{i=1}^n x_i & \equiv & a_1 \pmod{p} \\ \sum_{i=1}^n x_i^2 & ...

随机推荐

  1. Learning ROS: Getting started with roswtf (检查ROS系统,找出问题)

    本文主要部分来源于ROS官网的Tutorials. roswtf是ROS的检查工具,用于检查ROS安装和运行系统. Checking your installation&Offline mak ...

  2. 使用vbs调用excel中的宏

    使用vbs打开excel文件,并且传递参数调用excel中的macro,自动化完成excel文件的制作. Set oExcel = createobject("Excel.Applicati ...

  3. Sentry For React 完整接入详解(2021 Sentry v21.8.x)前方高能预警!三万字,慎入!

    内容源于:https://docs.sentry.io/platforms/javascript/guides/react/ 系列 1 分钟快速使用 Docker 上手最新版 Sentry-CLI - ...

  4. (七)羽夏看C语言——模板(C++)

    写在前面   由于此系列是本人一个字一个字码出来的,包括示例和实验截图.本人非计算机专业,可能对本教程涉及的事物没有了解的足够深入,如有错误,欢迎批评指正. 如有好的建议,欢迎反馈.码字不易,如果本篇 ...

  5. JUnit5快速入门指南-2

    重复测试中容易产生的问题 //结果类 private static int result = 0; public static int count(int x) throws InterruptedE ...

  6. ipsec.conf配置文件多个保护子网解析流程

    Author       : Email         : vip_13031075266@163.com Date          : 2021.01.23 Copyright : 未经同意不得 ...

  7. vue 进入页面每次都调用methods里的方法

    // 监听路由,每次进入页面调用方法,放在method里 mounted(){ this.getPath() }, methods: { getPath(){ console.log(this.$ro ...

  8. [第十篇]——Docker 容器连接之Spring Cloud直播商城 b2b2c电子商务技术总结

    Docker 容器连接 前面我们实现了通过网络端口来访问运行在 docker 容器内的服务. 容器中可以运行一些网络应用,要让外部也可以访问这些应用,可以通过  -P 或  -p 参数来指定端口映射. ...

  9. Android学习记录(一)——安装Android Studio

    "工欲善其事必先利其器"学习安卓开发的第一步,安装Android Studio. 一.什么是Android Studio? Android Studio 是谷歌推出的一个Andro ...

  10. phpstorm一直 updating indices刷新

    解决方法: File-> 选中 Invalidate Caches/Restart  ->选中 Invalidate Caches/Restart