SCU3139——Sliding Window【单调队列or线段树】

Time Limit: 3000 MS Memory Limit: 65536 K

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

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
5 1
1 2 3 4 5

Sample Output

-1 -3 -3 -3 3 3
3 3 5 5 6 7
1 2 3 4 5
1 2 3 4 5

Source

POJ Monthly–2006.04.28, Ikki

题目大意:
先输入两个整数 n n n k k k,再在第二行输入 n n n个整数组成的数组,从第一个数开始往后有一个长度为 k k k的窗口,窗口以每次一个数的速度向右移动,将每次窗口中的最小值,最大值分别输出。
解题思路:
思路一:线段树( O ( n l o g n ) O(nlogn) O(nlogn)
先对整个数组建立一个查询区间最大最小值的线段树,后面根据窗口的位置离线查询即可
代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
int a[5005000],b[5005000];
int arr[1001000];
void push_up(int x) {
	a[x]=min(a[x<<1],a[x<<1|1]);
	b[x]=max(b[x<<1],b[x<<1|1]);
}
void build(int l,int r,int x) {
	if(l==r) {
		a[x]=arr[l];
		b[x]=arr[l];
		return ;
	}
	int mid=(l+r)>>1;
	build(l,mid,x<<1);
	build(mid+1,r,x<<1|1);
	push_up(x);
}
int query1(int l,int r,int x,int L,int R) {
	int ans=inf;
	if(L<=l&&r<=R) {
		return a[x];
	}
	int mid=(l+r)>>1;
	if(mid<L) {
		ans=min(ans,query1(mid+1,r,x<<1|1,L,R));
	}
	else if(mid>=R) {
		ans=min(ans,query1(l,mid,x<<1,L,R));
	}
	else {
		ans=min(ans,query1(mid+1,r,x<<1|1,L,R));
		ans=min(ans,query1(l,mid,x<<1,L,R));
	}
	return ans;
}
int query2(int l,int r,int x,int L,int R) {
	int ans=-inf;
	if(L<=l&&r<=R) {
		return b[x];
	}
	int mid=(l+r)>>1;
	if(mid<L) {
		ans=max(ans,query2(mid+1,r,x<<1|1,L,R));
	}
	else if(mid>=R) {
		ans=max(ans,query2(l,mid,x<<1,L,R));
	}
	else {
		ans=max(ans,query2(mid+1,r,x<<1|1,L,R));
		ans=max(ans,query2(l,mid,x<<1,L,R));
	}
	return ans;
}
int m1[1001000],m2[1001000];
int main() 
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    #endif
    //freopen("out.txt", "w", stdout);
    //ios::sync_with_stdio(0),cin.tie(0);
    int n,k;
    while(scanf("%d %d",&n,&k)!=EOF) {
        rep(i,1,n) scanf("%d",&arr[i]);
        build(1,n,1);
        rep(i,1,n-k+1) {
        	m1[i]=query1(1,n,1,i,i+k-1);
        	m2[i]=query2(1,n,1,i,i+k-1);
        }
        rep(i,1,n-k) {
        	printf("%d ",m1[i]);
        }
        printf("%d\n",m1[n-k+1]);
        rep(i,1,n-k) {
        	printf("%d ",m2[i]);
        }
        printf("%d\n",m2[n-k+1]);
    }
    return 0;
}

思路二:单调队列( O ( n ) O(n) O(n)
先建立一个双端队列,对于查询最小值,队列为单调递增的,每次将元素放入队列,先将其与双端队列的队尾元素进行比较,如果队尾元素大于或等于该元素,则将队尾元素弹出,最后插入该元素以保证双端队列的单调性。还需比较队首元素与队尾元素下标的差值与 k k k的大小关系,如果差值大于等于 k k k则证明队首元素已过期,须将队首元素弹出,以此来维护最小值。
最大值以相同的方法维护。
代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
deque<pair<int,int>> qumin;
deque<pair<int,int>> qumax;
pair<int,int> arr[1001000];
int arrmin[1001000],arrmax[1001000];
int main() 
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    #endif
    //freopen("out.txt", "w", stdout);
    //ios::sync_with_stdio(0),cin.tie(0);
    int n,k;
    while(scanf("%d %d",&n,&k)!=EOF) {
    	rep(i,1,n) {
    		scanf("%d",&arr[i].second);
    		arr[i].first=i;
    	}
    	while(!qumin.empty()) {
    		qumin.pop_back();
    	}
    	while(!qumax.empty()) {
    		qumax.pop_back();
    	}
        int cnt=0;
    	for(int id=1;id<=n;id++) {
    		while(!qumin.empty()&&arr[id].second<=qumin.back().second) {
    			qumin.pop_back();
    		}
    		qumin.push_back(arr[id]);
    		if(id-qumin.front().first>=k) {
    			qumin.pop_front();
    		}
    		if(id>=k) {
    			arrmin[++cnt]=qumin.front().second;
    		}
    	}
        cnt=0;
    	for(int id=1;id<=n;id++) {
    		while(!qumax.empty()&&arr[id].second>=qumax.back().second) {
    			qumax.pop_back();
    		}
    		qumax.push_back(arr[id]);
    		if(id-qumax.front().first>=k) {
    			qumax.pop_front();
    		}
    		if(id>=k) {
    			arrmax[++cnt]=qumax.front().second;
    		}
    	}
    	rep(i,1,n-k) {
    		printf("%d ",arrmin[i]);
    	}
        printf("%d\n",arrmin[n-k+1]);
    	rep(i,1,n-k) {
    		printf("%d ",arrmax[i]);
    	}
        printf("%d\n",arrmax[n-k+1]);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值