Sliding Window
Time Limit: 12000MS   Memory Limit: 65536K
Total Submissions: 50738   Accepted: 14590
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"cstdio"
#include"cstring"
#include"queue"
#include"vector"
using namespace std;
const int MAXN=;
int a[MAXN];
int Min[MAXN];
int Max[MAXN];
int n,k;
int cnt;
struct comp1{
bool operator()(const int x,const int y)
{
return a[x] > a[y];
}
};
struct comp2{
bool operator()(const int x,const int y)
{
return a[x] < a[y];
}
};
priority_queue<int,vector<int>,comp1> que1;
priority_queue<int,vector<int>,comp2> que2;
int main()
{
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=k;i++)
{
que1.push(i);
que2.push(i);
}
Min[cnt]=a[que1.top()];
Max[cnt]=a[que2.top()];
cnt++;
for(int i=k+;i<=n;i++)
{
que1.push(i);
que2.push(i);
while(i-que1.top()>=k) que1.pop();// 将窗口之外的删除
Min[cnt]=a[que1.top()];
while(i-que2.top()>=k) que2.pop();
Max[cnt]=a[que2.top()];
cnt++;
}
for(int i=;i<cnt;i++)
printf("%d%c",Min[i],(i==cnt-)?'\n':' ');
for(int i=;i<cnt;i++)
printf("%d%c",Max[i],(i==cnt-)?'\n':' ');
return ;
}

POJ2823(优先队列)的更多相关文章

  1. 堆排序与优先队列——算法导论(7)

    1. 预备知识 (1) 基本概念     如图,(二叉)堆是一个数组,它可以被看成一个近似的完全二叉树.树中的每一个结点对应数组中的一个元素.除了最底层外,该树是完全充满的,而且从左向右填充.堆的数组 ...

  2. 数据结构:优先队列 基于list实现(python版)

    #!/usr/bin/env python # -*- coding:utf-8 -*- #Author: Minion-Xu #list实现优先队列 class ListPriQueueValueE ...

  3. python优先队列,队列和栈

    打印列表的疑问 class Node: def __str__(self): return "haha" print([Node(),Node()]) print(Node()) ...

  4. 数据结构作业——Sanji(优先队列)

    山治的婚约 Description 我们知道,山治原来是地下有名的杀人家族文斯莫克家族的三子,目前山治的弟弟已经出现,叫做四治,大哥二哥就叫汪(One)治跟突(Two)治好了(跟本剧情无关) .山治知 ...

  5. Java优先队列

    按照Java api的说法: java.util.PriorityQueue.PriorityQueue() Creates a PriorityQueue with the default init ...

  6. 优先队列实现Huffman编码

    首先把所有的字符加入到优先队列,然后每次弹出两个结点,用这两个结点作为左右孩子,构造一个子树,子树的跟结点的权值为左右孩子的权值的和,然后将子树插入到优先队列,重复这个步骤,直到优先队列中只有一个结点 ...

  7. “玲珑杯”ACM比赛 Round #7 B -- Capture(并查集+优先队列)

    题意:初始时有个首都1,有n个操作 +V表示有一个新的城市连接到了V号城市 -V表示V号城市断开了连接,同时V的子城市也会断开连接 每次输出在每次操作后到首都1距离最远的城市编号,多个距离相同输出编号 ...

  8. Dijkstra算法优先队列实现与Bellman_Ford队列实现的理解

    /* Dijkstra算法用优先队列来实现,实现了每一条边最多遍历一次. 要知道,我们从队列头部找到的都是到 已经"建好树"的最短距离以及该节点编号, 并由该节点去更新 树根 到其 ...

  9. 数据结构作业——ギリギリ eye(贪心+优先队列/贪心+并查集)

    ギリギリ eye Description A.D.1999,由坠落地球的“谜之战舰”带来的 Over Technology,揭示了人类历史和远古文明之间的丝丝联系, 促使人类终止彼此间的战争,一方面面 ...

随机推荐

  1. AngularJS的简单表单验证

    代码下载:https://files.cnblogs.com/files/xiandedanteng/angularjsCheckSimpleForm.rar 代码: <!DOCTYPE HTM ...

  2. windows程序设计——飞机大战笔记(Access数据库的使用)

    //////////////////2015/07/22/////////////////// /////////////////by xbw ///////////////////////// // ...

  3. shell(2):正则表达式

    一.整理正则表达式博客 (1)正则 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物的规则. 在linux中,通配符是由shel ...

  4. JAVA虚拟机、Dalvik虚拟机和ART虚拟机简要对照

    1.什么是JVM? JVM本质上就是一个软件,是计算机硬件的一层软件抽象,在这之上才干够运行Java程序,JAVA在编译后会生成相似于汇编语言的JVM字节码,与C语言编译后产生的汇编语言不同的是,C编 ...

  5. 九度OJ 1090:路径打印 (树、DFS)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1704 解决:314 题目描述: 给你一串路径,譬如: a\b\c a\d\e b\cst d\ 你把这些路径中蕴含的目录结构给画出来,子目 ...

  6. BZOJ3231: [Sdoi2008]递归数列

    BZOJ3231: [Sdoi2008]递归数列 Description 一个由自然数组成的数列按下式定义: 对于i <= k:ai = bi 对于i > k: ai = c1ai-1 + ...

  7. wifi 协议栈的历史的总结

    google 了一下找到下面的网页关于wifi 协议栈的说明 https://www.lifewire.com/wireless-standards-802-11a-802-11b-g-n-and-8 ...

  8. 【Effective C++】让自己习惯C++

    条款01:视C++为一个语言联绑 C++的四个语言层次: C:C++是以C为基础的.基本数据类型.语句.预处理器.数组.指针等统统来自C. Oject-Oriented C++:面向对象这一特性包含了 ...

  9. Machine Learning in Action(1) K-近邻

    机器学习分两大类,有监督学习(supervised learning)和无监督学习(unsupervised learning).有监督学习又可分两类:分类(classification.)和回归(r ...

  10. PAT 天梯赛 L3-010. 是否完全二叉搜索树 【Tree】

    题目链接 https://www.patest.cn/contests/gplt/L3-010 思路 因为是 完全二叉搜索树 可以用 数据 建树的方式 然后 遍历一遍这个 数字 就是 层序遍历 遍历的 ...