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 }
    
    
  • 相关阅读:
    tensorflow学习3---mnist
    tensorflow学习2-线性拟合和神经网路拟合
    关于泛型数据结构中OrderBy的使用
    敏捷开发之观察者模式
    敏捷开发之设计文档
    C#算法实现获取树的高度
    武林高手?敏捷开发,唯velocity取胜
    C#接口多继承方法重名问题
    .Net平台技术栈?不止于此
    浅谈C#中Tuple和Func的使用
  • 原文地址:https://www.cnblogs.com/homura/p/4685912.html
Copyright © 2011-2022 走看看