算法---倍增法和ST算法

目录

1;倍增法的概念

2:ST算法


1;倍增法的概念

什么是倍增法呢?顾名思义,就是成倍增长。在一些数据范围大的算法中,如果一个一个去遍历的话,毫无疑问是会超时的,所以所以就需要倍增法,倍增法是按照2倍2倍的去增加的。

比如说在一个区间中寻找它的最大值,那么按照倍增法如何划分了。首先一个二维数组go[s][t],他表示的是从左区间是s开始的长度为2的t次方的区间,那么按照这样的思想,很容易得出一个方程式:

go[s][t]=max(go[s][t-1],go[s+(1<<(t-1))][t-1])

2:ST算法

ST算法其实就是基于倍增原理的算法,一般我们是用来求解区间最值查询的,就像是在讲到倍增法时我举的例子。具体我们可以看下下面的例题来感受一下。

题目描述

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.

每天,农夫 John 的 n(1\le n\le 5\times 10^4)n(1≤n≤5×104) 头牛总是按同一序列排队。

有一天, John 决定让一些牛们玩一场飞盘比赛。他准备找一群在队列中位置连续的牛来进行比赛。但是为了避免水平悬殊,牛的身高不应该相差太大。John 准备了 q(1\le q\le 1.8\times10^5)q(1≤q≤1.8×105) 个可能的牛的选择和所有牛的身高 h_i(1\le h_i\le 10^6,1\le i\le n)hi​(1≤hi​≤106,1≤i≤n)。他想知道每一组里面最高和最低的牛的身高差。

输入格式

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.

第一行两个数 n,qn,q。

接下来 nn 行,每行一个数 h_ihi​。

再接下来 qq 行,每行两个整数 aa 和 bb,表示询问第 aa 头牛到第 bb 头牛里的最高和最低的牛的身高差。

输出格式

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.

输出共 qq 行,对于每一组询问,输出每一组中最高和最低的牛的身高差。

输入输出样例

输入 #1复制

6 3
1
7
3
4
2
5
1 5
4 6
2 2

输出 #1复制

6
3
0

题解代码:

#include<bits/stdc++.h>
using namespace std;
int n,q;
const int N=1e5+10;
int dp_max[N][30],dp_min[N][30];
int a[N];

void init()
{
	for(int i=1;i<=n;i++){
		dp_max[i][0]=a[i];
		dp_min[i][0]=a[i];
	}
	int k=log2(n);
	
	for(int i=1;i<=k;i++){
		for(int j=1;j+(1<<i)<=n+1;j++){
			dp_max[j][i]=max(dp_max[j][i-1],dp_max[j+(1<<(i-1))][i-1]);
			dp_min[j][i]=min(dp_min[j][i-1],dp_min[j+(1<<(i-1))][i-1]);
		}
	}
}

int answ(int l,int r)
{
	//在区间[l,r]寻找最值
	int k=log2(r-l+1);
	int ma=max(dp_max[l][k],dp_max[r-(1<<k)+1][k]);//最大值
	int mi=min(dp_min[l][k],dp_min[r-(1<<k)+1][k]);//最小值
	return ma-mi;
}

int main()
{
	cin>>n>>q;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	init();
	for(int i=1;i<=q;i++){
		int l,r;
		cin>>l>>r;
		cout<<answ(l,r)<<endl;
	}
	return 0;
}

该题不是很难,代码应该很好看懂,就不细说了哈!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜到极致就是渣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值