POJ_2823_Sliding Window(RMQ)
2015-08-21 22:32
465 查看
Sliding Window
Submit
Status
Description
An array of size n ≤ 10 6 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.
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
Sample Output
题意:有n个数,每次从左到右选取k个数,第一行选取每个区间中的最小值输出,第二行每次选取区间中的最大值输出。
分析:RMQ求解。题目数据量比较大,如果用一般的二维数组显然是要MLE的,由于此题是固定的区间长度,所以只要用一维数组即可,第二维可以省略。
题目链接:http://poj.org/problem?id=2823
代码清单:
Time Limit: 12000MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
Status
Description
An array of size n ≤ 10 6 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 |
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
题意:有n个数,每次从左到右选取k个数,第一行选取每个区间中的最小值输出,第二行每次选取区间中的最大值输出。
分析:RMQ求解。题目数据量比较大,如果用一般的二维数组显然是要MLE的,由于此题是固定的区间长度,所以只要用一维数组即可,第二维可以省略。
题目链接:http://poj.org/problem?id=2823
代码清单:
#include<set> #include<map> #include<cmath> #include<queue> #include<stack> #include<ctime> #include<cstdio> #include<string> #include<cctype> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; const int maxn = 1e6 + 5; int n,k; int a[maxn]; int minq[maxn]; int maxq[maxn]; void input(){ scanf("%d%d",&n,&k); for(int i=1;i<=n;i++) scanf("%d",&a[i]); } void RMQ_ST(){ int L=(int)((log(k*1.0))/(log(2.0))); for(int i=1;i<=n;i++) minq[i]=maxq[i]=a[i]; for(int j=1;j<=L;j++){ for(int i=1;i+(1<<j)-1<=n;i++){ minq[i]=min(minq[i],minq[i+(1<<(j-1))]); maxq[i]=max(maxq[i],maxq[i+(1<<(j-1))]); } } } void solve(){ RMQ_ST(); int mi=(int)((log(k*1.0))/(log(2.0))); for(int i=1;i+k-1<=n;i++){ printf("%d%c",min(minq[i],minq[i+k-(1<<mi)]),i+k-1==n?'\n':' '); } for(int i=1;i+k-1<=n;i++){ printf("%d%c",max(maxq[i],maxq[i+k-(1<<mi)]),i+k-1==n?'\n':' '); } } int main(){ input(); solve(); return 0; }
相关文章推荐
- [NBUT 1458 Teemo]区间第k大问题,划分树
- (读书笔记)人人都是产品经理-苏杰
- GDOI模拟8.21总结
- 51nod Bash游戏(V1,V2,V3,V4(斐波那契博弈))
- 对太极的感悟
- Android开源载入视图(loading)控件分享
- 最佳拍档之内部存储和外部存储(sdcard)
- 架构必备:Rate limiting 的作用和常见方式 - 互联网技术和架构
- 关于android中调用系统拍照,返回图片是旋转90度..
- hdu 5411 CRB and Puzzle(矩阵快速幂)
- 2015年8月21日工作日志---------赵鑫
- 统计每个月兔子的总数
- 教育O2O在学校落地,学堂在线瞄准混合式教学
- utf-8和gbk切换
- Reverse Linked List II
- 这样做,你会受益匪浅!
- 模拟登录学校教务管理系统,获取成绩单!
- 为何?
- hdu 5410 CRB and His Birthday(背包)
- 在线教育未来?这是个值得深思的问题。