zoukankan      html  css  js  c++  java
  • 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 }
    
    
  • 相关阅读:
    Eth-Trunk 技术
    MSTP技术
    STP生成树原理及不足
    表示数值的字符串(python)
    字符流中第一个不重复的字符(python)
    连续子数组的最大和(python)
    和为S的两个数字(python)
    数组中重复的数字(python)
    构建乘积数组(python)
    idea中查看类层级class hierarchy
  • 原文地址:https://www.cnblogs.com/homura/p/4685912.html
Copyright © 2011-2022 走看看