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

    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
    

    Source

     
    题意 :给定长n的数列,问长为k的区间在数列中所有情况的最小值和最大值。
    思路:学长教导的RMQ解法,ST版实质是DP,比起不太懂DP的以前,现在感觉好理解多了。此外感觉可以使用线段树解。
    注意先打log的表。
     1 #include <stdio.h>
     2 #include <algorithm>
     3 //#define LOG[i] = (i & (i - 1)) ? LOG[i - 1] : LOG[i - 1] + 1
     4 #define MAXX 1234567
     5 #include <vector>
     6 using namespace std;
     7 
     8 int a[MAXX];
     9 int dp1[MAXX][22];
    10 int LOG[MAXX];
    11 
    12 void init(int n)
    13 {
    14     LOG[1] = 0; 
    15     for(int i=2; i<=n; i++)
    16         LOG[i]=(i&(i-1))?LOG[i-1]:LOG[i-1]+1;
    17 }
    18 
    19 int ST(int l, int r, int i)
    20 {
    21     int k=LOG[r-l+1];
    22     if(i==1)
    23         return max(dp1[l][k],dp1[r-(1<<k)+1][k]);
    24     if(i==0)
    25         return min(dp1[l][k],dp1[r-(1<<k)+1][k]);
    26 }
    27 int main()
    28 {
    29     int n, k;
    30     
    31     while(~scanf("%d%d",&n, &k))
    32     {
    33         int i, j;
    34         init(n); 
    35         for(i=1; i<=n; i++)
    36         {
    37             scanf("%d", &a[i]);
    38             dp1[i][0]=a[i];
    39         }
    40         for(j=1; j<=20; j++)
    41         { 
    42             for(i=1; i<=n; i++)
    43             {
    44                 if(i+(1<<j)-1>n)
    45                     break;
    46                 dp1[i][j]=min(dp1[i][j-1], dp1[i+(1<<(j-1))][j-1]);
    47             }
    48         }
    49         for(i=1; i<=n-k+1; i++)
    50         {
    51             if(i!=1)
    52                 printf(" ");
    53             printf("%d", ST(i,i+k-1,0));
    54         }
    55         //////
    56 
    57         for(i=1; i<=n; i++)
    58         {
    59             dp1[i][0]=a[i];
    60             for(j=1; j<=20; j++)
    61                 dp1[i][j]=0;
    62         }
    63         for(j=1; j<=20; j++)
    64         { 
    65             for(i=1; i<=n; i++)
    66             {
    67                 if(i+(1<<j)-1>n)
    68                     break;
    69                 dp1[i][j]=max(dp1[i][j-1], dp1[i+(1<<(j-1))][j-1]);
    70             }
    71         } 
    72         printf("
    ");
    73         for(i=1; i<=n-k+1; i++)
    74         {
    75             if(i!=1)
    76                 printf(" ");
    77             printf("%d", ST(i,i+k-1,1));
    78         }
    79         printf("
    ");
    80     }
    81 } 
  • 相关阅读:
    System.Diagnostics.Process.Start()
    Asp.Net 构架(HttpModule 介绍) Part.3
    Asp.Net 构架(Http Handler 介绍) Part.2
    Asp.Net构架(Http请求处理流程)
    Ruby 2.0 发布首个预览版
    Java基本数据类型及类型转换
    J2EE 1.4 APIs and Technologies
    java final 关键字
    Android获取通讯录
    Activity的四种加载模式(转载)
  • 原文地址:https://www.cnblogs.com/Yumesenya/p/5501071.html
Copyright © 2011-2022 走看看