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. canvas学习(二):渐变与曲线的绘制

    canvas学习(二):渐变与曲线的绘制 一:createLinearGradient()线性渐变: 二:createLinearGradient() 放射状/圆形渐变: 三:createPatter ...

  2. NFC学习总结

    NFC 学习总结 1.NFC 的基本概念 NFC 是 Near FieldCommunication 的缩写,即距离无线通信技术.由飞利浦公司和索尼公司共同开发的NFC 是一种非接触式识别和互联技术, ...

  3. mysql分页的limit优化

    1.很多新人都会很纳闷,为什么我建了索引使用分页还是这么卡.好,现在让我们一步一步去找原因吧. 首先limit本身跟索引没有直接关系. 先建一张商品sku表 create table goods_sk ...

  4. mysql唯一查询

    MySQL单一字段唯一其他字段差异性忽略查询.在使用MySQL时,有时需要查询出某个字段不重复的记录,虽然mysql提供 有distinct这个关键字来过滤掉多余的重复记录只保留一条,但往往只用它来返 ...

  5. Jedis源码解析——Jedis和BinaryJedis

    1.基本信息 先来看看他们的类定义: public class Jedis extends BinaryJedis implements JedisCommands, MultiKeyCommands ...

  6. JDK版本Java SE、Java EE、Java ME的区别

    想在win7 X64上搭建JAVA开发环境来着(只是尝试下),打开JAVA 官网下载JDK,发现好多版本懵了,百度了下找到这些版本的区别,故有了下文 1.JAVA SE Java2平台标准版(Java ...

  7. 2018 杭电多校1 - Distinct Values

    题目链接 Problem Description Chiaki has an array of n positive integers. You are told some facts about t ...

  8. Python Collections详解

    Python Collections详解 collections模块在内置数据结构(list.tuple.dict.set)的基础上,提供了几个额外的数据结构:ChainMap.Counter.deq ...

  9. 完全理解Python的 '==' 和 'is'

    '==' 比较的是两个对象的值 'is' 比较的是两个对象的内存地址(id) 下面我们着重理解 'is'.对于这个,我们需要知道:小整数对象池,大整数对象池,以及intern机制 小整数池:Pytho ...

  10. Python使用requests模块下载图片

    MySQL中事先保存好爬取到的图片链接地址. 然后使用多线程把图片下载到本地. # coding: utf-8 import MySQLdb import requests import os imp ...