【luogu P2251 质量检测】 题解

也是一道ST表的板子题,很裸的RMQ问题,只需要在查询区间时处理一下下就好啦~

 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 const int maxn = 1000000 + 10;
 5 int st[maxn][21], a[maxn], ans[maxn];
 6 int n, m, i, j, left, right;
 7 int main()
 8 {
 9     scanf("%d%d", &n, &m);
10     for(i = 1; i <= n; i++)
11     {
12         scanf("%d", &a[i]);
13         st[i][0] = a[i];
14     }
15     
16     for(j = 1; (1<<j) <= n; j++)
17         for(i = 1; i <= n-(1<<j)+1; i++)
18         st[i][j] = min(st[i][j-1],st[i+(1<<(j-1))][j-1]);
19     
20     right = m;
21     for(i = 1; i <= n-m+1; i++)
22     {
23         j = 0;
24         left = i;
25         while((1<<(j+1)) <= (right-left+1)) j++;
26         ans[i] = min(st[left][j],st[right-(1<<j)+1][j]);
27         right++;
28     }
29     
30     for(i = 1; i <= n-m+1; i++)
31     printf("%d\n",ans[i]);
32     return 0;
33 }

 

posted @ 2018-02-26 09:28  Misaka_Azusa  阅读(147)  评论(0编辑  收藏  举报
Live2D