Link:http://poj.org/problem?id=2823
Sliding Window
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 8 3 1 3 -1 -3 5 3 6 7 Sample Output -1 -3 -3 -3 3 3 3 3 5 5 6 7 Source
POJ Monthly--2006.04.28, Ikki
|
题意:窗口大小为k,依次向右滑动窗口,求窗口中元素的最大值和最小值。
编程思想:求区间最值,可用线段树做。同时,由于该题区间固定,且只向右滑动,可以用单调队列维护这一过程,故可以用单调队列实现。
1、单调队列C++ 提交的AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#define PI acos(-1.0)
#define LINF 1000000000000000000LL
#define eps 1e-8
#define LL long long
#define MAXN 1000010
using namespace std;
const int INF=0x3f3f3f3f;
/*int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}*/
int n,k;
int a[MAXN],q1[MAXN],q2[MAXN],ans1[MAXN],ans2[MAXN];//q1:单调递增队列 q2:单调递减队列
int l1=1,l2=1,r1,r2;
int main()
{
//n=read();
//k=read();
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
//a[i]=read();
scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
{
while(l1<=r1&&q1[l1]<=i-k) l1++;
while(l2<=r2&&q2[l2]<=i-k) l2++;
while(l1<=r1&&a[i]<a[q1[r1]]) r1--;
q1[++r1]=i;
while(l2<=r2&&a[i]>a[q2[r2]]) r2--;
q2[++r2]=i;
ans1[i]=a[q1[l1]];
ans2[i]=a[q2[l2]];
}
for(int i=k;i<=n;i++) printf("%d ",ans1[i]);
puts("");
for(int i=k;i<=n;i++) printf("%d ",ans2[i]);
puts("");
return 0;
}
2、线段树求区间最值模板 C++提交AC代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#define max(a,b) a>b?a:b
#define min(a,b) a<b?a:b
#define M 1100010
using namespace std;
struct no{
int left ,right,max1,min1;
}tree[M<<2];
int a[M];
void build (int l,int r,int i){
tree[i].left=l;
tree[i].right=r;
if(l==r){
tree[i].min1=tree[i].max1=a[l];
return;
}
int mid=(l+r)>>1;
build(l,mid,i<<1);
build(mid+1,r,i<<1|1);
tree[i].min1=min(tree[i<<1].min1,tree[i<<1|1].min1);
tree[i].max1=max(tree[i<<1].max1,tree[i<<1|1].max1);
}
int mi,ma;
void query(int l,int r,int i){
if(tree[i].left>=l&&tree[i].right<=r){
mi=min(mi,tree[i].min1);
ma=max(ma,tree[i].max1);
return;
}
if(tree[i].left==tree[i].right) return;
int mid=(tree[i].left+tree[i].right)>>1;
if(r<=mid) query(l,r,i<<1);
else if(l>mid) query(l,r,i<<1|1);
else query(l,mid,i<<1),query(mid+1,r,i<<1|1);
}
int ans[M];
int main(){
int n,m;
while(~scanf("%d%d",&n,&m)){
for(int i=1;i<n+1;i++){
scanf("%d",&a[i]);
}
build(1,n,1);
if(m>=n){
mi=M*100,ma=-M*100;
query(1,n,1);
printf("%d\n%d\n",mi,ma);
continue;
}
int j=0;
for(int i=1;i<=n-m+1;i++){
mi=M*100,ma=-M*100;
query(i,m+i-1,1);
printf("%d ",mi);
ans[j]=ma;
j++;
}
puts("");
for(int i=0;i<j;i++){
printf("%d ",ans[i]);
}
puts("");
}
}