UESTC_Sliding Window 2015 UESTC Training for Data Structures<Problem K>
K - Sliding Window
Time Limit: 18000/6000MS (Java/Others) Memory Limit: 131072/131072KB (Java/Others)
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 -3 -3 3 3 |
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>的更多相关文章
- 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 ...
- 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 ...
- UESTC_秋实大哥与战争 2015 UESTC Training for Data Structures<Problem D>
D - 秋实大哥与战争 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Subm ...
- UESTC_秋实大哥与快餐店 2015 UESTC Training for Data Structures<Problem C>
C - 秋实大哥与快餐店 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Sub ...
- UESTC_秋实大哥搞算数 2015 UESTC Training for Data Structures<Problem N>
N - 秋实大哥搞算数 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Subm ...
- UESTC_秋实大哥与线段树 2015 UESTC Training for Data Structures<Problem M>
M - 秋实大哥与线段树 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Sub ...
- UESTC_秋实大哥下棋 2015 UESTC Training for Data Structures<Problem I>
I - 秋实大哥下棋 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
- UESTC_秋实大哥打游戏 2015 UESTC Training for Data Structures<Problem H>
H - 秋实大哥打游戏 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Subm ...
- UESTC_秋实大哥去打工 2015 UESTC Training for Data Structures<Problem G>
G - 秋实大哥去打工 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Subm ...
随机推荐
- jquery图片滚动仿QQ商城带左右按钮控制焦点图片切换滚动
jquery图片滚动仿QQ商城带左右按钮控制焦点图片切换滚动 http://www.17sucai.com/pins/demoshow/382
- javascript delete机制学习
想了解delete的机制缘起一个现象,我无法解释,也无法理解. 首先看一下下面这个例子: var x = 1; delete x; //false 然后我又执行了一次: y = 2; delete y ...
- Android学习笔记__3__Android应用程序组成
Android开发必须要了解构造块,Android应用程序是由里有六个重要组成部分组成的,这六种构造块如下: ◆Activity ◆Intent Receiver ◆Service ◆Content ...
- 雅虎工程师初始化css
/*css reset code */ /**** 文字大小初始化,使1em=10px *****/ body { font-size:62.5%; } /* for IE/Win */ html&g ...
- 河内塔(hanoi)
理论: 河内塔: 1.有三根杆子A,B,C.A杆上有若干碟子 2.每次移动一块碟子,小的只能叠在大的上面 3.把所有碟子从A杆全部移到C杆上 讲解: 设A上有n个盘子.如果n=1,则将圆盘从A直接 ...
- Hook linux 网络封包
要注册一个hook函数需要用到nf_register_hook()或者nf_register_hooks()系统API和一个struct nf_hook_ops{}类型的结构体对象 一个简单的demo ...
- ACdream 1083 有向无环图dp
题目链接:点击打开链接 人民城管爱人民 Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 128000/64000 KB (Java/Othe ...
- java中的二进制
(1)按位与运算 & 1 & 1 = 1, 0 & 1 = 0 51 & 5 即 0011 0011 & 0000 0101 =0000 0001 = 1 ...
- gcc基本用法
GCC基本用法 GCC最基本的用法是: gcc [option] filenames option:编译器所需要的编译选项 filenames:要编译的文件名 gcc编译流程 都以 hello.c 为 ...
- 图片轮播插件 Slides-SlidesJS-3
图片轮播插件 Slides-SlidesJS-3 demo document 地址: http://slidesjs.com/