• POJ 2823 Sliding Window


    Sliding Window
    Time Limit: 12000MS   Memory Limit: 65536K
    Total Submissions: 26178   Accepted: 7738
    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 kis 3.
    Window positionMinimum valueMaximum value
    [1  3  -1] -3  5  3  6  7  -1 3
     1 [3  -1  -3] 5  3  6  7  -3 3
     1  3 [-1  -3  5] 3  6  7  -3 5
     1  3  -1 [-3  5  3] 6  7  -3 5
     1  3  -1  -3 [5  3  6] 7  3 6
     1  3  -1  -3  5 [3  6  7] 3 7

    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

    //开始有线段树做 9188MS
    //后面改用刘老师的基础数据结构里面介绍的用单调队列做 5313MS
    //不过用线段树写的不止简洁一丁点、
    //也有可能是偶代码技术还不好
     
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <queue>
    #include <cmath>
    #define N 1000004
    #define lson l,m,k<<1
    #define rson m+1,r,k<<1|1
    using namespace std;
    struct node
    {
        int min,max;
    };
    node st[N<<2];
    void build(int l,int r,int k)
    {
        if(l==r)
        {
          scanf("%d",&st[k].min);
          st[k].max=st[k].min;
          return ;
        }
        int m=(l+r)>>1;
        build(lson);
        build(rson);
        st[k].max=max(st[k<<1].max,st[k<<1|1].max);
        st[k].min=min(st[k<<1].min,st[k<<1|1].min);
    }
    int quMin(int &L,int &R,int l,int r,int k)
    {
        if(L<=l&&R>=r)
        {
            return st[k].min;
        }
        int m=(l+r)>>1;
        int t1=1000000000,t2=1000000000;
        if(L<=m) t1=quMin(L,R,lson);
        if(R>m)  t2=quMin(L,R,rson);
        return t1<t2?t1:t2;
    }
    int quMax(int &L,int &R,int l,int r,int k)
    {
        if(L<=l&&R>=r)
        {
            return st[k].max;
        }
        int m=(l+r)>>1;
        int t1=-1000000000,t2=-1000000000;
        if(L<=m) t1=quMax(L,R,lson);
        if(R>m)  t2=quMax(L,R,rson);
        return t1>t2?t1:t2;
    }
    int main()
    {
        int i,k;
        int n,m,r;
        while(scanf("%d%d",&n,&k)!=EOF)
        {
            build(1,n,1);
            m=n-k;
            for(i=1;i<=m;i++)
             r=i+k-1,printf("%d ",quMin(i,r,1,n,1));
            r=m+1;
            printf("%d\n",quMin(r,n,1,n,1));
    
            for(i=1;i<=m;i++)
             r=i+k-1,printf("%d ",quMax(i,r,1,n,1));
            r=m+1;
            printf("%d\n",quMax(r,n,1,n,1));
        }
        return 0;
    }


    #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <queue> #include <cmath> #define N 1000004 using namespace std; struct node { int id,val; }; node M[N]; int front,back; int a[N]; int main() { int n,k,i; while(scanf("%d%d",&n,&k)!=EOF) { front=back=1; scanf("%d",&a[1]); M[front].val=a[1];M[front].id=1; for(i=2;i<=k;i++) { scanf("%d",&a[i]); if(a[i]<=M[back].val) { while(1) { M[back].val=a[i],M[back].id=i; if(back>front&&a[i]<=M[back-1].val) back--; else break; } } else { M[++back].val=a[i]; M[back].id=i; } } printf("%d",M[front].val); for(i=k+1;i<=n;i++) { scanf("%d",&a[i]); if(a[i]<=M[back].val) { while(1) { M[back].val=a[i],M[back].id=i; if(back>front&&a[i]<=M[back-1].val) back--; else break; } } else { M[++back].val=a[i]; M[back].id=i; } while(M[front].id<i-k+1) front++; printf(" %d",M[front].val); } printf("\n"); front=back=1; M[front].val=a[1];M[front].id=1; for(i=2;i<=k;i++) { if(a[i]>=M[back].val) { while(1) { M[back].val=a[i],M[back].id=i; if(back>front&&a[i]>=M[back-1].val) back--; else break; } } else { M[++back].val=a[i]; M[back].id=i; } } printf("%d",M[front].val); for(i=k+1;i<=n;i++) { if(a[i]>=M[back].val) { while(1) { M[back].val=a[i],M[back].id=i; if(back>front&&a[i]>=M[back-1].val) back--; else break; } } else { M[++back].val=a[i]; M[back].id=i; } while(M[front].id<i-k+1) front++; printf(" %d",M[front].val); } printf("\n"); } return 0; }
  • 相关阅读:
    利用matlab给图像加高斯噪声
    频谱分析代码片段2
    相关性分析代码片段2
    相关性分析代码片段
    频谱分析代码片段
    大脑提取每一个体素26领域的matlab代码
    当前所看论文记录
    论文阅读笔记
    余弦距离、欧氏距离和杰卡德相似性度量的对比分析 by ChaoSimple
    Container With Most Water
  • 原文地址:https://www.cnblogs.com/372465774y/p/2625695.html
Copyright © 2020-2023  润新知