Sliding Window

本文介绍了一种解决滑动窗口最大最小值问题的方法,通过构建树状结构来高效更新和查询滑动窗口内的最大值和最小值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B - Sliding Window
Time Limit:12000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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  nintegers 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
 
   
 
   
 
   
AC代码:
 
   
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
#define T 1000100*50
int L=1;
struct node
{
	int ma,mi;
	int sta,end;
	void newnode(int sta=0,int end=0)
	{
		this->sta = sta;
		this->end = end;
		L = ma = mi =0;
	}
}tree[T];
void build(int sta,int end,int L)
{
	if(::L<L)::L=L;
	tree[L].newnode(sta,end);
	if(sta!=end)
	{
		int mid = (sta+end)>>1;
		build(sta,mid,L*2);
		build(mid+1,end,L*2+1);
	}
}
void update(int t,int pos,int val)
{
	if(tree[t].sta==tree[t].end)
	{
		tree[t].mi = val;
		tree[t].ma = val;
		return;
	}
	if(pos<=tree[t*2].end)
	{
		update(t*2,pos,val);
	}
	else
	{
		update(t*2+1,pos,val);
	}
	tree[t].ma = max(tree[t*2].ma,tree[t*2+1].ma);
	tree[t].mi = min(tree[t*2].mi,tree[t*2+1].mi);
}
int query(int t,int sta,int end,int f)
{
	if(tree[t].sta==sta&&tree[t].end==end)
	{
		if(f) return tree[t].ma;
		return tree[t].mi;
	}
	if(end<=tree[t*2].end)
		return query(t*2,sta,end,f);
	else if(sta>= tree[t*2+1].sta)
		return query(t*2+1,sta,end,f);
	else
	{
		int left = query(t*2,sta,tree[t*2].end,f);
		int right = query(t*2+1,tree[t*2+1].sta,end,f);
		if(f) return max(left,right);
		return min(left,right);
	}
}
int main()
{
	/*freopen("input.txt","r",stdin);*/
	int n,m,i,j,k;
	scanf("%d%d",&n,&m);
	build(1,n,1);
	for(i=1;i<=n;++i)
	{
		scanf("%d",&k);
		update(1,i,k);
	}
	for(j=0;j<=1;++j)
	{
		for(i=1;i<=n-m+1;++i)
			printf("%d ",query(1,i,i+m-1,j));
		printf("\n");
	}
	return 0;
}


MLE代码:
 
   
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
#define T 1000100
struct node
{
	int ma,mi;
	int sta,end;
	node* L,*R;
	node(int sta=0,int end=0)
	{
		this->sta = sta;
		this->end = end;
		ma = mi =0;
		L = R = NULL;
	}
	~node()
	{
		if(L)
			delete L;
		if(R)
			delete R;
		L = R = NULL;
	}
}*tree;
node* build(int sta,int end)
{
	node* t = new node(sta,end);
	if(sta!=end)
	{
		int mid = (sta+end)>>1;
		t->L = build(sta,mid);
		t->R = build(mid+1,end);
	}
	return t;
}
void update(node* t,int pos,int val)
{
	if(t->sta==t->end)
	{
		t->mi = val;
		t->ma = val;
		return;
	}
	if(pos<=t->L->end)
	{
		update(t->L,pos,val);
	}
	else
	{
		update(t->R,pos,val);
	}
	t->ma = max(t->L->ma,t->R->ma);
	t->mi = min(t->L->mi,t->R->mi);
}
int query(node* t,int sta,int end,int f)
{
	if(t->sta==sta&&t->end==end)
	{
		if(f) return t->ma;
		return t->mi;
	}
	if(end<=t->L->end)
		return query(t->L,sta,end,f);
	else if(sta>= t->R->sta)
		return query(t->R,sta,end,f);
	else
	{
		int left = query(t->L,sta,t->L->end,f);
		int right = query(t->R,t->R->sta,end,f);
		if(f) return max(left,right);
		return min(left,right);
	}
}
int main()
{
	/*freopen("input.txt","r",stdin);*/
	int n,m,i,j,k;
	scanf("%d%d",&n,&m);
	tree = build(1,n);
	for(i=1;i<=n;++i)
	{
		scanf("%d",&k);
		update(tree,i,k);
	}
	for(j=0;j<=1;++j)
	{
		for(i=1;i<=n-m+1;++i)
			printf("%d ",query(tree,i,i+m-1,j));
		printf("\n");
	}
	delete tree;
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值