zoukankan      html  css  js  c++  java
  • 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; }
  • 相关阅读:
    [转载] python 计算字符串长度
    收藏好文章
    centos7安装部署kafka_2.13-2.4.1集群
    (转)zookeeper-3.5.5安装报错:找不到或无法加载主类 org.apache.zookeeper.server.quorum.QuorumPeerMain
    (转)Marathon私有镜像仓库用户名和密码方式
    centos7安装最新版git
    最新版Harbor搭建(harbor-offline-installer-v1.10.1.tgz)(转)
    安装不可描述服务端socket.error: [Errno 99] Cannot assign requested address错误:
    Integer值判断是否相等问题
    LVS实现Kubernetes集群高可用
  • 原文地址:https://www.cnblogs.com/372465774y/p/2625695.html
Copyright © 2011-2022 走看看