zoukankan      html  css  js  c++  java
  • POJ 2823 Sliding Window

    Sliding Window
    Time Limit: 12000MSMemory Limit: 65536K
    Total Submissions: 35486Accepted: 10492
    Case Time Limit: 5000MS

    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 -13
     1 [3  -1  -3] 5  3  6  7 -33
     1  3 [-1  -3  5] 3  6  7 -35
     1  3  -1 [-3  5  3] 6  7 -35
     1  3  -1  -3 [5  3  6] 7 36
     1  3  -1  -3  5 [3  6  7]37

    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
    PS:单调队列水体。
    妈蛋poj还卡scanf,G++超时,换c++输入会快些

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 using namespace std;
     5 const int MAX = 1e6+10;
     6 int a[MAX],b[MAX],pos1[MAX],pos2[MAX];
     7 int ans1[MAX],ans2[MAX];
     8 int main()
     9 {
    10     int x,n,k; int cur1,cur2;
    11     scanf("%d %d",&n,&k);
    12     int rear=0,front=0; cur1=cur2=0;
    13     int rear2=0,front2=0;
    14     for(int i=0;i<n;i++)
    15     {
    16         scanf("%d",&x);
    17         while(front<rear&&a[rear-1]>x) rear--;
    18         a[rear]=x; pos1[rear++]=i;
    19         while(front2<rear2&&b[rear2-1]<x) rear2--;
    20         b[rear2]=x; pos2[rear2++]=i;
    21         if(i>=k-1)
    22         {
    23             ans1[cur1++]=a[front];
    24             ans2[cur2++]=b[front2];
    25             if(i-pos1[front]+1>=k) front++;
    26             if(i-pos2[front2]+1>=k) front2++;
    27         }
    28     }
    29     for(int i=0;i<cur1;i++)
    30     {
    31         if(i) printf(" %d",ans1[i]);
    32         else printf("%d",ans1[i]);
    33     }
    34     printf(" ");
    35     for(int i=0;i<cur2;i++)
    36     {
    37         if(i) printf(" %d",ans2[i]);
    38         else printf("%d",ans2[i]);
    39     }
    40     printf(" ");
    41 
    42     return 0;
    43 }
    mycode
  • 相关阅读:
    SQL语句在数据库中可以执行在mybatis执行不了
    spring_08aop原理及案例
    spring_07使用spring的特殊bean、完成分散配置
    spring_06装配bean_2
    spring_05装配bean
    spring_03ApplicationContext三种经常用到的实现
    spring_04bean的生命周期
    spring_02工具及接口案例
    深入理解Ribbon之源码解析
    深入理解Feign之源码解析
  • 原文地址:https://www.cnblogs.com/acvc/p/3601580.html
Copyright © 2011-2022 走看看