Sliding Window POJ - 2823 单调队列模板题 题意 给出一个数列 并且给出一个数m 问每个连续的m中的最小\最大值是多少,并输出 思路 使用单调队列来写,拿最小值来举例 要求区间最小值 就是维护一个单调递增的序列 对于样例 8 3 1 3 -1 -3 5 3 6 7 我们先模拟一遍 1.队列为空 1 进队 队列:1 2.3>队尾元素 3 进队 队列: 1 3 3.-1小于队尾元素,一直从尾部出队知道找到比-1小的元素或者队列为空 队列:-1 当队列中元素大于m的时候从队头删…
#include<stdio.h>//每次要吧生命值长的加入,吧生命用光的舍弃 #define N  1100000 int getmin[N],getmax[N],num[N],n,k,a[N]; int main(){ int i,first,last; while(scanf("%d%d",&n,&k)!=EOF) { for(i=1;i<=n;i++) scanf("%d",&a[i]); first=1;last=…
给定一个n个数的数列,从左至右输出每个长度为m的数列段内的最大数. 输入:第一行两个整数n和m( 1<= n <= 20 0000,m<=n).下来给出n个整数. 输出:一行一个整数,表示每连续m个数的最大值.样例输入: 8 3 1 3 -1 -3 5 3 6 7 样例输出: 3 3 5 5 6 7 维护一个单调队列,列内严格递减.若是首端元素已经脱离当前M个元素的区间,则去除.若是新元素比队尾元素严格小,直接入队,否则元素出队至合格后,新元素入队.比较模板了: #include <…
题目链接:https://www.luogu.org/problem/P1886#submit 题意:给定n个数,求大小为k的滑动窗口中最小值和最大值. 思路:单调队列模板题. AC代码: #include<cstdio> #include<algorithm> using namespace std; ; int n,k,head,tail; int a[maxn],q[maxn],p[maxn]; int main(){ scanf("%d%d",&…
题目链接:https://cn.vjudge.net/contest/276251#problem/G 题目大意:给你n和m,然后问你对于(m,n)这中间的每一个数,(i-m+1,i)这个区间的最小值和最大值. 具体思路:单调队列,对于个数的控制,我们通过队列来实现一个模拟的滑动窗口.然后最值的寻找,我们可以通过控制队列保持单调递增或者单调递减来实现. STL AC代码(耗时:10985s): #include<iostream> #include<stack> #include&…
最最基础的单调队列题目.一个单增一个单减.还是可以借此好好理解一下单调队列的. #include <stdio.h> #include <string.h> #include <iostream> using namespace std; #define maxx 1000005 int num[maxx], inque[maxx], dque[maxx], maxn[maxx], minn[maxx]; int pre1, pre2, lst1, lst2; int…
题面: 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 window. Each time the sliding window moves rightwards by one po…
2823 思路: 单调队列: 以前遇到都是用线段树水过: 现在为了优化dp不得不学习单调队列了: 代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn 1000005 struct QueueType { int x,dis; }; struct QueueType mique[ma…
思路:裸的单调队列. #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #define Maxn 1000010 using namespace std; int n,k,que[Maxn],num[Maxn],head,rear; int main() { int i,j,a; while(scanf("%d%d",&n,&…
题意 刚学单调队列的时候做过 现在重新做一次 一个很经典的题目 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗口中的最大值和最小值. 思路 单调队列 一个递增 一个递减 代码 //author: sysky #include<cstdio> #define N 1000006 #define INF 0X3FFFFFFF using namespace std; int n,k; int mina[N],…