POJ 2823 Sliding Window ST RMQ

Description

An array of size  n ≤ 10 6 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

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("\n");
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("\n");
80     }
81 } 

 

转载于:https://www.cnblogs.com/Yumesenya/p/5501071.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值