ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

算法学习(5):ST表

2021-04-30 18:01:21  阅读:249  来源: 互联网

标签:50000 int Max height 学习 算法 000 cows ST


ST表(Sparse Table,稀疏表)是一种简单的数据结构,主要用来解决RMQ(Range Maximum/Minimum Query,区间最大/最小值查询)问题。

//进行预处理,计算区间最大值
int f[MAXN][21]; // 第二维的大小根据数据范围决定,不小于log(MAXN)
for (int i = 1; i <= n; ++i)
    f[i][0] = read(); // 读入数据
for (int i = 1; i <= 20; ++i)
    for (int j = 1; j + (1 << i) - 1 <= n; ++j)
        f[j][i] = max(f[j][i - 1], f[j + (1 << (i - 1))][i - 1]);

//预处理log计算
for (int i = 2; i <= n; ++i)
    Log2[i] = Log2[i / 2] + 1;

//查询
for (int i = 0; i < m; ++i)
{
    int l = read(), r = read();
    int s = Log2[r - l + 1];
    printf("%d\n", max(f[l][s], f[r - (1 << s) + 1][s]));
}

例题:
(洛谷P2880 [USACO07JAN]平衡的阵容Balanced Lineup)

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 ≤ 180,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.

#include<bits/stdc++.h>
using namespace std;
int Log[50000+2],Max[50000+2][20],Min[50000+2][20];
int main() {
	int n,m;
	cin >> n >> m;
	for(int i = 1;i <= n; ++ i) {
		cin >> Max[i][0];
		Min[i][0] = Max[i][0];
	}
	for(int i = 2;i <= n; ++ i) {
		Log[i] = Log[i / 2] + 1;
	}
	for(int i = 1;i < 20; ++ i) {
		for(int j = 1;j + (1 << i) - 1 <= n; ++ j) {
			Min[j][i] = min(Min[j][i - 1], Min[j + (1 << (i - 1))][i - 1]);
            Max[j][i] = max(Max[j][i - 1], Max[j + (1 << (i - 1))][i - 1]);
		}
	}
	for(int i = 0;i < m; ++ i) {
		int l,r;
		cin >> l >> r;
		int s = Log[r - l + 1];
		int ma = max(Max[l][s], Max[r - (1 << s) + 1][s]);
        int mi = min(Min[l][s], Min[r - (1 << s) + 1][s]);
        printf("%d\n", ma - mi);
	}
	return 0;
} 

标签:50000,int,Max,height,学习,算法,000,cows,ST
来源: https://www.cnblogs.com/xiaoxingaa/p/14722904.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有