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
  • 相关阅读:
    Scanner类
    16 String类
    15_面向对象_01
    14_面向对象_01_2 完整格式
    14_面向对象_01_1 成员变量 成员方法
    13_面向对象辅助看懂实例
    面向对象的基本概念
    Servlet_03 进阶随笔 两种域的运用调用方式
    Servlet_03 资源跳转 关键字
    Servlet_02 资源跳转
  • 原文地址:https://www.cnblogs.com/acvc/p/3601580.html
Copyright © 2011-2022 走看看