• POJ2823 单调队列


    #include<stdio.h>
    #define max 1000000+5
    int a[max],q1[max]/*单调递增*/,q2[max]/*单调递减*/,ans1[max],ans2[max];
    int n,k,h1,t1,h2,t2;

    void q1_in(int i){           //入队
        while(h1<=t1&&a[q1[t1]]>=a[i]){
            t1--;
        }
        q1[++t1]=i;
    }

    void q2_in(int i){ //入队
        while(h2<=t2&&a[q2[t2]]<=a[i]){
            t2--;
        }
        q2[++t2]=i;
    }

    int main(){
        while(~scanf("%d%d",&n,&k)){
            int i;
            for(i=1;i<=n;i++){
                scanf("%d",&a[i]);
            }
            h1=h2=1;t1=t2=0;

            for(i=1;i<k;i++){
                q1_in(i);
                q2_in(i);
            }
            int c1=0,c2=0;
            for(i=k;i<=n;i++){
                q1_in(i);
                q2_in(i);

                while(q1[h1]<i-k+1){//出队
                    h1++;
                }
                while(q2[h2]<i-k+1){//出队
                    h2++;
                }
                ans1[c1++]=a[q1[h1]];
                ans2[c2++]=a[q2[h2]];
            }
            for(i=0;i<c1;i++){
                if(i){
                    printf(" ");
                }
                printf("%d",ans1[i]);
            }
            puts("");
            for(i=0;i<c2;i++){
                if(i){
                    printf(" ");
                }
                printf("%d",ans2[i]);
            }
            puts("");
        }
    }


  • 相关阅读:
    python拼接字符串
    SyntaxError: Non-ASCII character 'xe5' in file a.py on line 9, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
    Python三目运算符
    上一条下一条方案对比
    编程注意
    Running asp net mvc4 on ubuntu
    慎重使用正则
    Web开发注意
    懒人的服务监控
    编辑器选择
  • 原文地址:https://www.cnblogs.com/Stomach-ache/p/3703238.html
Copyright © 2020-2023  润新知