K - Sliding Window

Time Limit: 18000/6000MS (Java/Others)     Memory Limit: 131072/131072KB (Java/Others)
Submit Status

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

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 and output

Sample Input Sample Output
8 3
1 3 -1 -3 5 3 6 7
-1 -3 -3 -3 3 3
3 3 5 5 6 7

Hint

The data used in this problem is unofficial data prepared by love8909. So any mistake here does not imply mistake in the offcial judge data.

解题报告

滑动窗口问题,我们可以在O(1)的时间内得到某个点的答案,就是维护一个单调队列,首先考虑最大值问题,我们考虑 i < j,且a[i] < a[j],显然可以得到a[i]是根本无用的(因为从左往右滑,a[j]未出之前a[i]根本不可能最优),因此我们只需维护一个单调递减的队列的即可,即可在O(1)的时间内得到某个点最优值.

插入时也要维护单调性,不再累述.

最小值同理.

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio> using namespace std;
const int maxn = 1e6 + ;
int n,k,q[maxn],h[maxn]; int main(int argc,char *argv[])
{
scanf("%d%d",&n,&k);
int front = , rear = ;
for(int i = ; i < n ; ++ i)
scanf("%d",&h[i]);
// Judge
if (k >= n)
k = n;
// Init
q[rear++] = ;
for(int i = ; i < k ; ++ i)
{
while(h[i] <= h[q[rear-]] && front < rear)
rear--;
q[rear++] = i;
}
printf("%d",h[q[front]]);
for(int i = k ; i < n ; ++ i)
{
while(front < rear && i - q[front] >= k)
front++;
while(h[i] <= h[q[rear-]] && front < rear)
rear--;
q[rear++] = i;
printf(" %d",h[q[front]]);
}
printf("\n");
// ReInit
front = , rear = , q[rear++] = ;
for(int i = ; i < k ; ++ i)
{
while(h[i] >= h[q[rear-]] && front < rear)
rear--;
q[rear++] = i;
}
printf("%d",h[q[front]]);
for(int i = k ; i < n ; ++ i)
{
while(front < rear && i - q[front] >= k)
front++;
while(h[i] >= h[q[rear-]] && front < rear)
rear--;
q[rear++] = i;
printf(" %d",h[q[front]]);
}
printf("\n");
return ;
}

UESTC_Sliding Window 2015 UESTC Training for Data Structures<Problem K>的更多相关文章

  1. UESTC_Rain in ACStar 2015 UESTC Training for Data Structures<Problem L>

    L - Rain in ACStar Time Limit: 9000/3000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Other ...

  2. UESTC_Islands 2015 UESTC Training for Data Structures<Problem J>

    J - Islands Time Limit: 30000/10000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Su ...

  3. UESTC_秋实大哥与战争 2015 UESTC Training for Data Structures<Problem D>

    D - 秋实大哥与战争 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Subm ...

  4. UESTC_秋实大哥与快餐店 2015 UESTC Training for Data Structures<Problem C>

    C - 秋实大哥与快餐店 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Sub ...

  5. UESTC_秋实大哥搞算数 2015 UESTC Training for Data Structures<Problem N>

    N - 秋实大哥搞算数 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Subm ...

  6. UESTC_秋实大哥与线段树 2015 UESTC Training for Data Structures<Problem M>

    M - 秋实大哥与线段树 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Sub ...

  7. UESTC_秋实大哥下棋 2015 UESTC Training for Data Structures<Problem I>

    I - 秋实大哥下棋 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  8. UESTC_秋实大哥打游戏 2015 UESTC Training for Data Structures<Problem H>

    H - 秋实大哥打游戏 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Subm ...

  9. UESTC_秋实大哥去打工 2015 UESTC Training for Data Structures<Problem G>

    G - 秋实大哥去打工 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Subm ...

随机推荐

  1. Asp.net MVC中的ViewData与ViewBag(转)

    在Asp.net MVC 3 web应用程序中,我们会用到ViewData与ViewBag,对比一下: ViewData ViewBag 它是Key/Value字典集合 它是dynamic类型对像 从 ...

  2. Visual Assist X在Windows 8.1下出现中文乱码的解决方法

    这主要是输入法造成的,我的输入法中有US.中文.搜狗输入法三个输入法:通过搜狗输入法管理器把“中文”去掉,或者通过语言首选项把“中文”去掉就不会在出现乱码. 这个办法的思路来自于http://www. ...

  3. 利用ant进行编译和发布项目

    本文通过一个示例来解说如何通过ant进行编译和发布项目.本例按如下目录结构来组织项目. D:/web/antsample项目根目录 D:/web/antsample/src源代码目录 D:/web/a ...

  4. 写PPT的方法

    这个方法是今天同事的方法,看到他的PPT简洁高效,明了,记下了他的方法: 写文字:写框架,这个框架或者内容可以是word形式的,目的是展示内容 找模板:在搜集到的各种ppt模板中选几个适合自己文字的页 ...

  5. [CSS3] CSS Display Property: Block, Inline-Block, and Inline

    Understanding the most common CSS display types of block, inline-block, and inline will allow you to ...

  6. openssl 对称加密算法enc命令详解

    1.对称加密算法概述 openssl的加密算法库提供了丰富的对称加密算法,我们可以通过openssl提供的对称加密算法指令的方式使用,也可以通过调用openssl提供的API的方式使用. openss ...

  7. EffectiveC#01--避免返回内部类对象的引用

    此篇是对00中第3点的再一次阐述. 1.如果一个属性返回一个引用类型,那么调用者就可以访问这个对象的公共成员,也包括修改这些属性的状态. public class MyBusinessObject { ...

  8. css-布局1-基本属性

    <!DOCTYPE html>CSS4-布局1-基本属性 属性:displayvisibilityfloatclear HTML元素类型:行内元素,块级元素 块级元素:最大的区别:换行行内 ...

  9. 利用PHP/MYSQL实现的简易微型博客(转)

    数据库:ly_php_base 表:ly_micro_blog(仅仅有一个表)字段:id,title,date,content,hits 文件: 文件 描述 default.php 默认主页.显示博文 ...

  10. 在asp.net中导出表格Excel数据

    第一步:需要引用org.in2bits.MyXls程序集到使用页面 第二步:前台代码 <asp:Button ID="LeadingOut" runat="serv ...