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. c++程序猿经典面试题(2)

    1.以下程序的输出结果是? #include<stdio.h> main(){ int b=3; int arr[]={6,7,8,9,10}; int *ptr=arr; *(ptr++ ...

  2. Odoo10对套件的处理

    Odoo10对套件的处理更强, 除了老版本支持的 销售套件, 按组件出货: 现在还增加了 采购套件, 按组件进货     建立 组件产品 KIT     设置 虚件BOM         测试,   ...

  3. 一些常用的shell

    1 if语句 if语句的三种写法,注意[]的两个空格,else if 写法是elif,不要漏了fi结束 if [ xxx ] then fi if [ xxx ] then echo "&q ...

  4. kubernetes安装过程中遇到问题及解决

    系列目录 根据机器环境的不同,有的可能一次就安装成功,有的则可能遇到各种各样的坑需要排查.建议不熟悉linux的用户使用全新的环境来安装kubernetes.以下记录本人在安装过程中遇到的问题及解决方 ...

  5. kubernetes滚动更新

    系列目录 简介 当kubernetes集群中的某个服务需要升级时,传统的做法是,先将要更新的服务下线,业务停止后再更新版本和配置,然后重新启动并提供服务.如果业务集群规模较大时,这个工作就变成了一个挑 ...

  6. MongoDB连接数与连接优化

    默认每个连接数占用10M内存 ulimit -a 查看stack size MongoDB服务器内存要满足 connection overhead + data size + index size 即 ...

  7. Java 多线程1(转载)

    来源:http://hllvm.group.iteye.com/group/wiki/2877-synchronized-volatile 最近想将java基础的一些东西都整理整理,写下来,这是对知识 ...

  8. iOS 优化方案浅析

    本文转载至 http://mobile.51cto.com/iphone-413256.htm Windows独特的注册表机制以及复杂的进程.内存管理,给了很多PC“优化”类软件极大的机遇,比如奇虎3 ...

  9. 【BZOJ4811】[Ynoi2017]由乃的OJ 树链剖分+线段树

    [BZOJ4811][Ynoi2017]由乃的OJ Description 由乃正在做她的OJ.现在她在处理OJ上的用户排名问题.OJ上注册了n个用户,编号为1-",一开始他们按照编号排名. ...

  10. EasyPlayer实现视频播放局部缩放、广角平移功能(类似水滴直播,快手视频)

    本文转自:http://blog.csdn.net/jyt0551/article/details/56063869 视频播放局部缩放.广角平移功能 在预览图片的时候,利用手势控制图片的缩放.平移,已 ...