• Sliding Window POJ


    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 position Minimum value Maximum 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

    题意:找出每组连续的k个数的最大值最小值。

    可以用线段树,也可以优先队列。

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<map>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<set>
    #include<cmath>
    #define ll long long
    #define mem(a,b) memset(a,b,sizeof(a))
    using namespace std;
    const int MAXN=1000000+10;
    const int inf=0x3f3f3f;
    int n,m;
    struct node1
    {
        ll x;
        int y;
        friend bool operator < (node1 a,node1 b)
        {
            return a.x>b.x;
        }
    }z1;
    struct node2
    {
        ll x;
        int y;
        friend bool operator<(node2 a,node2 b)
        {
            return a.x<b.x;
        }
    }z2;
    priority_queue<node1>q1;
    priority_queue<node2>q2;
    ll z[MAXN];
    ll minn[MAXN];
    ll maxn[MAXN];
    int main()
    {
            scanf("%d%d",&n,&m);
            while(!q1.empty()) q1.pop();
            while(!q2.empty()) q2.pop();
            for(int i=1;i<=n;i++)
            {
                scanf("%lld",&z[i]);
                z1.y=z2.y=i;
                z2.x=z1.x=z[i];
                if(i<=m)
                {
                    q1.push(z1);
                    q2.push(z2);
                }
            }
            int ans=0;
            minn[ans]=q1.top().x;
            maxn[ans++]=q2.top().x;
            int left=2;
            for(int i=m+1;i<=n;i++)
            {
                z1.x=z[i];z1.y=i;
                z2.x=z[i];z2.y=i;
                q1.push(z1);
                q2.push(z2);
                while(q1.top().y<left)
                {
                    q1.pop();
                }
                while(q2.top().y<left)
                {
                    q2.pop();
                }
                left++;
                minn[ans]=q1.top().x;
                maxn[ans++]=q2.top().x;
            }
            printf("%lld",minn[0]);
            for(int i=1;i<ans;i++) printf(" %lld",minn[i]);
            printf("
    ");
            printf("%lld",maxn[0]);
            for(int i=1;i<ans;i++) printf(" %lld",maxn[i]);
            printf("
    ");
    }

    线段树

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<map>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<set>
    #include<cmath>
    #define ll long long
    #define mem(a,b) memset(a,b,sizeof(a))
    #define inf 0x3f3f3f3f
    #define maxn 1000009
    #define root  1,n,1
    #define lson  l,mid,rt<<1
    #define rson  mid+1,r,rt<<1|1
    using namespace std;
    struct node{
        int l,r,rt;
        int mid(){
            return (l+r)/2;
        }
    }tree[maxn*4];
    int n,k;
    int sum1[maxn<<2],sum2[maxn<<2];
    void pushup(int rt){
        sum1[rt]=min(sum1[rt<<1],sum1[rt<<1|1]);
        sum2[rt]=max(sum2[rt<<1],sum2[rt<<1|1]);
    }
    void build(int l,int r,int rt){
        tree[rt].l=l;tree[rt].r=r;
       if(l==r){
        tree[rt].l=tree[rt].r=l;
        scanf("%d",&sum1[rt]);
        sum2[rt]=sum1[rt];return ;
       }
       int mid=tree[rt].mid();
       build(lson);
       build(rson);
       pushup(rt);
    }
    int query1(int a,int b,int l,int r,int rt){
        if(a==l&&b==r){
            return sum1[rt];
        }
        int mid=tree[rt].mid();
        if(a>mid){
            return query1(a,b,mid+1,r,rt*2+1);
        }
        else if(b<=mid){
            return query1(a,b,l,mid,rt*2);
        }
        else
            return min(query1(a,mid,lson),query1(mid+1,b,rson));
    
    }
    int query2(int a,int b,int l,int r,int rt){
        if(a==l&&b==r){
            return sum2[rt];
        }
        int mid=tree[rt].mid();
        if(a>mid){
            return query2(a,b,mid+1,r,rt*2+1);
        }
        else if(b<=mid){
            return query2(a,b,l,mid,rt*2);
        }
        else
            return max(query2(a,mid,lson),query2(mid+1,b,rson));
    
    }
    int main(){
        mem(sum1,inf);mem(sum2,-inf);
        scanf("%d%d",&n,&k);
        build(root);
        for(int i=1;i<=n-k+1;i++){
                if(i!=1)printf(" ");
          printf("%d",query1(i,i+k-1,root));
        }
        printf("
    ");
        for(int i=1;i<=n-k+1;i++){
                if(i!=1)printf(" ");
          printf("%d",query2(i,i+k-1,root));
        }
        printf("
    ");
    }


  • 相关阅读:
    .net Winform 32位桌面应用程序突破系统2G内存限制,解决内存溢出问题
    .Net Core WebAPI Swagger Failed to load API definition
    MySql 安装详细步骤
    Vue ElementUI 按需引入提示Cannot find module 'babelpresetes2015'
    复制label标签上的文本
    3D游戏的碰撞检测是如何实现的?
    flex布局定位二
    flex布局定位一
    k8s工作负载、服务、pod
    应用内moniter
  • 原文地址:https://www.cnblogs.com/da-mei/p/9053218.html
Copyright © 2020-2023  润新知