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 }
    
    
  • 相关阅读:
    Ninject Lazy Load问题
    log4net 极简配置
    log4net 使用指南,最常遇到的问题整理。。。
    【应聘】阿里巴巴Java面试题目
    【Unity3D】自动寻路(Nav Mesh Agent组件)
    【unity3D】鼠标控制camera的移动、放大(俯视浏览效果、LOL摄像机移动效果)
    【Unity3D】枪战游戏—弹孔设置
    【Unity3D】枪战游戏—发射子弹、射线检测
    【Unity3D】Unity自带组件—完成第一人称人物控制
    软件工程【第5章】- 需求工程与需求设计
  • 原文地址:https://www.cnblogs.com/homura/p/4685912.html
Copyright © 2011-2022 走看看