您的位置:首页 > 其它

poj 2823 Sliding Window (线段树 求固定区间的最大最小值 )

2015-08-17 13:27 393 查看
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

分析:题意很清楚,窗口向前滑动,每次求窗口内的最大值与最小值,这题应该可以用RMQ,线段树,单调区间来做,现在先给出先用线段树来做,以后再补充吧

用线段树做,因为要最大值一行,最小值一行,我们可以设置两个数组,minn[],maxn[], 建一棵树就可以,查询的时候可以有两种方法;

法一:每次遍历一棵树,直到找到所查询的区间,直接修改最值;这样比较快,时间6547(要用c++提交,似乎c++与G++不是一个编译器,G++较慢)

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAX=1000010;
const int INF=0x3f3f3f3f;
int maxn[MAX],minn[MAX];
int maxm,minm;
struct node
{
    int ll,rr,minval,maxval;
    int getmid(){return (ll+rr)>>1;}
}tree[MAX*4];
int num[MAX];
void build(int ll,int rr,int rt)
{
    tree[rt].ll=ll;
    tree[rt].rr=rr;
    int mid=tree[rt].getmid();
    if(ll==rr){
        tree[rt].minval=tree[rt].maxval=num[rr];
        return;
    }
    build(ll,mid,rt<<1);
    build(mid+1,rr,rt<<1|1);
    tree[rt].minval=min(tree[rt<<1].minval,tree[rt<<1|1].minval);
    tree[rt].maxval=max(tree[rt<<1].maxval,tree[rt<<1|1].maxval);
}
void query(int ll,int rr,int rt)
{
    //if(tree[rt].maxval<=maxm&&tree[rt].minval>=minm)return;
    if(tree[rt].ll==ll&&tree[rt].rr==rr){
        minm=min(tree[rt].minval,minm);
        maxm=max(tree[rt].maxval,maxm);
        return;
    }
    int mid=tree[rt].getmid();
    if(rr<=mid) query(ll,rr,rt<<1);
    else if(mid<ll)  query(ll,rr,rt<<1|1);
    else{
        query(ll,mid,rt<<1);
        query(mid+1,rr,rt<<1|1);
    }
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",&num[i]);
    build(1,n,1);

    for(int i=1;i<=n-m+1;i++){
        int j=i+m-1;
        maxm=-INF;
        minm=INF;
        query(i,j,1);
        minn[i]=minm;
        maxn[i]=maxm;
    }
    for(int i=1;i<n-m+1;i++)
        printf("%d ",minn[i]);
    printf("%d\n",minn[n-m+1]);
    for(int i=1;i<n-m+1;i++)
        printf("%d ",maxn[i]);
    printf("%d\n",maxn[n-m+1]);
}


法二:分别查询最大值和最小值,将值传给数组,因为查询差不多比法一多了一倍,时间有点慢,8516ms

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAX=1000010;
int maxn[MAX],minn[MAX];
struct node
{
    int ll,rr,minval,maxval;
    int getmid(){return (ll+rr)>>1;}
}tree[MAX*4];
int num[MAX];
void build(int ll,int rr,int rt)
{
    tree[rt].ll=ll;
    tree[rt].rr=rr;
    int mid=tree[rt].getmid();
    if(ll==rr){
        tree[rt].minval=tree[rt].maxval=num[rr];
        return;
    }
    build(ll,mid,rt<<1);
    build(mid+1,rr,rt<<1|1);
    tree[rt].minval=min(tree[rt<<1].minval,tree[rt<<1|1].minval);
    tree[rt].maxval=max(tree[rt<<1].maxval,tree[rt<<1|1].maxval);
}
int query(int ll,int rr,int rt,bool flag)
{
    //if(tree[i].)
    if(tree[rt].ll==ll&&tree[rt].rr==rr){
        if(flag)
            return tree[rt].minval;
        else return tree[rt].maxval;
    }
    int mid=tree[rt].getmid();
    if(rr<=mid) return query(ll,rr,rt<<1,flag);
    else if(mid<ll) return query(ll,rr,rt<<1|1,flag);
    else{
        //query(ll,mid,rt<<1,flag);
       // query(mid+1,rr,rt<<1|1,flag);
       if(flag)return min(query(ll,mid,rt<<1,flag),query(mid+1,rr,rt*2+1,flag));
       else return max(query(ll,mid,rt<<1,flag),query(mid+1,rr,rt*2+1,flag));
    }
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",&num[i]);
    build(1,n,1);
    int maxm,minm;
    for(int i=1;i<=n-m+1;i++){
        int j=i+m-1;
        minn[i]=query(i,j,1,1);
        maxn[i]=query(i,j,1,0);
    }
    for(int i=1;i<n-m+1;i++)
        printf("%d ",minn[i]);
    printf("%d\n",minn[n-m+1]);
    for(int i=1;i<n-m+1;i++)
        printf("%d ",maxn[i]);
    printf("%d\n",maxn[n-m+1]);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: