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 ...
随机推荐
- Linux下core文件产生的一些注意问题
前面转载了一篇文章关于core文件的产生和调试使用的设置,但在使用有一些需要注意的问题,如 在什么情况 才会正确地产生core文件. 列出一些常见问题: 一,如何使用core文件 1. 使用core文 ...
- 解决Struts2.2.20版本的标签不支持style属性的问题
我先把Exception错误信息贴出来:org.apache.jasper.JasperException: /WEB-INF/jsp/topicAction/addUI.jsp (line: 40, ...
- Encode and Decode Strings 解答
Question Design an algorithm to encode a list of strings to a string. The encoded string is then sen ...
- Hdu5737-Differencia(有序表线段树)
题意很直观,我就不说了. 解析:这是我以前没有接触过的线段树类型,有序表线段树,每个节点申请了两段空间,主要是为了保存左边儿子会有多少比v小的,右边儿子会有多少比v小 的,所以在建树过程中要归并排序. ...
- android 缓存Bitmap - 开发文档翻译
由于本人英文能力实在有限,不足之初敬请谅解 本博客只要没有注明“转”,那么均为原创,转贴请注明本博客链接链接 Loading a single bitmap into your user interf ...
- jdbc资料收集
1.Hibernate史上最简单的Hibernate入门简介http://blog.csdn.net/doodoofish/article/details/43207/ jdbc不足 尽管JDBC在J ...
- Handsontable通用方法
1.clear():清空数据 2.createCol(index,amount,createAutomatically):添加列 index:列索引,amount:添加的列总数,crea ...
- Oracle-nomount/mount/open
通常所说的Oracle Server主要由两个部分组成:Instance和Database.Instance是指一组后台进程(在Windows上是一组线程)和一块共享内存区域:Database是指存储 ...
- EffectiveC#8--确保0对于值类型数据是有效的(初始化问题)
1.决不要创建一个不包括0在内的枚举类型 2.举例如下: public enum Planet { Mercury = 1, Venus = 2, Earth = 3, Mars = 4, Jupit ...
- 不用position,让div垂直居中
先弄懂after伪类的用法,就可以很容易理解了. <!DOCTYPE html> <html lang="en"><head> <meta ...