单调队列,我用deque维护。这道题不难写,我第二次写单调队列,1次AC。

-----------------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<deque>
#define rep(i,r) for(int i=0;i<r;i++)
#define clr(x,c) memset(x,c,sizeof(x))
#define Rep(i,l,r) for(int i=l;i<r;i++)
using namespace std;
const int maxn=1000000+5;
int n,k;
int Min[maxn],Max[maxn];
deque<int> minq,maxq;
deque<int> minnum,maxnum;
int main()
{
// freopen("test.in","r",stdin);
// freopen("test.out","w",stdout);
while(scanf("%d%d",&n,&k)==2) {
int t;
rep(i,n) {
scanf("%d",&t);
if(minq.empty()) {
minq.push_back(t); minnum.push_back(i);
} else {
while(!minq.empty() && minnum.front()<=i-k) {
   minq.pop_front();
   minnum.pop_front();
   }
   while(!minq.empty() && minq.back()>=t) {
       minq.pop_back();
       minnum.pop_back();
   }
   minq.push_back(t); minnum.push_back(i);
}
if(maxq.empty()) {
maxq.push_back(t); maxnum.push_back(i);
} else {
while(!maxq.empty() && maxnum.front()<=i-k) {
   maxq.pop_front();
   maxnum.pop_front();
   }
   while(!maxq.empty() && maxq.back()<=t) {
       maxq.pop_back();
       maxnum.pop_back();
   }
   maxq.push_back(t); maxnum.push_back(i);
}
Min[i]=minq.front(); Max[i]=maxq.front();    
   }
   Rep(i,k-1,n) printf("%d ",Min[i]);
printf("\n");
Rep(i,k-1,n) printf("%d ",Max[i]);
printf("\n");
}
return 0;
}

-----------------------------------------------------------------------------------

Sliding Window
Time Limit: 12000MS Memory Limit: 65536K
Total Submissions: 41760 Accepted: 12360
Case Time Limit: 5000MS

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 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

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

Sample Output

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

Source

POJ2823 Sliding Window(单调队列)的更多相关文章

  1. POJ 2823 Sliding Window + 单调队列

    一.概念介绍 1. 双端队列 双端队列是一种线性表,是一种特殊的队列,遵守先进先出的原则.双端队列支持以下4种操作: (1)   从队首删除 (2)   从队尾删除 (3)   从队尾插入 (4)   ...

  2. poj 2823 Sliding Window (单调队列入门)

    /***************************************************************** 题目: Sliding Window(poj 2823) 链接: ...

  3. POJ 2823:Sliding Window 单调队列

    Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 48930   Accepted: 14130 ...

  4. POJ 2823 Sliding Window (单调队列)

    单调队列 加了读入挂比不加更慢.... 而且这份代码要交c++ 有大神G++跑了700ms..... orzorzorz #include<iostream> #include<cs ...

  5. POJ 2823 UESTCoj 1221 Sliding Window 单调队列 经典入门题

    题意:给出一个序列,求出每连续k个数字中最大的数和最小的数. 这是道单调队列裸题,直接写就行了. 本来用deque写出来后,发现在poj上硬是超时了,在discuss上看很多人也在抱怨超时的问题,据说 ...

  6. POJ2823 Sliding Window (单调队列)

    POJ2823 Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 38342   Accepte ...

  7. POJ2823 Sliding Window(单调队列)

    题目要输出一个序列各个长度k的连续子序列的最大值最小值. 多次RMQ的算法也是能过的,不过单调队列O(n). 这题,队列存元素值以及元素下标,队尾出队维护单调性然后入队,队首出队保持新元素下标与队首元 ...

  8. [POJ2823]Sliding Window 滑动窗口(单调队列)

    题意 刚学单调队列的时候做过 现在重新做一次 一个很经典的题目 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗 ...

  9. [POJ2823] Sliding Window 「单调队列」

    我们从最简单的问题开始: 给定一个长度为N的整数数列a(i),i=0,1,...,N-1和窗长度k. 要求:   f(i) = max{ a(i-k+1),a(i-k+2),..., a(i) },i ...

随机推荐

  1. PHPExcel 多工作表 导出

    //浏览器输出excel header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet ...

  2. 截取字符串一之substr

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. linux 获取文件系统信息(磁盘信息)

    源代码例如以下: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <s ...

  4. C++数据类型简析

    C++语言的基本数据类型有如下四种: 整型,说明符为int: 字符型,说明符为char: 浮点型(又称实型),说明符为float(单精度),double(双精度): 空值型,说明符为void,用于函数 ...

  5. ExtJs中的Grid具体操作(笔记及心得)

    一.基本操作步骤 var cm=new Ext.grid.ColumnModel([ //对列的定义,cm是它的简写,作为真个表格的列模式,需要首先创建的{header:'编号',dataIndex: ...

  6. R包——jiebaR分词器

    关于R的分词器jiebaR 关于R的分词器jiebaR "结巴"中文分词的R语言版本,支持最大概率法(Maximum Probability),隐式马尔科夫模型(Hidden Ma ...

  7. 【转】Eclipse自动补全的设置方法

    转自:http://blog.csdn.net/xiadasong007/archive/2009/11/11/4799715.aspx 打开 Eclipse -> Window -> P ...

  8. 【LeetCode题意分析&解答】42. Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  9. 用Cython加速Python程序以及包装C程序简单测试

    用Cython加速Python程序 我没有拼错,就是Cython,C+Python=Cython! 我们来看看Cython的威力,先运行下边的程序: import time def fib(n): i ...

  10. JS表格排序

    var employees = [] employees[0] = { name: "George", age: 32, retiredate: "March 12, 2 ...