前缀和(1)——详解一维前缀和(建议收藏!!)

【tessy】软件环境配置:Keil C51 Keil uVision UVSC (Default) 本文详细说明了tessy软件如何配置【Keil C51 Keil uVision UVSC (Default)】环境,具体操作步骤和注意事项 阅读详情

一维前缀和的定义

我们设前缀和数组是sum数组,输入的数组是a,数组的长度是n。

那么代表的就是a数组的前i项之和。

代码如下:

#include<bits/stdc++.h>
using namespace std;
int n,a[10001],sum[10001];
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i],sum[i]=sum[i-1]+a[i];
    cout<<"前缀和数组是:"<<endl;
    for(int i=1;i<=n;i++)cout<<sum[i]<<' '; 
    return 0;
}

结果如下:

一维前缀和的作用

一维前缀和的定义我们讲完了,那它有什么作用呢?

它的作用是可以用的时间复杂度来求出一段数的和。

比如我们输入一个数组,输出第个数字的和。

我们用暴力做是这样的:

#include<bits/stdc++.h>
using namespace std;
int n,a[10001],x,y,sum;
int main()
{
    cin>>n>>x>>y;
    for(int i=1;i<=n;i++)cin>>a[i];
    for(int i=x;i<=y;i++)sum+=a[i];
    cout<<sum;
    return 0;
}

输出结果如下:

其实我们如果用前缀和的话,就可以用一句话就可以解决这个问题:

为什么??????

我们画两个图:

将,就得到了:

代码如下:

#include<bits/stdc++.h>
using namespace std;
int n,a[10001],x,y,sum[10001];
int main()
{
    cin>>n>>x>>y;
    for(int i=1;i<=n;i++)cin>>a[i],sum[i]=sum[i-1]+a[i];
    cout<<sum[y]-sum[x-1];
    return 0;
}

在很多要输出一个区间和的这种题目下,前缀和绝对是最快的!

2、机器人控制技术:阻抗/导纳控制与强化学习的融合 本文探讨了阻抗/导纳控制与强化学习在机器人控制中的融合应用。阻抗/导纳控制广泛应用于外骨骼和跟踪控制,通过改进方案提升建模误差下的鲁棒性;强化学习则为解决维度灾难、实现最优交互提供了有效途径,尤其在大输入空间、连续时间系统、冗余机器人及鲁棒控制中表现突出。结合近似器与合理奖励设计,强化学习可在无模型情况下实现高效控制。未来,两者融合有望推动更智能、更安全的机器人系统发展。 阅读详情

相关推荐

Halcon深度图转点云实战:从XYZ映射到3D可视化全解析

本文详细解析了如何使用Halcon将深度图转换为点云图,涵盖从XYZ映射到3D可视化的全流程。通过实战案例和代码示例,介绍了深度图预处理、xyz_to_object_model_3d核心转换及3D可视化技巧,适用于工业检测、三维重建等场景。

stem5的博客 770

前缀和(一维)

前缀和前言一、前缀和是什么?二、一维前缀和讲解举例说明总结 前言 空间复杂度对代码运行的时间有着很重要的 作用,在代码中适当的用前缀和来进行维护 可以很好地节约时间复杂度。 一、前缀和是什么? 前缀和,通俗的说:对存有一些数字的某个数组而言,前缀就是指的其数组的前k项相加的值,因此对应的前缀和就是数组前k项的和。对一个数组,问[L,R]的区间和(从第L项到第R项的值相加的和),就可以利用前缀和进行求解。 二、一维前缀和讲解 一维前缀和大概就是:有一个由N个数字组成的数组arr[ ],对其进行.

weixin_63734305的博客 1375

SolidWorks与Python二次开发实战:从宏录制到自动化脚本

本文详细介绍了利用Python对SolidWorks进行二次开发的实战方法。通过宏录制获取操作指令,再将其转换为Python脚本,工程师可以高效实现建模、出图等重复任务的自动化,显著提升设计效率。文章涵盖了环境搭建、代码转换、调试技巧及完整实战案例,为工程师提供了从入门到精通的完整路径。

weixin_29082143的博客 969

一维前缀和

一维前缀和

SolitudeAlma的博客 2337

二、声明式 Pipeline 语法

选择型参数,一般用于给定几个可选的值,然后选择其中一个进行赋值,例如:parameters { choice(name: ‘CHOICES’, choices: [‘one’, ‘two’, ‘three’], description: ‘’) },表示定义一个名为 CHOICES 的变量,可选的值为 one、two、three;默认情况下,如果定义了某个 stage 的 agent,在进入该 stage 的 agent 后,该 stage 的 when条件才会被评估,但是可以通过一些选项更改此选项。

Cyan_Jiang的博客 1226

CEH Practical 实战考试真题与答案

CEH Practical 是 EC-Council 推出的Certified Ethical Hacker(CEH)认证项目中的一项高级动手实践考试。它不同于传统的理论考试,侧重于在真实环境中检验考生的实操能力。

sgafdgdagd的博客 981

【算法基础】一维前缀和 + 二维前缀和

图文详讲前缀和

Weraphael的博客 3849

Mamba vs YOLO:目标检测选型指南(附实际项目对比)

本文深入对比了Mamba和YOLO在目标检测领域的性能差异与应用场景。通过实际项目数据,分析了两者在城市交通监控、工业质检等场景下的表现,提供了详细的选型指南和部署优化建议,帮助开发者根据实时性、精度等需求选择最适合的解决方案。

weixin_29062255的博客 372

前缀和(一维、二维)

前缀和算法

weixin_56265979的博客 447

PDF.js实现分片加载问题

PDF.js实现分片加载问题。

qq_32822693的博客 4984

ResNet改进(45):结合通道混洗(ShuffleNet)的混合架构

本文提出了一种创新的卷积神经网络(CNN)架构CustomResNet,它将ResNet34与通道混洗(ChannelShuffle)技术相结合。该模型通过ShuffleBlock模块替换原始ResNet层,包含三个核心组件:1) ChannelShuffle模块实现通道分组混洗;2) ShuffleBlock模块整合点卷积、分组卷积和残差连接;3) CustomResNet主模型保持ResNet整体架构但替换核心层为ShuffleBlock。这种设计既保留了ResNet的残差学习能力,又通过通道混洗增强特

2401_82355416的博客 215

【C++算法模板】预处理算法:一维前缀和、二维前缀和总结,详解带例题

因为前缀和这个板子的推导比较简单,因此本博客重点在于知识点归纳而不在于证明

qq_63586399的博客 1813

基于YOLOv8n的FPGA无PS端PL部署完整实现方案

本文提出了一种将YOLOv8n目标检测模型完整部署到FPGA纯PL端的实现方案。通过PyTorch训练和INT8量化技术,结合Vitis AI工具链,将模型转换为适合FPGA实现的硬件描述。方案详细阐述了从模型训练、量化校准到FPGA硬件设计的全流程,重点解决了无PS端部署的架构设计、资源优化和实时性保障等关键技术挑战。实验结果表明,该方案在Xilinx Virtex-690T FPGA上实现了30FPS以上的实时目标检测性能,为边缘计算场景提供了高效的硬件加速解决方案。

huanghm88的专栏 516

【算法学习之路】1.前缀和与差分

前缀和可以简单的理解为数列的前n项和,是一种重要的预处理方式。(建议:在写前缀和与差分时数组的下标从1开始,后面未提到下标从0开始则是从1开始。一维前缀和(sum数组)的第i个元素就是从数组(a数组)的第一个元素开始加到第i个元素。一维前缀和比较简单,比赛一般和其他算法结合考,是用来优化代码的,求[l,r]区间的和其公式不注意很容易犯错,其他的没什么了。二维前缀和也比较简单,遇到与矩阵相关的题目很有可能就要要用到二维前缀和。原数组就是sum数组的差分数组。

2401_86663413的博客 2286

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

【算法】前缀和

💬 hello!各位铁子们大家好哇。今日更新了前缀和的相关内容。

qinjh_的博客 3252

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

【算法基础】前缀和与差分

前缀和指一个数组的某下标之前的所有数组元素的和(包含其自身)。前缀和分为一维前缀和,以及二维前缀和。前缀和是一种重要的预处理,能够降低算法的时间复杂度。可以快速地求出某一段的和。类似于数学中的求导和积分,差分可以看成前缀和的逆运算。

编程小菜kun 3167

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

一篇带你速通前缀和算法(C/C++)

前缀和是一种常见的算法计算技巧,通常用于处理数组或序列的连续子区间求和问题。它可以帮助我们在 O(1) 的时间内计算出指定子区间的和,而不需要每次都遍历整个子区间。前缀和一般用于预处理当中,具有高效率的特点。

闻道有先后,术业有专攻 4121

基础算法——前缀和详解

秋名山码民的主页 🎉欢迎关注🔎点赞👍收藏⭐️留言📝 🙏作者水平很有限,如果发现错误,一定要及时告知作者 前言 由于有些读者朋友私聊我,希望出几期基础算法的讲解,kmp,dp,哈希,搜索,贪心等对初学者还是不太友好,所以我打算更新几期基础算法合集,没办法谁让我宠粉丝呢?彦祖,热巴说你呢,快关注! 目录大致如下: 排序(十大排序)——已经讲过 高精度算法 从0->1入门双指针 前缀和 二分 位运算 区间合并 何为前缀和算法? 前缀和算法,属于基础算法,一般来说没有固定的模板,但是其思路值得借鉴,我.

秋名山码民的技术小屋 7967

前缀和详解

对于一个数组a来说,假定它的是s,那么s[i]表示。

nzy20110604的博客 1274

【C++算法图解专栏】一篇文章带你掌握前缀和算法(一维+二维)

在有些题目中,需要我们快速的获得一个区间值的和,如果每次查询都循环一个个加的话,时间复杂度会比较大,这时候就要用到前缀和算法,查询区间和的时候,时间复杂度只有 O(1),废话少说,直接上图解。

Newin2020的博客 2095

【专题四】前缀和(1):前缀和重要思想及方法

专题四:前缀和:前缀和数组的建立重要思想及方法 题目包括牛客网:DP34 【模板】前缀和,DP35 【模板】二维前缀和

tan_run的博客 904

ACM_前缀和算法

ACM算法 ——前缀和算法。

小安的博客 908

蓝桥杯一维前缀和 | 算法基础

亲爱的友友们,大家元旦快乐吖🧨🧨🧨🧨祝各位友友们在2024年开开心心,顺顺利利,红红火火🎉🎉🎉🎉我们今天还是来讲解一个简单的算法吧,这个算法在比赛(蓝桥杯,ICPC,ACM)中用到的概率挺高的,经常用来优化时间复杂度,这个算法的名字叫做前缀和算法

小知识吖 8947

【算法】基础算法004之前缀和

本篇文章为大家带来前缀和算法,前缀和算法可以以O(1)的时间复杂度快速求出某一段连续区间的和,这个连续区域既可以是一维的也可以是二维的。

℉f的博客 2106

【题单】1.前缀和与差分

有时间我就会更新这个题单,简单题直接给答案,是为了让第一次写这种题的人一次独立的思考,较难题会写出难点,思考和解决方案,易错题会写出易错点。如果大家发现错误,或有好题目希望大家和我交流交流。

2401_86663413的博客 918

【蓝桥杯每日一题】前缀和算法

本文简要介绍了前缀和的简要概念和几道前缀和的经典例题,希望大家读后能有所收获!

qq_62662919的博客 1068
上一篇: 洛谷P1080 [NOIP2012 提高组] 国王游戏(贪心、高精度经典练习题)
下一篇: P1028 [NOIP2001 普及组] 数的计算题解
Cosmos_zzx
博客等级 码龄6年 90粉丝 36原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值