当前位置:网站首页>AcWing 135. Maximum subsequence sum
AcWing 135. Maximum subsequence sum
2022-07-18 15:45:00 【hys__ handsome】
AcWing 135. The largest subsequence and
Ideas
- Computing intervals and problems are generally transformed into prefixes and problems , So the original problem can be transformed into finding two positions l l l And r r r bring a [ r ] − a [ l − 1 ] a[r]-a[l-1] a[r]−a[l−1] The biggest and r − l + 1 < = m r-l+1 <= m r−l+1<=m.
- First, let's enumerate i i i, When i i i When fixed as the right endpoint , The problem turns into finding a left endpoint j j j, j j j The scope is [ i − m + 1 , i − 1 ] [i-m+1,i-1] [i−m+1,i−1], also a [ j − 1 ] a[j-1] a[j−1] Minimum ( Because to make i i i To j j j And a [ i ] − a [ j − 1 ] a[i]-a[j-1] a[i]−a[j−1] Maximum ).
- At this time, it is easy to think of monotonous queues ( q u e que que The subscript queue ): The value at the end of the team a [ q u e [ t − 1 ] ] a[que[t-1]] a[que[t−1]] Greater than or equal to a [ i ] a[i] a[i] You can pop up the tail of the team , because i The subscript is larger ( It's not easy to be eliminated ) And the value is smaller . Then the value of the team leader must be the smallest , What we should pay attention to here is , When updating the right endpoint, you need to judge whether the queue header pops up ( keep i − q u e [ t − 1 ] + 1 < = m i-que[t-1]+1 <= m i−que[t−1]+1<=m ).
#include<iostream>
#define int long long
using namespace std;
const int N = 300010;
int a[N],que[N],h,t;
signed main(){
int n,m; cin>>n>>m;
for(int i = 1; i <= n; i++) {
int x; cin>>x;
a[i] = a[i-1] + x;
}
int res = -0x3f3f3f3f;
h = 0,t = 1;
for(int i = 1; i <= n; i++){
if(h < t && i - que[h] > m) h++;
res = max(res, a[i] - a[que[h]]);
while(h < t && a[que[t-1]] >= a[i]) t--;
que[t++] = i;
}
cout<<res;
return 0;
}
Reference resources
https://www.acwing.com/solution/content/28015/
边栏推荐
- cron 表达式周一到周五执行以及只有周六周天执行
- gradle
- Vs2019 inline assembly development
- UE4/5中的旋转:三个欧拉角Picth、Yaw、Roll及FRotator
- Find active SQL connections in SQL Server
- Remove duplicate elements in the list max min key
- ncnn 编译与使用 pnnx 编译与使用
- CV2. Setmousecallback() displays the pixel value and position of the mouse click image
- 澳大利亚规模领先的鞋业公司与Boomi合作以加快电子商务和转型步伐
- Comparison table of wireless transmission technical parameters of Internet of things
猜你喜欢

Optimization of AI model structure based on nvidiagpu

Characteristics of Lora technology in wireless communication

CV2. Setmousecallback() displays the pixel value and position of the mouse click image

Iowait understanding

Build your own website (23)

【延期召开】2022年网络与信息安全国际会议(NISecurity 2022)

Vulnhub-dc9 learning notes

Detailed explanation of the decision table method of common test case design methods
![[Arduino shakes hands with mpu6050]](/img/c0/8b3df8747afecf150c741957dcbbcc.png)
[Arduino shakes hands with mpu6050]

PostgreSQL 现在安装
随机推荐
Find active SQL connections in SQL Server
ESRI launches indoor positioning system for facility routing
Domestic postman tool, easy to use!
Comparison table of wireless transmission technical parameters of Internet of things
PostgreSQL 现在安装
Redis configuration, cluster installation and capacity expansion
[halcon] writeimage save image crash
Data storage [C language]
澳大利亚规模领先的鞋业公司与Boomi合作以加快电子商务和转型步伐
从小米10发布来看编译优化
JS Object. keys()
What is a recommendation system?
高性能定時器
史上最全mysql!拼命整理
2022 robocom world robot developer competition - undergraduate group (provincial competition) T4, T5
1111111111111
one trillion and one hundred and eleven billion one hundred and eleven million one hundred and eleven thousand one hundred and eleven
Detailed explanation of C language "address book"
[batch dos-cmd command - summary and summary] - symbolic link, hard link, soft link, directory link (mklink)
cas(Compare-and-Swap)