当前位置:网站首页>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;
}
边栏推荐
- Get all listening events in the element
- Malicious code analysis practice - lab06-01 exe~Lab06-04. Exe dynamic analysis
- Set SVG color
- Composer command
- Properties Chinese garbled code
- 【CF1392D】D. Omkar and Bed Wars(环形与后效性dp)
- JS pull-up loading more problems encountered in normal execution
- JS string combination
- 2022淘宝618超级喵运会怎么玩?2022淘宝618喵运会玩法技巧
- Telecommuting with cpolar (2)
猜你喜欢

Machine learning is not something you can use if you want to use it

Valentina Studio Pro for Mac(mac数据库管理软件)

A hundred secrets and a few secrets - Caesar encryption

Malicious code analysis practice - lab03-01 Exe basic dynamic analysis

properties中文乱码

Common methods of string class

Leetcode 2169. Get operands of 0

Malicious code analysis practice -- using apatedns and inetsim to simulate network environment

浅谈调和形状上下文特征HSC对3DSC的改进

Fiddler automatically saves the result of the specified request to a file
随机推荐
How to play the 2022 Taobao 618 Super Cat Games? What are the strategies for the Super Cat Games
PHP: seven cattle cloud upload file
Amélioration de la 3dsc par HSC
JS open common application market
Pagoda chevereto1.6.2 the latest version of stepping on the pit tutorial in Chinese
Error during session start; please check your PHP and/or webserver log file and configure your PHP
Mysql5.6.24 installation free deployment method
Leetcode2154. 将找到的值乘以 2(二分查找)
人脸识别pip 安装dlib库失败
session_ start(): Cannot send session cache limiter - headers already sent
Composer command
Collation of common functions in JS
Immer source code reading
Machine learning is not something you can use if you want to use it
MYSQL——内置函数
2022 Taobao 618 Super Cat Games introduction 618 super cat games playing skills
k53.第二章 基于二进制包安装kubernetes v1.22 --集群部署
Tp6+memcached configuration
k52.第一章 基于kubeadm安装kubernetes v1.22 -- 集群部署
Common configuration commands for Cisco network device security management