您的位置:首页 > 其它

poj 2823 线段树 求固定区间的最大最小值

2010-04-20 21:02 483 查看
Sliding Window

Time Limit: 12000MSMemory Limit: 65536K
Total Submissions: 10791Accepted: 2934
Case Time Limit: 5000MS
Description
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 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

Source
POJ Monthly--2006.04.28, Ikki
#include <iostream>
#include <string>
using namespace std;
class cNode {
public:int l,r;
int root;
int min,max;
}Node[5000001];
int data[1000011];
void build(int l,int r,int root)
{
Node[root].l = l;
Node[root].r = r;
if(l == r)
{
Node[root].min=Node[root].max=data[l];
return;
}
int mid=(l+r)>>1;
build(l,mid,root*2);
build(mid+1,r,root*2+1);
Node[root].min = min(Node[root*2].min,Node[root*2+1].min);
Node[root].max = max(Node[root*2].max,Node[root*2+1].max);
}
int minl[1000011],maxl[1000011];
void query(int l,int r,int& min,int& max,int root)
{
if(Node[root].l == l && Node[root].r == r)
{
min = Node[root].min;
max = Node[root].max;
return ;
}
int mid = (Node[root].l + Node[root].r)>>1;
if(mid >= r)
query(l,r,min,max,root*2);
else if(mid<l)
query(l,r,min,max,root*2+1);
else
{
int max2,min2;
query(l,mid,min,max,root*2);
query(mid+1,r,min2,max2,root*2+1);
min=min<min2?min:min2;
max=max>max2?max:max2;
}
}
int main()
{
int k,len;
while(scanf("%d%d",&k,&len)!=EOF)
{
for(int i=1;i<=k;i++)
scanf("%d",data+i);
build(1,k,1);
int maxt,mint;
for(int i=1;i<=k-len+1;i++)
{
query(i,i+len-1,mint,maxt,1);
minl[i]=mint;
maxl[i]=maxt;
}
for(int i=1;i<=k-len+1;i++)
printf("%d ",minl[i]);
cout<<endl;
for(int i=1;i<=k-len+1;i++)
printf("%d ",maxl[i]);
cout<<endl;
}
}

也可以使用优先队列的方式
#include <queue>
#include <iostream>
using namespace std;
int a[1000003];
int outMin[1000003];
int outMax[1000003];
struct cmp{
bool   operator() (const int l,const int r)
{
return a[l]>a[r];
}
};
struct cmp2{
bool operator() (const int l,const int r)
{
return a[l]<a[r];
}
};
priority_queue<int,vector<int>,cmp>f1;
priority_queue<int,vector<int>,cmp2>f2;
int main()
{
int n,k;
while(scanf("%d%d",&n,&k)!=EOF)
{
for(int i=1;i<=n;i++)
scanf("%d",a+i);
for(int i=1;i<=k;i++)
{
f1.push(i);
f2.push(i);
}
int cnt1=0;
int cnt2=0;
outMin[cnt1++]=a[f1.top()];
outMax[cnt2++]=a[f2.top()];
for(int i=k+1;i<=n;i++)
{
f1.push(i);
f2.push(i);
while(i-f1.top()>=k)
f1.pop();
outMin[cnt1++]=a[f1.top()];
while(i-f2.top()>=k)
f2.pop();
outMax[cnt2++]=a[f2.top()];
}
for(int i=0;i<=n-k;i++)
printf("%d ",outMin[i]);
cout<<endl;
for(int i=0;i<=n-k;i++)
printf("%d ",outMax[i]);
cout<<endl;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: