POJ_2823_Sliding Window(RMQ / 单调队列)

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 positionMinimum valueMaximum value
[1  3  -1] -3  5  3  6  7 -13
 1 [3  -1  -3] 5  3  6  7 -33
 1  3 [-1  -3  5] 3  6  7 -35
 1  3  -1 [-3  5  3] 6  7 -35
 1  3  -1  -3 [5  3  6] 7 36
 1  3  -1  -3  5 [3  6  7]37

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


题意:有n个数,每次从左到右选取k个数,第一行选取每个区间中的最小值输出,第二行每次选取区间中的最大值输出。

分析:RMQ或单调队列求解。

RMQ:题目数据量比较大,如果用一般的二维数组显然是要MLE的,由于此题是固定的区间长度,所以只要用一维数组即可,第二维可以省略。

单调队列:数组模拟队列,用deque会超时。

题目链接:http://poj.org/problem?id=2823

代码清单:

(1) RMQ

#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;
}


(2)单调队列

#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <ctime>
#include <vector>
#include <cctype>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;

#define exit() return 0

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;

const int maxn = 1e6 + 5;

struct Deque {
	int data;
	int idx;
	Deque() {}
	Deque(int _data, int _idx) : data(_data), idx(_idx) {}
};

Deque minq[maxn];
Deque maxq[maxn];
int head1, tail1;
int head2, tail2;

int n, k;
int a[maxn];
int minans[maxn];
int maxans[maxn];

void init() {
	head1 = tail1 = 0;
	head2 = tail2 = 0;
}

void input() {
	scanf("%d%d", &n, &k);
	for(int i = 0; i < n; i++) scanf("%d", &a[i]);
}

void work() {
	for(int i = 0; i < n; i++) {
		while(head1 < tail1 && minq[tail1 - 1].data > a[i]) tail1--;
		minq[tail1++] = Deque(a[i], i);
		while(head1 < tail1 && minq[head1].idx <= i - k) head1++;
		if(i >= k - 1) minans[i] = minq[head1].data;

		while(head2 < tail2 && maxq[tail2 - 1].data < a[i]) tail2--;
		maxq[tail2++] = Deque(a[i], i);
		while(head2 < tail2 && maxq[head2].idx <= i - k) head2++;
		if(i >= k - 1) maxans[i] = maxq[head2].data;
	}
}

void print() {
	for(int i = k - 1; i < n; i++) {
		if(i >= k) printf(" ");
		printf("%d", minans[i]);
	}
	printf("\n");

	for(int i = k - 1; i < n; i++) {
		if(i >= k) printf(" ");
		printf("%d", maxans[i]);
	}
	printf("\n");
}

void solve() {
	work();
	print();
}

int main() {
	init();
	input();
	solve();
	exit();
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值