poj 2823 Sliding Window (线段树 求固定区间的最大最小值 )
2015-08-17 13:27
393 查看
Sliding Window
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.
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
分析:题意很清楚,窗口向前滑动,每次求窗口内的最大值与最小值,这题应该可以用RMQ,线段树,单调区间来做,现在先给出先用线段树来做,以后再补充吧
用线段树做,因为要最大值一行,最小值一行,我们可以设置两个数组,minn[],maxn[], 建一棵树就可以,查询的时候可以有两种方法;
法一:每次遍历一棵树,直到找到所查询的区间,直接修改最值;这样比较快,时间6547(要用c++提交,似乎c++与G++不是一个编译器,G++较慢)
法二:分别查询最大值和最小值,将值传给数组,因为查询差不多比法一多了一倍,时间有点慢,8516ms
Time Limit: 12000MS | Memory Limit: 65536K | |
Total Submissions: 10791 | Accepted: 2934 | |
Case Time Limit: 5000MS |
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 |
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
分析:题意很清楚,窗口向前滑动,每次求窗口内的最大值与最小值,这题应该可以用RMQ,线段树,单调区间来做,现在先给出先用线段树来做,以后再补充吧
用线段树做,因为要最大值一行,最小值一行,我们可以设置两个数组,minn[],maxn[], 建一棵树就可以,查询的时候可以有两种方法;
法一:每次遍历一棵树,直到找到所查询的区间,直接修改最值;这样比较快,时间6547(要用c++提交,似乎c++与G++不是一个编译器,G++较慢)
#include<iostream> #include<string.h> #include<stdio.h> #include<algorithm> #include<cmath> using namespace std; const int MAX=1000010; const int INF=0x3f3f3f3f; int maxn[MAX],minn[MAX]; int maxm,minm; struct node { int ll,rr,minval,maxval; int getmid(){return (ll+rr)>>1;} }tree[MAX*4]; int num[MAX]; void build(int ll,int rr,int rt) { tree[rt].ll=ll; tree[rt].rr=rr; int mid=tree[rt].getmid(); if(ll==rr){ tree[rt].minval=tree[rt].maxval=num[rr]; return; } build(ll,mid,rt<<1); build(mid+1,rr,rt<<1|1); tree[rt].minval=min(tree[rt<<1].minval,tree[rt<<1|1].minval); tree[rt].maxval=max(tree[rt<<1].maxval,tree[rt<<1|1].maxval); } void query(int ll,int rr,int rt) { //if(tree[rt].maxval<=maxm&&tree[rt].minval>=minm)return; if(tree[rt].ll==ll&&tree[rt].rr==rr){ minm=min(tree[rt].minval,minm); maxm=max(tree[rt].maxval,maxm); return; } int mid=tree[rt].getmid(); if(rr<=mid) query(ll,rr,rt<<1); else if(mid<ll) query(ll,rr,rt<<1|1); else{ query(ll,mid,rt<<1); query(mid+1,rr,rt<<1|1); } } int main() { int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d",&num[i]); build(1,n,1); for(int i=1;i<=n-m+1;i++){ int j=i+m-1; maxm=-INF; minm=INF; query(i,j,1); minn[i]=minm; maxn[i]=maxm; } for(int i=1;i<n-m+1;i++) printf("%d ",minn[i]); printf("%d\n",minn[n-m+1]); for(int i=1;i<n-m+1;i++) printf("%d ",maxn[i]); printf("%d\n",maxn[n-m+1]); }
法二:分别查询最大值和最小值,将值传给数组,因为查询差不多比法一多了一倍,时间有点慢,8516ms
#include<iostream> #include<string.h> #include<stdio.h> #include<algorithm> #include<cmath> using namespace std; const int MAX=1000010; int maxn[MAX],minn[MAX]; struct node { int ll,rr,minval,maxval; int getmid(){return (ll+rr)>>1;} }tree[MAX*4]; int num[MAX]; void build(int ll,int rr,int rt) { tree[rt].ll=ll; tree[rt].rr=rr; int mid=tree[rt].getmid(); if(ll==rr){ tree[rt].minval=tree[rt].maxval=num[rr]; return; } build(ll,mid,rt<<1); build(mid+1,rr,rt<<1|1); tree[rt].minval=min(tree[rt<<1].minval,tree[rt<<1|1].minval); tree[rt].maxval=max(tree[rt<<1].maxval,tree[rt<<1|1].maxval); } int query(int ll,int rr,int rt,bool flag) { //if(tree[i].) if(tree[rt].ll==ll&&tree[rt].rr==rr){ if(flag) return tree[rt].minval; else return tree[rt].maxval; } int mid=tree[rt].getmid(); if(rr<=mid) return query(ll,rr,rt<<1,flag); else if(mid<ll) return query(ll,rr,rt<<1|1,flag); else{ //query(ll,mid,rt<<1,flag); // query(mid+1,rr,rt<<1|1,flag); if(flag)return min(query(ll,mid,rt<<1,flag),query(mid+1,rr,rt*2+1,flag)); else return max(query(ll,mid,rt<<1,flag),query(mid+1,rr,rt*2+1,flag)); } } int main() { int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d",&num[i]); build(1,n,1); int maxm,minm; for(int i=1;i<=n-m+1;i++){ int j=i+m-1; minn[i]=query(i,j,1,1); maxn[i]=query(i,j,1,0); } for(int i=1;i<n-m+1;i++) printf("%d ",minn[i]); printf("%d\n",minn[n-m+1]); for(int i=1;i<n-m+1;i++) printf("%d ",maxn[i]); printf("%d\n",maxn[n-m+1]); }
相关文章推荐
- POJ 1182 食物链
- 选择排序
- android中的与GPS定位的API
- 九度oj 1067
- IIC知识
- 极客编程小挑战#30:用CSS3实现各种钟表的显示效果
- F.lux——自动调整屏幕亮度与颜色,降低蓝光
- AS3写的页面注册登录组件
- 【effective c++读书笔记】【第7章】模板和泛型编程(3)
- 【effective c++读书笔记】【第7章】模板和泛型编程(3)
- 如何在QML应用中调用系统设置中的页面来设置我们的系统
- 【bzoj3306】 树 dfs序+线段树
- UI星辰:UI代码创建视图和Label设置
- CGI编程完全手册
- Uncaught TypeError: Cannot read property 'prototype' of null using Openerp 7.0
- mysql中文乱码解决方案
- IOS中CALayer的使用
- mac 下修改mysql的密码
- 栈 Deque
- android 音乐模仿器demo,实现指定目录下的音乐循环播放,和在线播放的方法。