RMQPOJ3264
Posted shuizhidao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RMQPOJ3264相关的知识,希望对你有一定的参考价值。
这里说几篇博客,建议从上到下看
https://blog.csdn.net/qq_31759205/article/details/75008659
https://blog.csdn.net/sgh666666/article/details/80448284
https://www.cnblogs.com/kuangbin/p/3227420.html
------------------------------------------------------------------------------------------------------------
这里介绍一维的,二维看上面第三篇博客
1)关于RMQ和ST
简单来说,RMQ(Range Minimum/Maximum Query),即区间最值查询,是一个查询!而ST(Sparse Table)。。。就是打表
2)dp表
RMQ说到底就是打这个表。我们开一个二维 dp[i][j],它表示的是从第 i 个数开始算,长度为 2^j 这个区间的最值,例如 1, 2, 3, 4, 5,那么 dp[2][2] 表示的是在区间 2, 3, 4, 5, 这 2^2 个数的最值。因为是 2 的次方,所以这些数的个数一定为偶数。
这里我们设题目给的数为 a[i]
1、dp[i][0] = a[0];
2、因为数为偶数,所以每个长度都可以分成两半,长度都为 2^(j - 1), 一个从 i 开始,到 i + 2^(j - 1) - 1 结束(i 自己也算一个长度),另一个从 i + 2^(j - 1) 开始,即 dp[i][j] = max(dp[i][j - 1], dp[i + (1 << j)][j - 1])
3)查询
例如查询 1, 2, 3, 4, 5 我们可以查找区间 1, 2, 3, 4 和区间 2, 3, 4, 5 的最值。即以长度 j 为标准,查询区间为 r - l,长度为 r - l + 1,就让 j <= r - l + 1,并使 j 最大就可以,这样只要求出 j ,就可以算 ans = max(dp[l][j], dp[r - (1 << j) + 1][j])
----------------------------------------------------------------------------------------------------
下面题目
POJ3264 http://poj.org/problem?id=3264
For the daily milking, Farmer John‘s N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.
Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.
Input
Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i
Lines N+2..N+Q+1: Two integers A and B (1 ≤ A ≤ B ≤ N), representing the range of cows from A to B inclusive.
Output
Sample Input
6 3 1 7 3 4 2 5 1 5 4 6 2 2
Sample Output
6 3 0
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <iomanip> #include <string> #include <sstream> #include <algorithm> #include <stack> #include <queue> #include <set> #include <map> using namespace std; typedef long long LL; const int INF = 0x3f3f3f3f; const int MAXN = 1005; const int MOD = 1e9 + 7; #define MemI(x) memset(x, -1, sizeof(x)) #define Mem0(x) memset(x, 0, sizeof(x)) #define MemM(x) memset(x, 0x3f, sizeof(x)); int dp_max[50005][20], dp_min[50005][20]; int n, m; void ST() { int i, j; for(i = 1;i <= n;++i) { cin >> dp_max[i][0]; dp_min[i][0] = dp_max[i][0]; } //这里循环手动模拟就懂 for(j = 1;(1 << j) <= n;++j) for(i = 1;i + (1 << j) - 1 <= n;++i) { dp_max[i][j] = max(dp_max[i][j - 1], dp_max[i + (1 << (j - 1))][j]); dp_min[i][j] = min(dp_min[i][j - 1], dp_min[i + (1 << (j - 1))][j]); } } int RMQ(int l, int r) { int k = 0; while((1 << (k + 1)) <= r - l + 1) k++; int a = max(dp_max[l][k], dp_max[r - (1 << k) + 1][k]); int b = min(dp_min[l][k], dp_min[r - (1 << k) + 1][k]); return a - b; } int main() { Mem0(dp_max); MemM(dp_min); cin >> n >> m; ST(); int a, b; while(m--) { cin >> a >> b; cout << RMQ(a, b) << endl; } return 0; }
以上是关于RMQPOJ3264的主要内容,如果未能解决你的问题,请参考以下文章