NC51001 Sliding Window 滑动窗口最大最小值问题 堆优化

链接:https://ac.nowcoder.com/acm/problem/51001
来源:牛客网

题目描述
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
Your task is to determine the maximum and minimum values in the sliding window at each position.

输入描述:

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.

输出描述:

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.

示例1
输入
复制

8 3
1 3 -1 -3 5 3 6 7

输出
复制

-1 -3 -3 -3 3 3
3 3 5 5 6 7

经典问题,滑动窗口的最值问题,

  • 堆优化 O ( n l o g n ) O(nlogn) O(nlogn)
  • 结构体保存数字的下标和数字本身
struct Node {
	int id, val; //下标和值
	bool operator < (const Node& no) const {
		return val < no.val;
	}
	bool operator > (const Node& no) const {
		return val > no.val;
	}
} ;
  • 用两个堆来模拟窗口(一个大根堆,一个小根堆)
std::priority_queue<Node> q1; //大根堆
std::priority_queue<Node, vector<Node>, greater<Node> > q2;
  • 初始化窗口内有 M 1 M-1 M1个元素
for(int i=1; i<m; i++) //初始化窗口里有m-1个元素
   	q1.push({i, a[i]}), q2.push({i, a[i]});
  • 每次加入一个元素 a [ i ] a[i] a[i],就把堆内不在窗口区间 [ i M + 1 , i ] [i-M+1,i] [iM+1,i]内的弹出
for(int i=m; i<=n; i++) {
	q2.push({i, a[i]}); //每次加入一个元素
	while(tb.id <= i-m) q2.pop(); //并把所有下标小于窗口左端的值弹走
	printf("%d%c", tb.val, i==n?'\n':' '); //此时堆顶一定是最小值
}

完整代码如下

#define debug
#ifdef debug
#include <time.h>
#include "/home/majiao/mb.h"
#endif

#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>

#define MAXN ((int)1e6+7)
#define ll long long int
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)

using namespace std;

#ifdef debug
#define show(x...) \ do { \ cout << "\033[31;1m " << #x << " -> "; \ err(x); \ } while (0)

void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }
#endif

#ifndef debug
namespace FIO {
	template <typename T>
	void read(T& x) {
		int f = 1; x = 0;
		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(); }
		x *= f;
	}
};
using namespace FIO;
#endif

#define ta (q1.top())
#define tb (q2.top())

int n, m, Q, K, a[MAXN];
struct Node {
	int id, val;
	bool operator < (const Node& no) const {
		return val < no.val;
	}
	bool operator > (const Node& no) const {
		return val > no.val;
	}
} ;

std::priority_queue<Node> q1; //大根堆
std::priority_queue<Node, vector<Node>, greater<Node> > q2;

int main() {
#ifdef debug
	freopen("test", "r", stdin);
	clock_t stime = clock();
#endif
	read(n), read(m);
	for(int i=1; i<=n; i++) read(a[i]);
	for(int i=1; i<m; i++) //初始化窗口里有m-1个元素
		q1.push({i, a[i]}), q2.push({i, a[i]});

	for(int i=m; i<=n; i++) {
		q2.push({i, a[i]}); //每次加入一个元素
		while(tb.id <= i-m) q2.pop(); //并把所有下标小于窗口左端的值弹走
		printf("%d%c", tb.val, i==n?'\n':' '); //此时堆顶一定是最小值
	}
	for(int i=m; i<=n; i++) {
		q1.push({i, a[i]});
		while(ta.id <= i-m) q1.pop();
		printf("%d%c", ta.val, i==n?'\n':' ');
	}





#ifdef debug
	clock_t etime = clock();
	printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif 
	return 0;
}




全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 听劝,这个简历怎么改 #
14086次浏览 182人参与
# 面试被问“你的缺点是什么?”怎么答 #
6359次浏览 98人参与
# 水滴春招 #
16405次浏览 346人参与
# 入职第四天,心情怎么样 #
11310次浏览 63人参与
# 租房找室友 #
8021次浏览 53人参与
# 读研or工作,哪个性价比更高? #
26152次浏览 356人参与
# 职场新人生存指南 #
199211次浏览 5509人参与
# 参加完秋招的机械人,还参加春招吗? #
26977次浏览 276人参与
# 文科生还参加今年的春招吗 #
4114次浏览 31人参与
# 简历无回复,你会继续海投还是优化再投? #
48624次浏览 561人参与
# 你见过最离谱的招聘要求是什么? #
144719次浏览 829人参与
# 如果重来一次你还会读研吗 #
155716次浏览 1706人参与
# 机械人选offer,最看重什么? #
69077次浏览 449人参与
# 选择和努力,哪个更重要? #
44292次浏览 493人参与
# 如果再来一次,你还会学硬件吗 #
103645次浏览 1245人参与
# 如果你有一天可以担任公司的CEO,你会做哪三件事? #
20520次浏览 413人参与
# 招聘要求与实际实习内容不符怎么办 #
46727次浏览 494人参与
# 22届毕业,是读研还是拿外包offer先苟着 #
4652次浏览 27人参与
# 你们的毕业论文什么进度了 #
901248次浏览 8961人参与
# 软开人,你觉得应届生多少薪资才算合理? #
81375次浏览 496人参与
# 国企还是互联网,你怎么选? #
109191次浏览 853人参与
牛客网
牛客企业服务