POJ2823_Sliding Window_优先队列||线段树

Sliding Window
Time Limit: 12000MS Memory Limit: 65536K
Total Submissions: 58278 Accepted: 16702
Case Time Limit: 5000MS

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个元素的数组,一个整数m。相当于有一个长度为m的框,通过这个框,每次只能看到数组中连续的m个数。现在这个框从数组的最左边开始一格一格地往右移动,要求输出每个状态上框中数字的最大值和最小值。


大体思路:

一开始是用优先队列做的。

后来直觉上觉得线段树可能效率更高一点,就又写了一版,结果发现被自己坑了。。。


优先队列版:

记忆中两个优先队列的名字max和min可能是写反了,懒得检查和改正了。。。


#include<cstdio>
#include<queue>

struct node
{
	int v,p;
};

struct __min
{
	bool operator () (node x,node y) {
		return x.v<y.v;
	}
};

struct __max
{
	bool operator () (node x,node y){
		return x.v>y.v;
	}
};

int n,k,tt;
node t;
int Max [1000100];
int Min [1000100];
std::priority_queue<node,std::vector<node>,__min>_min;
std::priority_queue<node,std::vector<node>,__max>_max;

int main ()
{
	//freopen("in.txt","r",stdin);

	scanf("%d%d",&n,&k);
	int i=0,l=0;

	for(;i<k;i++){
		scanf("%d",&tt);
		t.v=tt,t.p=i;
		_min.push(t);
		_max.push(t);
	}
	Max[l]=_max.top().v;
	Min[l]=_min.top().v;
	l++;

	for(;i<n;i++){

		scanf("%d",&tt);
		t.v=tt,t.p=i;
		_max.push(t);
		_min.push(t);

		while(_max.top().p<=i-k) _max.pop();
		Max[l]=_max.top().v;
		while(_min.top().p<=i-k) _min.pop();
		Min[l]=_min.top().v;
		l++;

	}

	printf("%d",Max[0]);
	for(i=1;i<l;i++)
		printf(" %d",Max[i]);
	printf("\n");
	printf("%d",Min[0]);
	for(i=1;i<l;i++)
		printf(" %d",Min[i]);

	return 0;
}


线段树版:


#include<cstdio>

#define limit 2147483646
#define Max(x,y) x>y ? x : y
#define Min(x,y) x<y ? x : y

struct win{
	int min, max;
	};

win s [4*1000010];
win a [1000010];
int n, m;

void Buildtree(int p, int l, int r)
{
	if(l==r){
		scanf("%d",&s[p].min);
		s[p].max = s[p].min;
		return;
	}

	int mid = ( l + r ) >> 1;
	Buildtree(2*p, l, mid);
	Buildtree(2*p+1, mid+1, r);
	s[p].max = Max(s[2*p].max,s[2*p+1].max);
	s[p].min = Min(s[2*p].min,s[2*p+1].min);
}

win Find (int p, int l, int r, int x, int y)
{
	if(l>=x && r<=y){
		return s[p];
	}

	int mid = ( l + r ) >> 1;
	win d;
	d.max = -limit, d.min = limit;

	if(x <= mid) d = Find(2*p, l, mid, x, y);
	if(y >mid){
		win q = Find(2*p+1, mid+1, r, x, y);
		if(d.max < q.max) d.max = q.max;
		if(d.min > q.min) d.min = q.min;
	}

	return d;
}

int main ()
{
	scanf("%d%d", &n, &m);

	Buildtree(1, 1, n);

	int k = 0;
	for(int i=1; i+m-1<=n; i++){
		win t = Find(1, 1, n, i, i+m-1);
		a[k].min = t.min, a[k].max = t.max, k++;
	}

	printf("%d",a[0].min);
	for(int i=1; i<k; i++)
		printf(" %d",a[i].min);
	printf("\n");
	printf("%d",a[0].max);
	for(int i=1; i<k; i++)
		printf(" %d",a[i].max);
	printf("\n");

	return 0;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值