POJ 2823 Sliding Window
Sliding Window
Time Limit: 12000MS
Memory Limit: 65536K
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
1: #include <iostream>
2: #include <cstdio>
3: #include <cstring>
4: #include <algorithm>
5: using namespace std;
6: #define lson l,m,rt<<1
7: #define rson m+1,r,rt<<1|1
8: typedef long long ll;
9: const int maxn=1111111;
10: int a[maxn<<2],b[maxn<<2];
11: int small[maxn],big[maxn],cnt=0;
12:
13: void PushUp(int rt)
14: {
15: a[rt]=max(a[rt<<1],a[rt<<1|1]);
16: b[rt]=min(b[rt<<1],b[rt<<1|1]);
17: }
18:
19: void build(int l,int r,int rt)
20: {
21: if(l==r){
22: scanf("%d",&a[rt]);
23: b[rt]=a[rt];
24: return;
25: }
26: int m=(l+r)>>1;
27: build(lson); build(rson);
28: PushUp(rt);
29: }
30:
31: void query(int L,int R,int l,int r,int rt,int &ma,int &mi)
32: {
33: if(L<=l&&R>=r)
34: {
35: ma=a[rt];
36: mi=b[rt];
37: return;
38: }
39: int m=(r+l)>>1;
40: if(L<=m&&R>m)
41: {
42: int x1,y1,x2,y2;
43: query(L,R,lson,x1,y1);
44: query(L,R,rson,x2,y2);
45: ma=max(x1,x2);
46: mi=min(y1,y2);
47: }
48: else if(L<=m)
49: query(L,R,lson,ma,mi);
50: else
51: query(L,R,rson,ma,mi);
52: }
53:
54: void show(int n,int e[])
55: {
56: for(int i=0; i<n ; i++){
57: if(i) printf(" ");
58: printf("%d",e[i]);
59: }
60: printf("\n");
61: }
62:
63: void run(int n,int k)
64: {
65: cnt=0;
66: build(1,n,1);
67: for(int i=k; i<=n; i++)
68: {
69: query(i-k+1,i,1,n,1,big[cnt],small[cnt]);
70: cnt++;
71: }
72: show(cnt,small);
73: show(cnt,big);
74: }
75:
76: int main()
77: {
78: int n,k;
79: while(scanf("%d%d",&n,&k)!=EOF)
80: run(n,k);
81: return 0;
82: }
POJ 2823 Sliding Window的更多相关文章
- POJ 2823 Sliding Window + 单调队列
一.概念介绍 1. 双端队列 双端队列是一种线性表,是一种特殊的队列,遵守先进先出的原则.双端队列支持以下4种操作: (1) 从队首删除 (2) 从队尾删除 (3) 从队尾插入 (4) ...
- POJ 2823 Sliding Window 题解
POJ 2823 Sliding Window 题解 Description An array of size n ≤ 106 is given to you. There is a sliding ...
- 洛谷P1886 滑动窗口(POJ.2823 Sliding Window)(区间最值)
To 洛谷.1886 滑动窗口 To POJ.2823 Sliding Window 题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每 ...
- poj 2823 Sliding Window (单调队列入门)
/***************************************************************** 题目: Sliding Window(poj 2823) 链接: ...
- 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 ...
- POJ 2823 Sliding Window(单调队列入门题)
Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 67218 Accepted: 190 ...
- POJ 2823 Sliding Window & Luogu P1886 滑动窗口
Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 66613 Accepted: 18914 ...
- 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 ...
- POJ 2823 Sliding Window (滑动窗口的最值问题 )
Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 41264 Accepted: 12229 ...
随机推荐
- 基于吉日嘎底层架构的Web端权限管理操作演示-角色管理
上一篇介绍了用户管理,这篇来介绍角色管理,这是权限管理的核心部分,因为我们的权限管理系统是基于角色的,有个高大上的英文名叫RBAC(Role Based Acccess Control). 下面的这段 ...
- 不可或缺 Windows Native (9) - C 语言: 动态分配内存,链表,位域
[源码下载] 不可或缺 Windows Native (9) - C 语言: 动态分配内存,链表,位域 作者:webabcd 介绍不可或缺 Windows Native 之 C 语言 动态分配内存 链 ...
- [moka同学笔记]百度编辑器Ueditor自动换行,添加<p>的问题(摘录)
原文:http://www.cnblogs.com/kissdodog/p/5419919.html 百度编辑器Ueditor其实蛮好用的,后来使用了一段时间发现,每次打开后又保存,发现都会往内容的 ...
- 利用php实现:当获取的网址不是特定网址时候跳转到指定地址
这个问题是在百度知道看到的问答,我不懂做,特定去百度了下.然后结合别人获取域名和跳转的知识,综合做了这个功能,以下是实现代码: <?php //获取当前的域名: echo "获取到的域 ...
- linux多线程-互斥&条件变量与同步
多线程代码问题描述 我们都知道,进程是操作系统对运行程序资源分配的基本单位,而线程是程序逻辑,调用的基本单位.在多线程的程序中,多个线程共享临界区资源,那么就会有问题: 比如 #include < ...
- javascript函数中的三个技巧【一】
在学习javascript中,函数是非常重要的,现在我来谈谈对函数的理解以及在工作和用法中的一些技巧 技巧一. [作用域安全的构造函数] 构造函数其实就是一个使用new操作调用的函数 function ...
- SharePoint 2013 Installation and Configuration Issues
# Issue 1: During Installing SharePoint 2013 Prerequisites there was an error in installing Applicat ...
- 【读书笔记】iOS-KVC
一,KVC即键/值编码. 二,KVC的基本调用包括-valueForKey:和-setValue:forKey:. 三,对于KVC,Cocoa自动放入和取出标量值.也就是说,当使用setValueFo ...
- CSS 属性选择器(八)
一.属性选择器 属性选择使用中括号进行标识,中括号内包含属性名,属性值或者属性表达式 如h1[title],h1[title="Logon"], 二.属性选择器分类 2.1.匹配属 ...
- 【转】牛人整理分享的面试知识:操作系统、计算机网络、设计模式、Linux编程,数据结构总结
基础篇:操作系统.计算机网络.设计模式 一:操作系统 1. 进程的有哪几种状态,状态转换图,及导致转换的事件. 2. 进程与线程的区别. 3. 进程通信的几种方式. 4. 线程同步几种方式.(一定要会 ...