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. modSecurity规则学习(三)——SecRule

    通用格式 SecRule VARIABLES OPERATOR [TRANSFORMATION_FUNCTIONS, ACTIONS]   阶段phase (1)request headers (2) ...

  2. Flat UI简介

    Flat UI简介 一.简介 Flat UI是基于Bootstrap之上进行二次开发的扁平化前端框架,他提供了动感.时尚的风格色调搭配,简洁.炫丽的功能组件,同时还提供了更为平滑的js交互动画,可以称 ...

  3. 搜寻Linux软件实用指南

    搜寻Linux软件实用指南  对于初学者来说,仅仅安装好Linux系统还是不够的,还需要安装大量的应用软件.许多下载网站都提供了诸如装机必备软件的下载,分门别类提供经典的工具软件下载.本文主要针对初学 ...

  4. layui动态无限极菜单

    ajax加jQuery实现 效果图 参考文章:https://www.wanpishe.top/detail?blogId=644aa177-9795-456a-8090-ee1264bf5d9d

  5. coverage python 代码覆盖率工具使用(django 使用)

    1. 安装包 pip install coverage 2.启动程序 coverage run -m pytest 3.获取html格式的报告文件 coverage html 4.创建配置文件 .co ...

  6. [ReasonML] Named & optional params

    // ::country is named param // ::country=?: which make it optional // because we make ::country=? op ...

  7. Irrlicht 3D Engine 笔记系列 之 教程5- User Interface

    作者:i_dovelemon 日期:2014 / 12 / 18 来源:CSDN 主题:GUI 引言 今天.博主学习了第五个教程. 这个教程解说了怎样使用Irrlicht内置的一个基础模块.GUI模块 ...

  8. android 自己定义View之SubmitView

    转载请注明出处:王亟亟的大牛之路 近期看了一大堆的自己定义View多数都能够充当耗时操作的交互界面.再接再厉再传一个SubmitView.一个和可用于模仿提交等待与用户交互用的一个自己定义View 效 ...

  9. The program yum-complete-transaction is found in the yum-utils package

    用yum安装的时候出现 The program yum-complete-transaction is found in the yum-utils package. 错误提示的解决方法:# 安装 y ...

  10. buildroot使用

    buildroot是一套自动化构建工具,比yocto简单. 项目地址:http://www.buildroot.org/ 培训资料地址:http://free-electrons.com/ 附带lin ...