poj2823(线段树详解)求区间最大(小)值

Sliding Window
Time Limit: 12000MS Memory Limit: 65536K
Total Submissions: 58321 Accepted: 16711
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

Source

POJ Monthly--2006.04.28, Ikki


传送门:poj 2823


思路:这一题我刚开始认为有专门的奇淫巧技,我还想了好久,但是看了网上的题解之后,我发现一般就是两种做法,一种是:优先队列(这个另外发一个blog),还有一个就是直接用线段树维护,最大值跟最小值,就是非常容易超时,我优化了好久,但是后来看到网上有大神说用不需要优化的线段树就可以ac,难道是当初我学线段树的模板优化的不够好吗?向大佬低头....剪枝优化果然很重要啊!

Node  tree[MAXN]存放线段树,Node包含两个int类型的成员变量:mins-最小值,maxs-最大值,初始化时,将叶子节点的最大值跟最小值都赋值为相同的值,并不断更新区间的最大值与最小值,由于要求的输出比较奇怪,是最大值占一行,最小值占一行,所以另外设置两个数组存放区间段的最大值跟最小值,详细见代码:

/*
 *Li Wenjun
 *Email:1542113545@qq.com
 */
#include<stdio.h>
#include<string.h>
#include<math.h>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <cctype>
#include <stack>
#include <list>
#include <cstdlib>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define mid (l+r)>>1
#define lson l,m,rt<<1
#define rson m+1,r,(rt<<1)|1

const int MAXN = 1000005;

struct Node{
    int maxs;
    int mins;
}tree[MAXN << 2];
int n,k;
int mins[MAXN],maxs[MAXN];

int push_up(int rt)
{
    tree[rt].mins = min(tree[rt<<1].mins,tree[(rt<<1)|1].mins);
    tree[rt].maxs = max(tree[rt<<1].maxs,tree[(rt<<1)|1].maxs);
    return 0;
}

int build(int l,int r,int rt)
{
    if(l==r)
    {
        scanf("%d",&tree[rt].mins);
        tree[rt].maxs = tree[rt].mins;
        return 0;
    }
    int m = mid;
    build(lson);
    build(rson);
    push_up(rt);

    return 0;
}

void query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        mins[L] = min(mins[L],tree[rt].mins);
        maxs[L] = max(maxs[L],tree[rt].maxs);
        return ;
    }

    int m = mid;

    if(L<=m)  query(L,R,lson);
    if(m<R)   query(L,R,rson);

    return ;
}


int main()
{

    //freopen("in.txt", "r", stdin);
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        memset(mins,INF,sizeof(mins));
        memset(maxs,-INF,sizeof(maxs));
        build(1,n,1);
        for(int i=1;i<=n-k+1;i++)
        {
            query(i,i+k-1,1,n,1) ;
        }
        for(int i=1;i<=n-k+1;i++)
        {
            printf("%d ",mins[i]);
        }
        printf("\n");
        for(int i=1;i<=n-k+1;i++)
        {
            printf("%d ",maxs[i]);
        }
        printf("\n");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值