• POJ 2823 Sliding Window


                                   Sliding Window
     

    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 k is 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
    

    单调队列
     1 #include<cstdio>
     2 #include<malloc.h>
     3 using namespace std;
     4 
     5 struct node
     6 {
     7     int val;
     8     int pos;
     9 };
    10 
    11 int main()
    12 {
    13     //freopen("in.txt","r",stdin);
    14     int i;
    15     int head1,end1,head2,end2;
    16     int m,n;
    17     while(~scanf("%d%d",&m,&n))
    18     {
    19         int *p=(int*)malloc(sizeof(int)*m);
    20         int *ans1=(int*)malloc(sizeof(int)*m);
    21         int *ans2=(int*)malloc(sizeof(int)*m);
    22         node *que1=(node*)malloc(sizeof(node)*m);
    23         node *que2=(node*)malloc(sizeof(node)*m);
    24         for(i=0;i<m;i++)
    25         scanf("%d",&p[i]);
    26         head1=head2=1,end1=end2=0;
    27         for(i=0;i<m;i++)
    28         {
    29             node temp;
    30             temp.pos=i;
    31             temp.val=p[i];
    32             while(end1!=0&&que1[end1].val<p[i]&&head1<=end1)
    33             end1--;
    34             end1++;
    35             que1[end1]=temp;
    36             while(end1!=0&&que1[head1].pos<i-n+1&&head1<=end1)
    37             head1++;
    38             ans1[i]=que1[head1].val;
    39              while(end2!=0&&que2[end2].val>p[i]&&head2<=end2)
    40             end2--;
    41             end2++;
    42             que2[end2]=temp;
    43             while(end2!=0&&que2[head2].pos<i-n+1&&head2<=end2)
    44             head2++;
    45             ans2[i]=que2[head2].val;
    46         }
    47         for(i=n-1;i<m-1;i++)
    48         printf("%d ",ans2[i]);
    49         printf("%d
    ",ans2[i]);
    50         for(i=n-1;i<m-1;i++)
    51         printf("%d ",ans1[i]);
    52         printf("%d
    ",ans1[i]);
    53         free(ans1),free(ans2),free(que1),free(que2),free(p);
    54     }
    55     return 0;
    56 }
    
    
  • 相关阅读:
    maven
    面试宝典之Java程序运行原理
    并发队列总结
    HashMap探究
    Linux安装python应用之前需要安装的库
    Linux中为Python应用安装uwsgi
    常用SQL语句
    Windows CE无法连接Win 10
    无法加载 DLL xpstar.dll 或它引用的一个 DLL。原因: 126(找不到指定的模块。)。
    SQL Server 数据恢复
  • 原文地址:https://www.cnblogs.com/homura/p/4685912.html
Copyright © 2020-2023  润新知