poj 2823 Sliding Window

基于STM32与SimpleFOC实现直流无刷电机开环控制的实战指南 本文提供了一份基于STM32微控制器与SimpleFOC开源库实现直流无刷电机开环控制的详细实战指南。内容涵盖硬件连接、PlatformIO环境搭建、核心代码解析及上电测试,旨在帮助开发者快速验证硬件并理解开环控制的基本原理,为后续实现高性能的闭环FOC控制奠定基础。 阅读详情

一 原题

Sliding Window
Time Limit: 12000MS Memory Limit: 65536K
Total Submissions: 68479 Accepted: 19426
Case Time Limit: 5000MS

Description

An array of size  n ≤ 10 6 is given to you. There is a sliding window of size  k which is moving from the very left of the array to the very right. You can only see the  k numbers in the window. Each time the sliding window moves rightwards by one position. Following is an example: 
The array is  [1 3 -1 -3 5 3 6 7], and  k is 3.
Window positionMinimum valueMaximum value
[1  3  -1] -3  5  3  6  7 -13
 1 [3  -1  -3] 5  3  6  7 -33
 1  3 [-1  -3  5] 3  6  7 -35
 1  3  -1 [-3  5  3] 6  7 -35
 1  3  -1  -3 [5  3  6] 7 36
 1  3  -1  -3  5 [3  6  7]37

Your task is to determine the maximum and minimum values in the sliding window at each position. 

Input

The input consists of two lines. The first line contains two integers  n and  k which are the lengths of the array and the sliding window. There are  n integers in the second line. 

Output

There are two lines in the output. The first line gives the minimum values in the window at each position, from left to right, respectively. The second line gives the maximum values. 

Sample Input

8 3
1 3 -1 -3 5 3 6 7

Sample Output

-1 -3 -3 -3 3 3
3 3 5 5 6 7

Source


二 分析

裸的滑动窗口,优先队列和双端队列都可以


三 代码

优先队列:

/*
ID: maxkibble
LANG: c++
PROB: poj 2823
*/

#include <iostream>
#include <queue>

using namespace std;

const int maxn = 1e6 + 5;
int n, k, a[maxn], mn[maxn], mx[maxn];

struct cmp1 {
	bool operator () (const int &x, const int &y) {
		return a[x] > a[y];
	}
};

struct cmp2 {
	bool operator () (const int &x, const int &y) {
		return a[x] < a[y];	
	}
};

priority_queue<int, vector<int>, cmp1> q1;
priority_queue<int, vector<int>, cmp2> q2;

int main() {
	ios::sync_with_stdio(false);
	cin >> n >> k;
	for (int i = 0; i < n; i++) cin >> a[i];
	for (int i = 0; i < k - 1; i++) {
		q1.push(i); q2.push(i);
	}
	for (int i = k - 1; i < n; i++) {
		q1.push(i); q2.push(i);
		while (i - q1.top() > k - 1) q1.pop();
		while (i - q2.top() > k - 1) q2.pop();
		mn[i - k + 1] = a[q1.top()]; mx[i - k + 1] = a[q2.top()];
	}
	for (int i = 0; i < n - k + 1; i++) cout << mn[i] << " \n"[i == n - k];
	for (int i = 0; i < n - k + 1; i++) cout << mx[i] << " \n"[i == n - k];
	return 0;
}

双端队列:

/*
ID: maxkibble
LANG: c++
PROB: poj 2823
*/

#include <iostream>
#include <queue>

using namespace std;

#define mp make_pair
#define fi first
#define se second

typedef pair<int, int> PII;
const int maxn = 1e6 + 5;
int n, k, a[maxn], mn[maxn], mx[maxn];

deque<PII> q1, q2;

int main() {
	ios::sync_with_stdio(false);
	cin >> n >> k;
	for (int i = 0; i < n; i++) cin >> a[i];
	for (int i = 0, j = 0; i < n - k + 1; i++) {
		while (j < i + k) {
			while (!q1.empty() && q1.back().fi > a[j])
				q1.pop_back();
			while (!q2.empty() && q2.back().fi < a[j])
				q2.pop_back();
			q1.push_back(mp(a[j], j));
			q2.push_back(mp(a[j], j));
			j++;
		}
		while (q1.front().se < i) q1.pop_front();
		while (q2.front().se < i) q2.pop_front();
		mn[i] = q1.front().fi; mx[i] = q2.front().fi;
	}
	for (int i = 0; i < n - k + 1; i++) cout << mn[i] << " \n"[i == n - k];
	for (int i = 0; i < n - k + 1; i++) cout << mx[i] << " \n"[i == n - k];
	return 0;
}

scratch经典游戏主题源码分享,包含了20多款经典游戏 经典游戏案例:1、360度角度射击游戏 2、捕鱼达人 3、超级马里奥 4、超级玛丽 5、吃豆人相关 6、穿着喷气背包的海龟 7、大鱼吃小鱼 8、俄罗斯方块 9、飞机大战 10、愤怒的小鸟 11、黄金矿工 12、节奏大师 13、连连看 14、迷宫游戏 15、跑酷游戏 16、扫雷 17、水果忍者 18、坦克大战 19、推箱子 20、消除泡泡 21、保卫萝卜 22、植物大战僵尸 阅读详情

相关推荐

408王道计算机组成原理强化——指令系统及大题解构

1.大纲要求 2.x86

JiangNan_1002的博客 8512

POJ 2823 Sliding Window

继续复习线段树。找女朋友浪费时间啊。课程全部落下拉。 这是标准的模版题。输出区间最小与区间最大。 AC代码 #include #include #include #include using namespace std; const int MaxF=10000000; const int MinF=-10000000; int n,ql,qr; int a[5000001]; int mi

自己选择的路,跪着也要走完。 619

嵌入式C开发必备:用Cursor规则搞定代码风格检查(附Pikachu项目实战)

本文以Pikachu开源项目为例,详细介绍了如何利用Cursor的AI代理规则系统,为嵌入式C开发项目配置一套完整的代码风格检查与规范执行方案。通过实战演示,指导开发者创建项目级规则,实现命名、格式、硬件抽象层及驱动开发的自动化规范,从而提升代码一致性、可维护性和团队协作效率。

weixin_29181861的博客 598

poj2823 Sliding Window(单调队列)

Sliding Window Description An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k num...

闲人请进 206

win10和Mac上安装Freesurfer

Mac安装Freesurfer 软件下载地址 可以根据官方给出的demo视频一步一步下载

Daily_365的博客 6077

torchsparse安装

torchsparse安装以及安装失败原因分析和解决

AI_BING_的博客 1万+

poj 2823 Sliding Window 单调队列

问题描述: DescriptionAn array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the

ww的博客 495

在WSL2中配置代理(Windows)

通过按照上述步骤在WSL2中配置代理,我们可以轻松地在Windows子系统Linux中使用代理服务器访问互联网。请确保提供正确的代理服务器地址和端口号,并根据实际情况进行修改。在WSL2中使用代理是一个常见的需求,它允许我们在Windows子系统Linux中通过代理服务器访问互联网。本文将详细介绍如何在WSL2中配置代理,并提供相应的源代码示例。如果返回的IP地址与代理服务器所在地不同,说明代理正常工作。希望本文能对你有所帮助!如有其他问题,请随时向我提问。替换为实际的代理服务器地址和端口号。

FcUnity的博客 4355

Sliding Window POJ - 2823(单调队列)

题意 给一个序列,求连续k个序列的最大最小值是什么,类似滑动窗口 思路 用单调队列维护最大值与最小值就可以,惊险卡过????4895?? #include <iostream> #include <cstdio> using namespace std; const int MAXN=1000000+10; int maxdq[MAXN],mindq[MAXN]...

leekerian的博客 174

Python 爬虫教程:抓取交通流量和道路信息

Scrapy是一个强大的爬虫框架,适用于大规模的爬虫任务。它提供了高效的数据抓取和存储功能,支持数据清洗和解析操作。Scrapy特别适合用于抓取开放的API接口和数据流。

2201_76125261的博客 2234

【安全启动】HSM安全启动原理与技术实现白皮书

摘要: 安全启动(Secure Boot)与度量启动(Measured Boot)通过硬件信任根(HSM/TPM等)确保系统启动链路的可信性,实现代码验证与状态记录。HSM作为核心组件,承担密钥保护、签名验证和密码运算任务,与SHE、TPM等协同构建分层信任链。通用计算与嵌入式/汽车场景的启动流程差异显著,前者依赖TPM的PCR度量,后者以HSM为核心实现硬件级安全闭环。工程实践中需权衡性能、合规性及抗攻击能力,并结合后量子密码(PQC)等演进技术优化方案。

weixin_46481662的博客 833

[单调队列] poj 2823 Sliding Window

题意: 给你n个数和一个k 把n分成连续的n-k+1个区间 第一行按顺序输出每个区间的最小值,第二行是最大值。 思路: 单调队列的模板题,这里注意的是插入队尾的时候需要二分加速 代码: #include"stdio.h" #include"algorithm" #include"string.h" #include"iostream" #include"queue" #include

Enmity_Dark 709

ROCm rocr-libhsakmt分析系列9-3: queue的内核创建流程

KFD队列是用户空间与GPU通信的核心机制,通过多种队列类型(计算、DMA、调试等)实现命令流管理。核心数据结构包括描述队列属性的queue_properties、内核调度对象queue、进程级管理器process_queue_manager,以及跨进程设备的qcm_process_device等。这些结构通过ring buffer、doorbell机制和MQD硬件描述符协同工作,支持队列全生命周期管理、资源调度、异常处理和上下文保存/恢复功能。

图形/计算GPU的驱动与虚拟化技术分享园地 2128

POJ-2823-Sliding Window

POJ-2823-Sliding Window http://poj.org/problem?id=2823 求区间的最大值与最小值,线段树即可,C++过了,G++超时,囧 #include #include #include #include using namespace std; #define N 1000005 struct cam { int x; int y; int

菜鸟的程序人生 1076

深度学习工具包 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")

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

POJ2823 Sliding Window

POJ2823 筱柒Littleseven QQ:3534947775 题目链接 #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #define maxn 1000100 using namespace std; int n,k,head,...

筱柒LittleSeven 127

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

POJ - 2823 Sliding Window

https://vjudge.net/problem/POJ-2823 POJ - 2823 Sliding Window题目AC代码 题目 An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the wi

斯文~ 358

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

POJ2823 Sliding Window

单调队列 滑动窗口

a free man 582

poj 2823 Sliding Window 题解

Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 53037   Accepted: 15207 Case Time Limit: 5000MS Description An array of size n ≤ 106 is gi

SBS的博客 247

POJ2823 Sliding Window 单调队列

题目链接 http://poj.org/problem?id=2823 分析 单调队列模板,在从队尾出队时,可以用二分快速找到最终位置,但效果并不明显。 AC代码 #include <cstdio> inline int read() { int num = 0, flag = 1; char c = getchar(); while (c < '0' || c >...

Kevin的博客 255

poj2823 Sliding Window

Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 52857   Accepted: 15140 Case Time Limit: 5000MS Description An array of

ACoder 458
上一篇: cf 1004E Sonya and Ice Cream
下一篇: poj 1183 食物链
max_kibble
博客等级 码龄11年 2粉丝 110原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值