Sliding Window
Time Limit: 12000MS   Memory Limit: 65536K
Total Submissions: 46507   Accepted: 13442
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

POJ Monthly--2006.04.28, Ikki

固定区间长度一维的数组就好,二维超内存

ac代码

#include<stdio.h>
#include<string.h>
#include<math.h>
#define max(a,b) (a>b?a:b)
#define min(a,b) (a>b?b:a)
int maxv[1000010],minv[1000010],a[1000010],n,m,k;
void init()
{
int i,j;
for(i=1;i<=n;i++)
{
minv[i]=a[i];
maxv[i]=a[i];
}
for(i=1;i<=k;i++)
{
for(j=1;j+(1<<i)-1<=n;j++)
{
minv[j]=min(minv[j],minv[j+(1<<(i-1))]);
maxv[j]=max(maxv[j],maxv[j+(1<<(i-1))]);
}
}
}
int q_max(int l,int r)
{
// int k=(int)(log((double)(r-l+1))/log(2.0));
return max(maxv[l],maxv[r-(1<<k)+1]);
}
int q_min(int l,int r)
{
// int k=(int)(log((double)(r-l+1))/log(2.0));
return min(minv[l],minv[r-(1<<k)+1]);
}
int main()
{
// int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
int i;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
k=(int)(log(double(m))/log(2.0));
init();
if(m<=n)
{
printf("%d",q_min(1,m+1-1));
}
for(i=2;i+m-1<=n;i++)
{
printf(" %d",q_min(i,m+i-1));
}
printf("\n");
if(m<=n)
{
printf("%d",q_max(1,m+1-1));
}
for(i=2;i+m-1<=n;i++)
{
printf(" %d",q_max(i,m+i-1));
}
printf("\n");
}
}

POJ 题目2823 Sliding Window(RMQ,固定区间长度)的更多相关文章

  1. 洛谷P1886 滑动窗口(POJ.2823 Sliding Window)(区间最值)

    To 洛谷.1886 滑动窗口 To POJ.2823 Sliding Window 题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每 ...

  2. POJ 2823 Sliding Window 线段树区间求和问题

    题目链接 线段树区间求和问题,维护一个最大值一个最小值即可,线段树要用C++交才能过. 注意这道题不是求三个数的最大值最小值,是求k个的. 本题数据量较大,不能用N建树,用n建树. 还有一种做法是单调 ...

  3. 【POJ】2823 Sliding Window

    单调队列. /* 2823 */ #include <iostream> #include <sstream> #include <string> #include ...

  4. POJ 2823 Sliding Window + 单调队列

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

  5. POJ 2823 Sliding Window 题解

    POJ 2823 Sliding  Window 题解 Description An array of size n ≤ 106 is given to you. There is a sliding ...

  6. 题解报告:poj 2823 Sliding Window(单调队列)

    Description An array of size n ≤ 106 is given to you. There is a sliding window of size k which is m ...

  7. POJ 2823 Sliding Window ST RMQ

    Description An array of size n ≤ 106 is given to you. There is a sliding window of size k which is m ...

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

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

  9. POJ - 2823 Sliding Window (滑动窗口入门)

    An array of size n ≤ 10 6 is given to you. There is a sliding window of size kwhich is moving from t ...

随机推荐

  1. Use PSO to find minimum in OpenCASCADE

    Use PSO to find minimum in OpenCASCADE eryar@163.com Abstract. Starting from OCCT6.8.0 will include ...

  2. mysql异常Lock wait timeout exceeded; try restarting transaction

    mysql中使用update语句更新数据报错: Lock wait timeout exceeded; try restarting transaction. 这是由于你要更新的表的锁在其它线程手里. ...

  3. SQL事务+异常

    BEGIN TRAN BEGIN TRY --这里写代码 --这里写代码 END TRY BEGIN CATCH ROLLBACK TRAN END CATCH COMMIT TRAN

  4. windows下gopath设置

    下载了go语言的安装包, 然后安装, 装完了需要设置三个地方: 1. 在windows的PATH变量中添加go的可执行文件所在的目录: PATH=C:\Go\bin;其他设置; 2. 设置 GOROO ...

  5. AIX设备四种状态

    AIX设备四种状态: Undefined(未定义):表示系统无法识别该设备,也就是系统无法找到该设备.(驱动无法识别) Defined(定义):表示系统可以识别设备,信息保存在ODM,但是系统不能使用 ...

  6. 小程序block总结

    小程序block总结 1.block并不是一个组件,它仅仅是一个包装元素,不会在页面中做任何渲染,只接受控制属性. 2.因为 wx:if 是一个控制属性,需要将它添加到一个标签上.如果要一次性判断多个 ...

  7. 【Codeforces Round #426 (Div. 2) C】The Meaningless Game

    [Link]:http://codeforces.com/contest/834/problem/C [Description] 有一个两人游戏游戏; 游戏包括多轮,每一轮都有一个数字k,赢的人把自己 ...

  8. 关于multiprocessing,我也来聊几句

    起因:近期须要从hbase中向 ES中导一批数据.使用multiprocessing 启动多个程序同一时候向ES导数据.能够大大提高效率.由于导数的任务是能够依照时间切割的. 一段简单的代码例如以下: ...

  9. Nio学习3——基础模型:多路复用模型

    Reactor模式和NIO 本文可看成是对Doug Lea Scalable IO in Java一文的翻译. 当前分布式计算 Web Services盛行天下,这些网络服务的底层都离不开对socke ...

  10. Qt程序调试之Q_ASSERT断言(它是一个宏,接受布尔值,当其中的布尔值为真时,便什么也不做)

    在使用Qt开发大型软件时,难免要调试程序,以确保程序内的运算结果符合我们的预期.在不符合预期结果时,就直接将程序断下,以便我们修改. 这就用到了Qt中的调试断言 - Q_ASSERT. 用一个小例子来 ...