首页 > 其他分享 >poj-2823 Sliding Window(单调队列)

poj-2823 Sliding Window(单调队列)

时间:2023-06-12 17:37:19浏览次数:65  
标签:int deq1 window deq2 Window maxn poj include Sliding


原题链接


Sliding Window


Time Limit: 12000MS

 

Memory Limit: 65536K

Total Submissions: 54929

 

Accepted: 15814

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 position

Minimum value

Maximum value

[1  3  -1] -3  5  3  6  7 

-1

3

 1 [3  -1  -3] 5  3  6  7 

-3

3

 1  3 [-1  -3  5] 3  6  7 

-3

5

 1  3  -1 [-3  5  3] 6  7 

-3

5

 1  3  -1  -3 [5  3  6] 7 

3

6

 1  3  -1  -3  5 [3  6  7]

3

7

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



用数组模拟单调递减队列deq1,单调递增队列deq2,deq1中的首元素是最大值,deq2中的首元素是最小值,再取队首元素时判断对首元素是否在k个元素的区间内

如果不是则队首指针+1

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <stack>
#include <deque> 
#define INF 1e9
#define maxn 1000005
using namespace std;
typedef long long ll;

int num[maxn], maxs[maxn], mins[maxn];
int deq1[maxn], deq2[maxn];
int main(){
	
//	freopen("in.txt", "r", stdin);
	int n, k;
	while(scanf("%d%d", &n, &k) == 2){
		int l1 = 0, r1 = 0, l2 = 0, r2 = 0; 
		if(k <= 0)
		 continue;
		for(int i = 0; i < n; i++)
		 scanf("%d", num+i);
		for(int i = 0; i < n; i++){
			while(l1 != r1 && num[deq1[r1-1]] <= num[i])
			 r1--;
			deq1[r1++] = i;
		    while(l2 != r2 && num[deq2[r2-1]] >= num[i])
			 r2--;
			deq2[r2++] = i;
			if(i >= k-1){
				int j = i - k;
				while(deq1[l1] <= j)
				 l1++;
				while(deq2[l2] <= j)
				 l2++;
				maxs[i] = num[deq1[l1]];
				mins[i] = num[deq2[l2]];
			}
		}
		printf("%d", mins[k-1]);
		for(int i = k; i < n; i++)
		 printf(" %d", mins[i]);
		puts("");
		printf("%d", maxs[k-1]);
		for(int i = k; i < n; i++)
		 printf(" %d", maxs[i]);
		puts("");
	}
	return 0;
}




标签:int,deq1,window,deq2,Window,maxn,poj,include,Sliding
From: https://blog.51cto.com/u_16158872/6464260

相关文章

  • Jmeter:"An error occurred: Can't connect to X11 window server using 'lacalhost:12
    做各种不同项目的性能测试,都需要在项目本地压测服务器配置jmeter,需要时还要调出jmeter图形化界面来调试jmeter脚本。标题中的问题遇过多次,这次做个记录。1.启动jmeter报错在配置好jmeter环境变量的linux系统执行jmeter命令,报错如下:[root@localhost~]#jmeter=====......
  • 为Linux配置固定IP(Windows)
    配置固定IP地址目前我们虚拟机的Linux操作系统,其IP地址是通过DHCP服务获取的。DHCP:动态获取IP地址,即每次重启设备后都会获取一次,可能导致IP地址频繁变更。为什么需要配置固定IP地址原因1:办公电脑IP地址变化无所谓,但是要远程连接到Linux系统,如果IP地址经常变化就要频繁修改适......
  • Windows系统提示“ping”不是内部或外部命令
    Windows系统电脑/服务器在运行CMD命令提示符时提示“ping不是内部或外部命令,也不是可运行的程序或批处理文件”,遇到这种情况怎么办呢?今天我和你们分享解决办法。解决方法重新配置系统环境变量(演示的是Windows10系统)1、开始——控制面板——系统2、高级系统设置——高级——环境变......
  • kafka环境搭建(Windows10)
    1.安装Javajdk说明:kafka是使用zookeeper来进行集群部署,zookeerper运行环境依赖Java环境,因此需要安装Javajdk,并设置好系统环境变量。1.1下载jdk1.8华为提供的下载服务:https://repo.huaweicloud.com/java/jdk/官网下载地址:https://www.oracle.com/java/technologies/download......
  • POJ 1459 Power Network(最大流)
    题意:第一眼看到这题目觉得神题啊...其实题目给的s[i]压根不用管的....总共有n个结点,其中有发电站np个、用户nc个和调度器n-np-nc个三种节点以及M条电线(用于传输电流的),每个发电站有一个最大发电量,每个用户有个最大接受电量,现在有m条有向边,边有一个最大的电流量,表示最多可以流出这......
  • POJ1787
    POJ1787一开始还没看多重背包…以为是完全背包加个限制条件,然后想了好久没想不出,看了下背包九讲..看到多重背包可是也没什么思路…后来搜了一下题解…豁然开朗dp[j]表示j块钱最多由多少块硬币组成,path[j]表示上一次最多有多少块构成的j块钱,used[j]表示j块钱时,已经放了......
  • POJ 3264 Balanced Lineup
    思路:线段树求最大值减最小值,每个结点分别维护最大值和最小值即可。#include<cstdio>#include<queue>#include<cstring>#include<iostream>#include<cstdlib>#include<algorithm>#include<vector>#include<map>#include<string>#in......
  • POJ1837 DP
    POJ1837DP题题目一开始看了N久…意思大概是有一个天平,左边臂长是-15到0,右边臂长是0到15,给你c个挂钩,g个砝码,每一个砝码重量都在1到25,问将所有砝码挂到天平上并使之平衡的方案有多少个。要使之平衡由物理知识可知力矩=0,左边重量X左边臂长+右边重量X右边臂长=0,故状态一共有25*15......
  • POJ 2352 HDU1541 Stars(树状数组)
    题意:二维平面给定n个点的坐标,然后要你输出每个点的“等级“。每个点的等级是它的左下放的点个数(包括正下放和正左方的点)。即要你输出对于每个点(x,y)来说,有多少点的坐标(xi,yi)满足xi<=x且yi<=y。思路:题目给出的坐标中已经是按y升序排列,那么其实只用考虑x轴,那么显然就是在前面的......
  • POJ 3498 March of the Penguins(枚举+最大流)
    题意:在X,Y坐标系中有N(N<=100)个冰块...有些冰块上有若干只企鹅..每只企鹅一次最多跳M距离..一个冰块在有Mi个企鹅离开..就会消失..问有哪些冰块可以作为集合点..就是所有企鹅都能成功到这个冰块上来.思路:枚举每一块冰块,看看最大流能否等于企鹅总数即可      建图:把每块......