Balanced Lineup
Time Limit: 5000MS Memory Limit: 65536K
Case Time Limit: 2000MS
Description
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
Line 1: Two space-separated integers, N and Q.
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
Lines 1…Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.
Sample Input
6 3
1
7
3
4
2
5
1 5
4 6
2 2
Sample Output
6
3
0
第一种做法 (线段树)
思路:
线段树的一题,首先就是建树,然后再建树的时候不断更新区间的最大值以及最小值,最后在查询的时候就可以直接找到这个区间并且用里面的最大值和最小值就行了。
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 50010;
const int inf = 0x7fffffff;
struct NODE {
int l;
int r;
int high;
int MAX;
int MIN;
int mid() {
return (l + r) >> 1;
}
};
NODE node[maxn << 2];
void PushUp(int rt) {
node[rt].MAX = max(node[rt << 1].MAX, node[rt << 1 | 1].MAX);
node[rt].MIN = min(node[rt << 1].MIN, node[rt << 1 | 1].MIN);
}
void BuildTree(int l, int r, int rt) {
node[rt].l = l;
node[rt].r = r;
if (l == r) {
scanf("%d", &node[rt].high);
node[rt].MAX = node[rt].MIN = node[rt].high;
return ;
}
int m = node[rt].mid();
BuildTree(l, m, rt << 1);
BuildTree(m + 1, r, rt << 1 | 1);
PushUp(rt);
}
int Max = 0, Min = inf;
void Query(int l, int r, int rt) {
if (node[rt].l == l && node[rt].r == r) {
Max = max(Max, node[rt].MAX);
Min = min(Min, node[rt].MIN);
return ;
}
int m = node[rt].mid();
if (r <= m) Query(l, r, rt << 1);
else if (l > m) Query(l, r, rt << 1 | 1);
else {
Query(l, m, rt << 1);
Query(m + 1, r, rt << 1 | 1);
}
}
int main() {
int n, m;
scanf("%d %d", &n, &m);
BuildTree(1, n, 1);
while (m--) {
int a, b;
Max = 0;
Min = inf;
scanf("%d %d", &a, &b);
Query(a, b, 1);
printf("%d\n", Max - Min);
}
return 0;
}
第二种做法(RMQ)
思路:
RMQ实际上就是一个用dp来实现每一个区间的存储,但是由于区间可能很大,数组无法开到那么大,所以二位数组中的i,j代表的就不是区间的l和r了,他们代表的是区间[i, i + 2j - 1]。
- 1:建立查询的dp数组:RMQ用的倍增的想法,所以就要知道n,最大可以倍增到哪个地方(也就是说n>=2x),然后就可以根据dp表示的区间[i, i + 2j - 1]来求dp数组了,这个时候你会发现求dp的时候的dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]分别表示着一个区间的两段,前段是i~i + 2j - 1, 后一段是i + 2j-1~ i +2j - 1两个区间的距离都是2j-1 - 1,然后才能找到最大的是多少。
- 2:查询的时候区间有两个,他把需要查询的都包括了进去,因为是倍增求得,所以假如r不是2k的时候dp就无法将l~r全部包括进去了,所以才需要两个区间去查询,比如查询1-5的话,那么这两个区间就分别是[1,4][2-5]也就将[1,5]都包括进去了。
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 50010;
int dp[maxn][20] = {0}, dpn[maxn][20];
int mm[maxn] = {0};
void BuildRmq(int n, int *a) {
mm[0] = -1;
for (int i = 1; i <= n; i++) {
mm[i] = mm[i - 1];
if ((i & (i - 1)) == 0) mm[i]++;
dp[i][0] = a[i];
dpn[i][0] = a[i];
}
for (int j = 1; j <= mm[n]; j++) {
for (int i = 1; i + (1 << j) - 1 <= n; i++) {
dp[i][j] = max(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);
dpn[i][j] = min(dpn[i][j - 1], dpn[i + (1 << (j - 1))][j - 1]);
}
}
}
int RmqMax(int l, int r) {
int k = mm[r - l + 1];
return max(dp[l][k], dp[r - (1 << k) + 1][k]);
}
int RmqMin(int l, int r) {
int k = mm[r - l + 1];
return min(dpn[l][k], dpn[r - (1 << k) + 1][k]);
}
int main() {
int n, m;
int a[maxn] = {0};
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
BuildRmq(n, a);
while (m--) {
int l, r;
scanf("%d %d", &l, &r);
printf("%d\n", RmqMax(l, r) - RmqMin(l, r));
}
return 0;
}