zoukankan
html css js c++ java
带长度限制的最大子段和(单调队列)
题意: 一组数,求最大的长度不超过K的子段的数值和。 思路: 算法课作业题,按理说应该要很快切,结果甚至自以为是的写了个滑窗假算法(还过了...),被HD大佬(吊人)告知后才发现。于是想了个单调队列的做法,应该是正解.虽然一般不记水题,但羞耻max以至于想记下来. 处理前缀和sum,最大的子段和即 sum
j
- sum
i-1
的差要最大.那么枚举j,对于每一个j找到合法的i且 sum
i-1
最小,用这个差尝试更新答案.找i的过程用单调队列可以O(1)做到.总之就是用单调队列把n
2
的枚举优化为n. ```C++ #include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define dd(x) cout<<#x<<" = "<
P; typedef vector
V; typedef map
M; typedef set
S; typedef queue
Q; typedef priority_queue
BQ; typedef priority_queue
,greater
> SQ; const int maxn=1e5+10,mod=1e9+7,INF=0x3f3f3f3f; int sum[maxn],q[maxn],h,t; int main() { int n,k; scanf("%d%d",&n,&k); for (int i=1;i<=n;++i) { int x; scanf("%d",&x); sum[i]=sum[i-1]+x; } int ans=-INF,ansl,ansr; for (int i=1;i<=n;++i) { while (h
k) ++h; while (h
ans) ans=sum[i]-sum[q[h]-1],ansl=q[h],ansr=i; } printf("%d %d %d",ans,ansl,ansr); return 0; } ``` (希望这份不是假算法了,毕竟那10个点太水了)
查看全文
相关阅读:
基于麦克风阵列的声源定位算法之GCC-PHAT
Parametric and Non-parametric Algorithms
MATLAB中运算符优先级
[HAOI2018]染色
[SHOI2016]黑暗前的幻想乡
[SCOI2012]滑雪
[PA2014]Kuglarz
Stroll
[SDOI2010]大陆争霸
解决IDEA Gradle构建报错"Cause: zip END header not found"
原文地址:https://www.cnblogs.com/orangee/p/9808095.html
最新文章
Tensorflow2.0笔记25——断点续训,存取模型
Tensorflow2.0笔记24——数据增强,增大数据量
Tensorflow2.0笔记23——观察数据集数据结构,配成特征标签对
Tensorflow2.0笔记22——回顾
Boosting and AdaBoost
Bagging and Random Forest
Support Vector Machines
Classification and Decision Trees
Naive Bayes Algorithm
K-Nearest Neighbors Algorithm
热门文章
Learning Vector Quantization
【album】Python使用笔记
Discrete Cosine Transform
Deep Learning 简介
Linear Discriminant Analysis Algorithm
Logistic Regression Algorithm
机器学习之正则化技术
Tips on Probability Theory
Bias, Variance and the Trade-off
Tips on Acoustic Signal Processing
Copyright © 2011-2022 走看看