---
id: 205
title: "CH1201:最大子序和题解"
slug: ch1201-max-subsequence-sum
lang: zh
date: 2018-11-25T20:57:26
date_gmt: 2018-11-25T12:57:26
modified: 2018-11-25T21:05:37
status: publish
password_protected: false
category: { name: Olympiad in Informatics, slug: oi }
tags: [OI, NOIP, 优先队列]
original_url: https://kalorona.com/oi/ch1201最大子序和题解/
archived: 2026-11-27
editor: classic
media: []
archive_notes:
  - "Body heading was <h1> in the original; demoted to level 2 so the page title is the only h1."
  - "The two Enlighter code blocks had titles (data-enlighter-title \"数据区\" and \"单调队列核心代码\") shown in the code block header; the archive format has no code titles, so they were dropped."
  - "The full code was a GitHub Gist <script> embed (https://gist.github.com/kaloronahuang/d58747f88c0d6369d70ffd4d291af221.js); replaced with a plain link to the gist page, per the embed rule. A copy of the gist's raw code as of archiving was saved to _work/205/gist-raw.txt."
comments: []
---

## Solve this Problem with the Priority Queue

单调队列的主要目标其实跟单调栈的主要目标比较相似，都是**在一个单调问题中，直接排除一些可以排除的答案进行运算**。

在本题中，我们会采用单调队列来解决。首先，我们采用前缀和来进行快速计算。然后，在前缀的前提下，我们会在本题中使用一个策略，那就是：如果有两个位置的前缀和$sum[i]>sum[j],其中i<j$，那么处于$i$位置的元素对于我们来讲，是毫无用处的。原因是，如果我们要在$m$长度内获得和最大的子序列，对于一个这样前缀和又大、又靠前的元素，最优解绝不是由这个元素组成的。

我们来用几个代码块来简述我们的策略。

<!--more-->

```cpp
// ll stands for long long
const int maxn = 300200;
ll n, arr[maxn], sum[maxn], m, q[maxn];
```

声明了几个变量，not a big deal.

```cpp
ll l = 1, r = 1, ans = -1e9;
q[1] = 0;
for (int i = 1; i <= n; i++)
{
    while (l <= r && q[l] < i - m)
        l++;
    ans = max(ans, sum[i] - sum[q[l]]);
    while (l <= r && sum[q[r]] >= sum[i])
        r--;
    q[++r] = i;
}
```

我们首先定义队头、队尾，还有一个无穷小的答案。之后，我们来枚举元素$i$。我们在循环中首先淘汰掉那些距离超过$m$的元素。之后我们来记录答案。然后就到我们实施策略的时候了。我们淘汰掉那些不属于最优解的答案。之后，我们记录在位置$++r$的答案为$i$。

完整代码：[https://gist.github.com/kaloronahuang/d58747f88c0d6369d70ffd4d291af221](https://gist.github.com/kaloronahuang/d58747f88c0d6369d70ffd4d291af221)
