当前位置:网站首页>AcWing 135. 最大子序和(前缀和 + 单调队列求定长区间最小值)
AcWing 135. 最大子序和(前缀和 + 单调队列求定长区间最小值)
2022-06-12 10:32:00 【Jacob* ̄▽ ̄*】
题意:
给定一个 长度为 n 的整数序列(可能有 负数),从中 找出一段长度不超过 m 的连续子序列,使得 子序列中所有数的和 最大。n、m ≤ 3 * 10 ^ 5
思路:
根据我们的经验,计算 “区间和” 的问题,一般转化为 “两个前缀和相减” 的形式进行求解。
先求出 s[i] 表示序列里前 i 项的和,则 连续子序列 [L,R] 中数的和 就等于 s[R] - s[L - 1]。
那么 原问题可以转化为:找出 两个位置 x, y,使 s[y] - s[x] 最大,并且 y - x ≤ m。
既然将 前缀和数组 s 都求出来了,那么就很好办了:
枚举区间的 右端点
i:从i = 1开始,依次从前往后枚举到i = n。对于 每一个右端点,固定好之后,我们找到一个 左端点
j,其中j ∈ [i - m,i - 1]并且使得s[j]最小(根据 任意一段区间[i,j + 1]的元素和 等于s[i] - s[j],我们要 使区间和最大,显然要 使s[j]尽量小)。
上述的过程不就是一个在每一个 区间长度为 m 的区间 中找到 最小值 吗,最终,我们就将本题转化成了 滑动窗口的经典运用。
将 遍历每一个右端点求出来的最大区间和 求出来,并取 max,即为答案。
时间复杂度:
O ( n ) O(n) O(n)
代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 3e5 + 10;
int a[N], q[N];
int n, m;
int s[N];
int solve()
{
int ans = -INT_MAX;
int hh = 0, tt = 0;
for (int i = 1; i <= n; ++i)
{
if (hh <= tt && i - q[hh] > m) hh++;
ans = max(ans, s[i] - s[q[hh]]);
while (hh <= tt && s[q[tt]] >= s[i]) --tt;
q[++tt] = i;
}
return ans;
}
signed main()
{
int T = 1; //cin >> T;
while (T--)
{
cin >> n >> m;
for (int i = 1; i <= n; ++i)
{
scanf("%lld", &a[i]);
s[i] = s[i - 1] + a[i];
}
cout << solve() << '\n';
}
return 0;
}
边栏推荐
- How to upload the video on the computer to the mobile phone without network
- 蓝桥杯2015年CA省赛(填坑中)
- Tp6+memcached configuration
- Simple use of autojs
- PHP interface generates cache and MD5 encryption uniformly
- Failed to load resource: the server responded with a status of 413 (Request Entity Too Large)
- Common methods of string class
- Binassii module - converting between binary and ASCII
- 93. 獲得內網的所有IP地址
- Building 64 bit wampserver and DVWA in win7 virtual machine
猜你喜欢

2022 Taobao 618 Super Cat Games introduction 618 super cat games playing skills

Code types and data structures corresponding to the five object types of redis

【实验】MySQL主从复制及读写分离

Telecommuting with cpolar (2)

ASP. Net core permission system practice (zero)

How to play the 2022 Taobao 618 Super Cat Games? Playing skills of 2022 Taobao 618 Cat Games

MySQL performance test (slow query log)

2022淘宝618超级喵运会怎么玩?2022淘宝618喵运会玩法技巧

This and final keywords

Malicious code analysis practice -- using apatedns and inetsim to simulate network environment
随机推荐
【MySQL】sql_ Model mode
How to play the 618 super cat games on Taobao? Here comes the introduction to the overall activities of the Super Cat Games
93. obtain all IP addresses of the Intranet
How to refund the pre-sale deposit of JD 618 in 2022? Can JD 618 deposit be refunded?
Win10 professional edition user name modification
The solution of Lenovo notebook ThinkPad t440 WiFi unable to connect to the Internet
PHP interface generates cache and MD5 encryption uniformly
PHP get (remote) large file method record
PHP wechat red packet allocation logic
3. Abstract Factory
session_ start(): Cannot send session cache limiter - headers already sent
CTF freshman cup PHP deserialization question - EzPop
2022 Taobao 618 Super Cat Games introduction 618 super cat games playing skills
Building 64 bit wampserver and DVWA in win7 virtual machine
【实验】MySQL主从复制及读写分离
How Qualcomm platform modifies special voltage
What can QA do in a "de QA" project?
Malicious code analysis practice - lab03-01 Exe basic dynamic analysis
Remote desktop cannot copy and paste solution
蓝桥杯2015年CA省赛(填坑中)

