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

 
题意 :给定长n的数列,问长为k的区间在数列中所有情况的最小值和最大值。
思路:学长教导的RMQ解法,ST版实质是DP,比起不太懂DP的以前,现在感觉好理解多了。此外感觉可以使用线段树解。
注意先打log的表。
 #include <stdio.h>
#include <algorithm>
//#define LOG[i] = (i & (i - 1)) ? LOG[i - 1] : LOG[i - 1] + 1
#define MAXX 1234567
#include <vector>
using namespace std; int a[MAXX];
int dp1[MAXX][];
int LOG[MAXX]; void init(int n)
{
LOG[] = ;
for(int i=; i<=n; i++)
LOG[i]=(i&(i-))?LOG[i-]:LOG[i-]+;
} int ST(int l, int r, int i)
{
int k=LOG[r-l+];
if(i==)
return max(dp1[l][k],dp1[r-(<<k)+][k]);
if(i==)
return min(dp1[l][k],dp1[r-(<<k)+][k]);
}
int main()
{
int n, k; while(~scanf("%d%d",&n, &k))
{
int i, j;
init(n);
for(i=; i<=n; i++)
{
scanf("%d", &a[i]);
dp1[i][]=a[i];
}
for(j=; j<=; j++)
{
for(i=; i<=n; i++)
{
if(i+(<<j)->n)
break;
dp1[i][j]=min(dp1[i][j-], dp1[i+(<<(j-))][j-]);
}
}
for(i=; i<=n-k+; i++)
{
if(i!=)
printf(" ");
printf("%d", ST(i,i+k-,));
}
////// for(i=; i<=n; i++)
{
dp1[i][]=a[i];
for(j=; j<=; j++)
dp1[i][j]=;
}
for(j=; j<=; j++)
{
for(i=; i<=n; i++)
{
if(i+(<<j)->n)
break;
dp1[i][j]=max(dp1[i][j-], dp1[i+(<<(j-))][j-]);
}
}
printf("\n");
for(i=; i<=n-k+; i++)
{
if(i!=)
printf(" ");
printf("%d", ST(i,i+k-,));
}
printf("\n");
}
}

POJ 2823 Sliding Window ST RMQ的更多相关文章

  1. POJ 2823 Sliding Window + 单调队列

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

  2. POJ 2823 Sliding Window 题解

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

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

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

  4. POJ 题目2823 Sliding Window(RMQ,固定区间长度)

    Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 46507   Accepted: 13442 ...

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

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

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

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

  7. POJ 2823 Sliding Window & Luogu P1886 滑动窗口

    Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 66613   Accepted: 18914 ...

  8. POJ 2823 Sliding Window

    Sliding Window Time Limit: 12000MSMemory Limit: 65536K Case Time Limit: 5000MS Description An array ...

  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. POJ 1679 The Unique MST(最小生成树)

    Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...

  2. 针对“来用”团队项目之NABC分析

    本项目特点之一:扩展性强 NABC分析: N(need):我们这个开发的这个软件主要是集娱乐软件和实用工具于一身的大容器,这里面有很多应用程序,针对不同用户需要,至少有一款应用程序能够满足用户的需要, ...

  3. dataTables基础函数变量

    DataTable下有四个命名空间(namespace),分别是defaults,ext,models,oApi. Defaults:主要是用于初始化表格的一些选项. Ext:拓展项,提供额外的表格选 ...

  4. Sparsity Invariant CNNs

    文章链接 Abstract 本文研究稀疏输入下的卷积神经网络,并将其应用于稀疏的激光扫描数据的深度信息完成实验.首先,我们表明,即使当丢失数据的位置提供给网络时,传统卷积网络在应用于稀疏数据时性能也很 ...

  5. lol人物模型提取(九)——终章

      之前顺丰来人拍照取证了一下,又和卖家协商了一下,最后顺风只打算赔偿我23块钱的顺丰邮寄卡,就是当你寄货物的时候可以用这卡来抵邮钱.想想也没多少钱,对方也不同意微信或支付宝转账给我,索性不让顺丰公司 ...

  6. ZOJ 1909 I-Square

    https://vjudge.net/contest/67836#problem/I Given a set of sticks of various lengths, is it possible ...

  7. Web服务器性能压力测试工具

    一.http_load 程序非常小,解压后也不到100K http_load以并行复用的方式运行,用以测试web服务器的吞吐量与负载. 但是它不同于大多数压力测试工具,它可以以一个单一的进程运行,一般 ...

  8. Linux命令之查看cpu个数_核数_内存总数

    http://blog.csdn.net/cgwcgw_/article/details/10000053 cpu个数 cat /proc/cpuinfo | grep "physical ...

  9. ajax 请求 后台返回的文件流

    download(url) { var xhr = new XMLHttpRequest(); xhr.open('GET', url, true); // 也可以使用POST方式,根据接口 xhr. ...

  10. bzoj3473字符串&bzoj3277串

    题意:给定n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中至少k个字符串的子串.注意本质相同的子串多次出现算多次,如1 1 aaa这组数据答案为6,贡献1WA.代码里有些部分是为了 ...