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. Reverse Linked List II 解答

    Question Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Giv ...

  2. TypeScript 素描 - 模块解析、声明合并

    模块解析 模块解析有两种方式 相对方式  也就是以/或 ./或-/开头的,比如import jq  from "/jq" 非相对方式  比如 import model  from ...

  3. Android学习总结——判断网络状态

    package com.example.xch.broadcasttest; import android.content.BroadcastReceiver; import android.cont ...

  4. Spark1.0.0 分布式环境搭建

    软件版本号例如以下: Hostname IP Hadoop版本号 Hadoop 功能 系统 master 192.168.119.128 1.1.2 namenode jdk1.6+hadoop+sc ...

  5. POJ 1987 BZOJ 3365 Distance Statistics 树的分治(点分治)

    题目大意:(同poj1741,刷一赠一系列) CODE: #include <cstdio> #include <cstring> #include <iostream& ...

  6. SQLLoader4(数据文件中的列与表中列不一致情况-filler)

    A.数据文件中字段个数少于表中列字段个数,但数据文件中缺少的列,在表定义中可以为空.----- 这种情况是比较简单的,只需要将数据文件中数据对应的列的名字写到控制文件中即可.因为SQL*Loader是 ...

  7. Android学习笔记--服务(Service)

    1.服务概述 1.服务是Android四大组件之一,在使用上可以分为本地服务和远程服务,本地服务是指在不影响用户操作的情况下在后台默默的执行一个耗时操作,例如下载,音频播放等.远程服务是指可以供其他应 ...

  8. ToString()和Convert.ToString()的区别

    ToString()和Convert.ToString()的区别 一般情况下,这两种方法都可以通用,但是当返回的数据类型中有可能出现null值时如果调用ToString方法了,就会返回NullRefe ...

  9. javascript学习笔记(一)

    学习书籍 Javascript高级程序设计 第3,4章 javascript数据类型 Undefined(undefined) Null(null,空指针) Boolean(true,false) N ...

  10. overflow清楚浮动 + 去掉li标签的小圆点

    原文链接:http://blog.163.com/qqabc20082006@126/blog/static/22928525201031211212955/ 测试用例: <!DOCTYPE h ...