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. 基础数据类型-tuple

    Python中,元组tuple与list类似,不同之处在于tuple的元素不能修改,tuple使用(),list使用[], (1)元组的创建使用(),需要注意的是创建包含一个元素的元组: tuple_ ...

  2. HDU 4617 Weapon(三维几何)

    Problem Description Doctor D. are researching for a horrific weapon. The muzzle of the weapon is a c ...

  3. Notes of the scrum meeting(12.12)

    meeting time:19:30~20:30p.m.,December 12th,2013 meeting place:3号公寓一层 attendees: 顾育豪                  ...

  4. 福大软工1816 · 第五次作业 - 结对作业2_map与unordered map的比较测试

    测试代码: #include <iostream> using namespace std; #include <string> #include <windows.h& ...

  5. Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset Trie

    题目链接: http://codeforces.com/contest/706/problem/D D. Vasiliy's Multiset time limit per test:4 second ...

  6. LintCode-69.二叉树的层次遍历

    二叉树的层次遍历 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) 样例 给一棵二叉树 {3,9,20,#,#,15,7} : 返回他的分层遍历结果: [     [3],     [9,2 ...

  7. ZOJ 1913 J-Eucild's Game

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

  8. 某一线互联网公司前端面试题总结css部分

    1,css3选择器 :not(selector) 选择页面内所有type!=text的类型: input:not([type=text]){ color: red; font-weight: bold ...

  9. QObject类 moc处理后代码

    QObject在QT中是所有类的基类,经过MOC处理后代码如下 之所以贴出这段代码,是因为很多流程追踪到最后一些关键性函数都是出自这个类 源码 4.8.6 MOC版本 63 /************ ...

  10. HDU 3879 Base Station(最大权闭合子图)

    将第i个用户和他需要的基站连边,转化成求二分图的最大权闭合子图. 答案=正权点之和-最小割. # include <cstdio> # include <cstring> # ...