Sliding Window
Time Limit: 12000MS   Memory Limit: 65536K
Total Submissions: 41264   Accepted: 12229
Case Time Limit: 5000MS

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

我按照自己的思路进行了部分优化模拟,仍然超时。
代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <deque> using namespace std; struct node
{
int mi;
int ma;
}q[1000004]; int a[1000004]; int main()
{
int n, k;
int i, j; scanf("%d %d", &n, &k);
for(int i=0; i<n; i++)
scanf("%d", &a[i] );
int dd=a[0], ff=a[0];
for(i=1; i<k; i++)
{
if(a[i]>dd) dd=a[i]; //max
if(a[i]<ff) ff=a[i]; //min
}
int e=0;
q[e].ma=dd; q[e++].mi=ff;
int pos; for(j=k; j<n; j++ )// 遍历这些序列
{
if(a[j]>dd) dd=a[j];
if(a[j]<ff) ff=a[j]; //判断是不是要更新
pos=j-k;
if(a[pos]>dd && a[pos]<ff )
continue;
else
{
int p, w;
if(a[pos]==dd) //起点是==最大值 更新最大值
{
p=a[pos+1];
for(i=pos+2; i<=j; i++)
{
p=max(p, a[i]);
}
dd=p;
}
else if(a[pos]==ff) //起点是==最小值 更新最小值
{
w=a[pos+1];
for(i=pos+2; i<=j; i++)
{
w=min(w, a[i]);
}
ff=w;
}
q[e].ma=dd; q[e++].mi=ff;
}
}
for(i=0; i<e; i++)
{
if(i==e-1)
printf("%d\n", q[i].mi);
else
printf("%d ", q[i].mi);
}
for(i=0; i<e; i++)
{
if(i==e-1)
printf("%d\n", q[i].ma );
else
printf("%d ", q[i].ma );
}
return 0;
}
												

POJ 2823 Sliding Window (滑动窗口的最值问题 )的更多相关文章

  1. 洛谷P1886 滑动窗口(POJ.2823 Sliding Window)(区间最值)

    To 洛谷.1886 滑动窗口 To POJ.2823 Sliding Window 题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每 ...

  2. POJ 2823 Sliding Window + 单调队列

    一.概念介绍 1. 双端队列 双端队列是一种线性表,是一种特殊的队列,遵守先进先出的原则.双端队列支持以下4种操作: (1)   从队首删除 (2)   从队尾删除 (3)   从队尾插入 (4)   ...

  3. POJ 2823 Sliding Window 题解

    POJ 2823 Sliding  Window 题解 Description An array of size n ≤ 106 is given to you. There is a sliding ...

  4. POJ 2823 Sliding Window & Luogu P1886 滑动窗口

    Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 66613   Accepted: 18914 ...

  5. 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 ...

  6. 【POJ 2823】【Luogu P1886】Sliding Window 滑动窗口

    POJ 2823 Luogu P1886 [解题思路] 这是一个单调队列算法的经典题目,几乎学习单调队列的人都接触过这题. 利用单调队列算法求出每一个固定区间内的最(大/小)值. 以下以最大值为例: ...

  7. POJ 2823 Sliding Window(单调队列入门题)

      Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 67218   Accepted: 190 ...

  8. poj 2823 Sliding Window (单调队列入门)

    /***************************************************************** 题目: Sliding Window(poj 2823) 链接: ...

  9. POJ 2823 Sliding Window ST RMQ

    Description An array of size n ≤ 106 is given to you. There is a sliding window of size k which is m ...

随机推荐

  1. springboot 集成 freemarker

    前面我们已经实现了thymeleaf模板,其实freemarker和thymeleaf差不多,都可以取代JSP页面,实现步骤也差不多,我们来简单实现一下 引入pom.xml依赖如下 <depen ...

  2. Deep learning with PyTorch: A 60 minute blitz _note(1) Tensors

    Tensors 1. construst matrix 2. addition 3. slice from __future__ import print_function import torch ...

  3. Jackson反序列化错误:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field的解决方法

    说明:出现这种问题的情况是由于JSON里面包含了实体没有的字段导致反序列化失败. 解决方法: // 第一种解决方案 // ObjectMapper对象添加 mapper.configure(Deser ...

  4. javascript 函数初探 (五)--- 几种类型的函数

    即时函数: 目前我们已经讨论了匿名函数在回调时的应用.接下来,我们来看看匿名函数的另一种应用实例 --- javascript即时函数: 比如: ( function(){ alert('her'); ...

  5. Hibernate 3 深度解析--苏春波

    Hibernate 3 深度解析   Hibernate 作为 Java ORM 模式的优秀开源实现, 当下已经成为一种标准,为饱受 JDBC 折磨的 Java 开发者带来了“福音.快速的版本更新,想 ...

  6. 记录一下:关于mysql数据误删除恢复的问题

    大概看来几篇博客: 1.delete的可以通过回滚(rollback)的方式恢复;但是前提是,你的数据表引擎是InnoDB而不是MyISAM,而且操作不是自动提交的 但是这种方式不可以恢复trunca ...

  7. 怎样使用Entityframework.Extended

    这个插件真的非常有用,我们能够使用下面语法来简化我们的工作,下面不过演示样例: Deleting <strong>//delete all users where FirstName ma ...

  8. odoo小数精确度

    python round() 函数     Python用于四舍五入的内建函数round() ,它的定义为 意思是, 将 小数部分保留到 ndigits 指定的 小数位,也就是 精度保持到 ndigi ...

  9. C语言变长数组 struct中char data[0]的用法

    版权声明:本文为博主原创文章,未经博主允许不得转载. 今天在看一段代码时出现了用结构体实现变长数组的写法,一开始因为忘记了这种技术,所以老觉得作者的源码有误,最后经过我深思之后,终于想起以前看过的用s ...

  10. C语言关键字—-sizeof 、typedef、const、static、register、extern、#define

    关键字:sizeof .#define.typedef.const.static.register.extern sizeof 1. 作用:求数据所占得内存空间大小 2. 本质:求数据得类型所占的内存 ...