当前位置:网站首页>AcWing 135. Maximum subsequence sum (prefix sum + monotone queue to find the minimum value of fixed length interval)
AcWing 135. Maximum subsequence sum (prefix sum + monotone queue to find the minimum value of fixed length interval)
2022-06-12 10:40:00 【Jacob* ̄▽ ̄*】


Pre knowledge : Monotone queue to find the minimum value of sliding window
The question :
Given a The length is n Integer sequence of ( There may be negative ), from Find a length that does not exceed m A continuous subsequence of , bring The sum of all numbers in a subsequence Maximum .n、m ≤ 3 * 10 ^ 5
Ideas :
According to our experience , Calculation “ Interval and ” The problem of , Generally, it turns into “ Two prefixes and subtraction ” In the form of .
Find out first s[i] It means before in the sequence i Sum of items , be Continuous subsequences [L,R] Sum of median numbers Is equal to s[R] - s[L - 1].
that The original problem can be transformed into : find Two positions x, y, send s[y] - s[x] Maximum , also y - x ≤ m.
Since it will Prefixes and arrays s It's all worked out , So it's easy :
Enumerating interval Right endpoint
i: fromi = 1Start , Enumerate from front to backi = n.about Each right endpoint , After fixing , We found one Left end point
j, amongj ∈ [i - m,i - 1]And makess[j]Minimum ( according to Any interval[i,j + 1]Elements and be equal tos[i] - s[j], We want to Maximize the sum of intervals , Obviously sends[j]As small as possible ).
The above process is not just one in each The interval length is m The range of Find minimum value Do you , Final , We will turn this question into Classic use of sliding windows .
take Traverse each right endpoint to find the maximum interval sum Find out , And take max, Is the answer .
Time complexity :
O ( n ) O(n) O(n)
Code :
#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;
}
边栏推荐
- Common port description
- AcWing 41. 包含min函数的栈(单调栈)
- 【机器学习】基于鸢尾花(iris)数据集的逻辑回归分类实践
- Unable to load dynamic library ‘oci8_ 12C 'or unable to load dynamic library' PDO_ OCI 'or cannot find module
- 在一个“去QA化”的项目中,QA能做什么?
- 淘宝618超级喵运会怎么玩?超级喵运会整体活动攻略来了
- JS scale down the width and height of the picture
- Find the location of a function in PHP
- 淺談調和形狀上下文特征HSC對3DSC的改進
- The most detailed explanation of the top ten levels of sqli labs platform
猜你喜欢

Amélioration de la 3dsc par HSC

How to refund the pre-sale deposit of JD 618 in 2022? Can JD 618 deposit be refunded?

深度学习与CV教程(14) | 图像分割 (FCN,SegNet,U-Net,PSPNet,DeepLab,RefineNet)

使用cpolar远程办公(2)

A snare - Cookie spoofing

Bug easily ignored by recursion

蓝桥杯2015年CA省赛(填坑中)

AcWing 135. 最大子序和(前缀和 + 单调队列求定长区间最小值)

Properties Chinese garbled code

2022淘宝618超级喵运会怎么玩?2022淘宝618喵运会玩法技巧
随机推荐
Why check the @nonnull annotation at run time- Why @Nonnull annotation checked at runtime?
人脸识别pip 安装dlib库失败
What can QA do in a "de QA" project?
A snare - Cookie spoofing
MySQL performance test (slow query log)
PHP wechat red packet allocation logic
AcWing 132. 小组队列(队列模拟题)
The name of a great man
Leetcdoe 2037. 使每位学生都有座位的最少移动次数(可以,一次过)
深度学习与CV教程(14) | 图像分割 (FCN,SegNet,U-Net,PSPNet,DeepLab,RefineNet)
Remote link software xshell and xftp download address
JS obtains the time period of this week and last week (one time period is from Monday to Sunday)
Vite Basics
Leetcode 2169. Get operands of 0
蓝桥杯2015年CA省赛(填坑中)
Leetcode2154. 将找到的值乘以 2(二分查找)
容器江湖的爱恨情仇
Collation of common functions in JS
Telecommuting with cpolar (2)
Solution to invalid small program scroll into view