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. Java反编译插件jad

    原文地址:http://www.cnblogs.com/JimLy-BUG/p/5405868.html 1.首先下载jar文件:net.sf.jadclipse_3.3.0.jar  下载   2. ...

  2. Shell下通过echo+telnet在远端执行命令

    创建脚本cmd.sh,用于输入telnet的用户与密码,以及生成远端需要执行的命令   执行命令 MY_SIGN=/tmp/sign; (sh cmd.sh ) | (telnet localhost ...

  3. 《Java程序员面试笔试宝典》之为什么需要public static void main(String[] args)这个方法

    public staticvoid main(String[] args)为Java程序的入口方法,JVM在运行程序的时候,会首先查找main方法.其中,public是权限修饰符,表明任何类或对象都可 ...

  4. HDU4907小技巧

    原题http://acm.hdu.edu.cn/showproblem.php?pid=4907 Task schedule Time Limit: 2000/1000 MS (Java/Others ...

  5. fatal error: malformed or corrupted AST file: &#39;Unable to load module &quot;/Users/apple/Library/Developer

    在同一时候安装使用Xcode5, Xcode6之后, 常常遇到这个问题. fatal error: malformed or corrupted AST file: 'Unable to load m ...

  6. C#/.Net Post获取数据流的一种简单写法

    最近在弄一些第三方的平台,经常调用第三方的接口实现某些特定的功能 在实现的同时基本上都需要本地的数据经过服务器在Request到第三方的服务器中处理,再返回相应的数据结构体:json/xml 以下是我 ...

  7. JS 事件对象和事件冒泡

    1.事件对象 js的事件对象中保存了当前被触发事件的一些相关的属性信息,如事件源.事件发生时的鼠标位置.事件按键等. 事件对象的获取方法: IE中可以window.event直接获取,而Firefox ...

  8. asp.net 内部重定向

    1. /* * 2. * Context.RewritePath() * 使用给定路径重写 URL.(内部重写) * 内部请求重写 */ public static void TestTwo() { ...

  9. Nohttp网络请求数据,Post以及Get的简单实用以及设置缓存文字的的请求

    开局声明:这是基于nohttp1.0.4-include-source.jar版本写的教程 由于nohttp功能强悍,因此需要多种权限,仅仅一个联网的权限是不够的,如果只给了Internet的权限,去 ...

  10. android 注释常用标签

    javadoc: {@link ActivityGroup}   链接到包.类: {@link #setContentView} 用#链接到类成员: @return View The current ...