Time Limit: 2 second

Memory Limit: 256 MB

【问题描述】

给你一个长度为N 的数组,一个长为K的滑动的窗体从最左移至最右端,你只能见到窗口的K个数,每次窗体向右移动一位,如下表:

Window position Min value Max 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

你的任务是找出窗口在各位置时的Max value和Min value。

【输入格式】

第一行N,K,第二行为长度为N的数组

【输出格式】

第一行每个位置的Min value,第二行每个位置的Max value

【输入样例1】

8 3

1 3 -1 -3 5 3 6 7

【输出样例1】

-1 -3 -3 -3 3 3

3 3 5 5 6 7

数据规模

20%:N≤500;50%:N≤100000;100%:N≤1000000;

【题目链接】:http://noi.qz5z.com/viewtask.asp?id=t019

【题解】



最小值维护一个单调递增队列就能搞定;(a[i]<=a[i+1]

每次加入队列中的元素先和队尾比较一下,对于队尾比它大的数,直接删掉就好,因为要求的是最小值,而你加入的这个数是在框框里面的,那么如果你比这个数还大,那么你肯定没有存在的价值了;

然后对于队头元素,如果它不在框框里面也全都删掉,直到队头元素在框框里为止,然后输出队头元素;

最大值类似.

(可以在2s内出)



【完整代码】

#include <cstdio>
#include <algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 const int MAXN = 1e6+100; int n,k,a[MAXN];
int m[MAXN],mm[MAXN],h,t; int main()
{
//freopen("F:\\rush.txt","r",stdin);
scanf("%d%d",&n,&k);
for (int i = 1;i <= n;i++)
scanf("%d",&a[i]);
h = 1,t = 0;
for (int i = 1;i <= k-1;i++)
{
while (t>=h && m[t]>=a[i]) t--;
t++;
m[t] = a[i];mm[t] = i;
}
for (int i = k;i<= n;i++)
{
while (t>=h && m[t]>=a[i]) t--;
t++;
m[t] = a[i];mm[t] = i;
while (mm[h]<i-k+1 && h <= t) h++;
printf("%d",m[h]);
if (i==n)
puts("");
else
putchar(' ');
}
h = 1,t = 0;
for (int i = 1;i <= k-1;i++)
{
while (t>=h && m[t]<=a[i]) t--;
t++;
m[t] = a[i];mm[t] = i;
}
for (int i = k;i <= n;i++)
{
while (t>=h && m[t]<=a[i]) t--;
t++;
m[t] = a[i];mm[t] = i;
while (mm[h]<i-k+1 && h <= t) h++;
printf("%d",m[h]);
if (i==n)
puts("");
else
putchar(' ');
}
return 0;
}

【t019】window(单调队列)的更多相关文章

  1. POJ 2823 Sliding Window + 单调队列

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

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

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

  3. POJ2823 Sliding Window(单调队列)

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

  4. POJ 2823:Sliding Window 单调队列

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

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

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

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

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

  7. poj2823Sliding Window——单调队列

    题目:http://poj.org/problem?id=2823 单调队列模板. 代码如下: #include<iostream> #include<cstdio> usin ...

  8. POJ2823 Sliding Window (单调队列)

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

  9. POJ 2823 Sliding Window(单调队列入门题)

      Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 67218   Accepted: 190 ...

随机推荐

  1. [TS] Swap two element in the array (mutation)

    Shuffling is a common process used with randomizing the order for a deck of cards. The key property ...

  2. Dell shareplex 与HVR数据复制软件

    今天大体了解了一下Dell shareplex 数据复制软件,网址为:http://software.dell.com/products/shareplex/ 从该网址能够看到. shareplex作 ...

  3. linux中软件安装方式

    通常Linux应用软件的安装包有三种: tar包,如software-1.2.3-1.tar.gz.它是使用UNIX系统的打包工具tar打包的. rpm包,如software-1.2.3-1.i386 ...

  4. MySql基本的语法(学习笔记)

    MySQL语法大全_自己整理的学习笔记 select * from emp;  #凝视 #--------------------------- #----命令行连接MySql--------- #启 ...

  5. flink DataStream API使用及原理

    传统的大数据处理方式一般是批处理式的,也就是说,今天所收集的数据,我们明天再把今天收集到的数据算出来,以供大家使用,但是在很多情况下,数据的时效性对于业务的成败是非常关键的. Spark 和 Flin ...

  6. 宝塔 ftp 不能连接 列出时出错

    摘抄自 https://www.bt.cn/bbs/forum.php?mod=viewthread&tid=1903&page=1&extra=#pid6515 1.注意内网 ...

  7. HDU 1212 Big Number 大数模小数

    http://acm.hdu.edu.cn/showproblem.php?pid=1212 题目大意: 给你一个长度不超过1000的大数A,还有一个不超过100000的B,让你快速求A % B. 什 ...

  8. 转 OC温故:类的三大特性(封装,继承,多态)

    原文标题:OC学习篇之---类的三大特性(封装,继承,多态) 我们都知道,面向对象程序设计中的类有三大特性:继承,封装,多态,这个也是介绍类的时候,必须提到的话题,那么今天就来看一下OC中类的三大特性 ...

  9. MyEclipse中 使用svn更新jar包 出现 svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted 导致整个svn异常处理

    svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted 2014-07-02 ...

  10. [Angular] Change component default template (ng-content, ng-template, ngTemplateOutlet, TemplateRef)

    Here is the defulat tab header template: <ng-template #defaultTabHeader let-tabs="tabsX" ...