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. eclipse 创建并运行maven web项目

    这两天想在eclipse上运行maven web项目,折腾了许久,总算success啦. 1,利用eclipse创建dynamic web project(eclipse需要安装m2eclipse). ...

  2. 【week3】四则运算 单元测试

    上一周的四则运算有bug,这次补充正确代码: // 中缀转后缀 public String[] SolveOrder(String[] in, HashMap<String, Integer&g ...

  3. ZOJ 1913 J-Eucild's Game

    https://vjudge.net/contest/67836#problem/J Two players, Stan and Ollie, play, starting with two natu ...

  4. 修改CSV中的某些值

    file.csv文件如下,然后对其中某些值进行变换操作,刚学Powershell的时候操作起来很麻烦,现在看来其实就是对于哈希表的操作. col1,col2,col3,col4 text1,text2 ...

  5. opencv2.4.0版本不支持Mat的大小自动调整?

    在opencv2.4.9中,resize(img,img,Size(850,550))是没问题的.到了2.4.0中,要新声明一个变量Mat img1;resize(img,img1,Size(850, ...

  6. WPF布局间的切换方法

    效果图,两种效果间的切换

  7. Struts2自定义结果视图(servlet验证码)

    1.编写一个类实现com.opensymphony.xwork2.Result,或者继承org.apache.struts2.dispatcher.StrutsResultSupport 2.自定义的 ...

  8. BZOJ4237 稻草人(分治+树状数组+单调栈)

    如果要询问的某个纵坐标为inf的点左边是否有点能与其构成所要求的矩形,只要用个单调栈就可以了.可以想到用分治来制造单调性. 按横坐标排序,每次考虑跨过分治中心的矩形.考虑右边的每个点能与左边的哪些点构 ...

  9. 《高性能MySQL》 读书总结

    目录: 第一章.MySQL架构与历史 第二章.MySQL基准测试 第三章.服务器性能剖析 第四章.Schema与数据类型优化 第五章.创建高性能的索引 第六章.查询性能优化 第七章.MySQL高级特性 ...

  10. Android Fragment 使用详解

    虽然网上有很多关于Fragment的文章,但我这里还是要写这篇笔记,因为我在编写程序的过程中发现了一个问题,至今未解决,希望得到大家的帮助: PS:当我在Fragment中定义一个名为setIndex ...