单调队列,我用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

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

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. 开源 免费 java CMS - FreeCMS1.9 简历管理

    项目地址:http://code.google.com/p/freecms/ 简历管理 管理当前管理网站的简历数据. 1. 回复简历 选择须要回复的简历.然后点击"回复". 注意: ...

  2. linux性能监控三张图

    一.监控 二.测试 三.优化

  3. 从零开始Unity3D游戏开发【4 材质球和渲染纹理】

    [创建材质球] 1.Project 面板下  create-Material 然后将材质球拖放到物体,物体的颜色便会和材质球相同: [渲染纹理 RenderTexture]

  4. C#实现发送和接收pop3邮件方法

    必须引入Interop.jmail.dll应用 /// <summary>    /// 收取新邮件.不删除老邮件.收取邮件后写入数据库    /// </summary>   ...

  5. MongoDB 启动异常

    今天启动MongoDB遇到异常状况 mongodb warning: 32-bit servers don't have journaling enable 解决方法: 删除数据库目录的.lock文件 ...

  6. win7环境下安装MongoDB

    1.从http://www.mongodb.org/downloads获取,下载适合windows版本的mongodb,注意32位和64位的区别2.将下载的zip版本,解压到D:/mongodb3.创 ...

  7. Ubuntu切换至root用户

    第一种方式: 使用命令 sudo passwd root 设置root用户的密码 然后su root即可切换至root用户  第二种方式: sudo bash

  8. BZOJ 2434: [Noi2011]阿狸的打字机( AC自动机 + DFS序 + 树状数组 )

    一个串a在b中出现, 那么a是b的某些前缀的后缀, 所以搞出AC自动机, 按fail反向建树, 然后查询(x, y)就是y的子树中有多少是x的前缀. 离线, 对AC自动机DFS一遍, 用dfs序+树状 ...

  9. python 实现单链表

    #! /usr/bin/env python ### ### Linked List python implementation ### ### @reference Data Structures ...

  10. C++学习之使用new的注意事项

    C++学习之使用new的注意事项       在构造函数中使用new来初始化对象的指针成员成员时必须特别小心,具体的说,应该如下这样做: 一.如果在构造函数中使用new来初始化指针成员,则应该在析构函 ...