RMQ_st

RMQ问题的st算法,可以说是求区间最值很常用的算法,之前一直不知道,直到15年网络赛一道很简单的题看了犇哥用了这种方法决定脑补一下。虽然已经过去半年了,还是磨出来了,以poj的一道题为例。

poj 3264

Balanced Lineup
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 42407 Accepted: 19957
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
意思是数组长度是n,有q次询问,输出[A,B]中最大值和最小值的差。数据量非常大。

直接贴代码吧

/*
 * rmp_st.cpp
 *
 *  Created on: 2016年3月4日
 *      Author: Triose
 */
#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<iterator>
#include<math.h>
#include<stdlib.h>
#include<map>
#include<set>
using namespace std;
//#define ONLINE_JUDGE
#define eps 1e-8
#define INF 0x7fffffff
#define inf 0x3f3f3f3f
#define rep(i,a) for((i)=0; i<(a);(i)++)
#define mem(a,b) (memset((a),b,sizeof(a)))
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sfs(a) scanf("%s",a)
#define pf(a) printf("%d\n",a)
#define pfs(a) printf("%s\n",a)
#define pfI(a) printf("%I64d\n",a)
#define LL __int64
const double PI = acos(-1.0);
template<class T> T gcd(T a, T b) { return b ? gcd(b, a%b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b)*b; }
template<class T> inline T Min(T a, T b) { return a < b ? a : b; }
template<class T> inline T Max(T a, T b) { return a > b ? a : b; }
int n, m;
int x, y;
#define N 50000
struct node {
	int max_num;
	int min_num;
};
node dp[N][20];
int a[N];
void Init() {
	int high = (int)log2(n * 1.0) + 1;
	for(int j = 1; j < high; j++) {
		int k = 1 << (j - 1);
		for(int i = 1; i - 1 + 2 * k <= n; i++) {
			dp[i][j].max_num = Max(dp[i][j - 1].max_num,dp[i + k][j - 1].max_num);
			dp[i][j].min_num = Min(dp[i][j - 1].min_num,dp[i + k][j - 1].min_num);
		}
	}
}
int main() {
#ifndef ONLINE_JUDGE
//	freopen("in.txt","r",stdin);
//	freopen("Out.txt", "w", stdout);
#endif
	while(~sfd(n,m)) {
		for(int i = 1; i <= n; i++) {
			sf(a[i]);
			dp[i][0].max_num = a[i];
			dp[i][0].min_num = a[i];
		}
		Init();
		for(int i = 0; i < m; i++) {
			sfd(x,y);
			int k = (int)log2(y - x + 1.0);
			pf(Max(dp[x][k].max_num,dp[y - (1 << k) + 1][k].max_num) - Min(dp[x][k].min_num,dp[y - (1 << k) + 1][k].min_num));
		}
	}
	return 0;
}
这是一道一维的st,因为询问次数很多,所以必须要求O(1)。所以只能用预处理,但是呢,n的范围是 (1 ≤ N ≤ 50,000) ,如果要dp[N][N],恐怕任何一个编译器也不会让你的代码过。所以只能是dp[N][lg2(N)]的数组来处理。下面讲原理:


然后我准备做一道二维的rmq问题。。。。不知道再编辑这篇解题报告又是在何年何月了

特别难得,在两小时后我做出了另外一道题目——poj,2019

Cornfields
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 6212 Accepted: 3062

Description

FJ has decided to grow his own corn hybrid in order to help the cows make the best possible milk. To that end, he's looking to build the cornfield on the flattest piece of land he can find. 

FJ has, at great expense, surveyed his square farm of N x N hectares (1 <= N <= 250). Each hectare has an integer elevation (0 <= elevation <= 250) associated with it. 

FJ will present your program with the elevations and a set of K (1 <= K <= 100,000) queries of the form "in this B x B submatrix, what is the maximum and minimum elevation?". The integer B (1 <= B <= N) is the size of one edge of the square cornfield and is a constant for every inquiry. Help FJ find the best place to put his cornfield. 

Input

* Line 1: Three space-separated integers: N, B, and K. 

* Lines 2..N+1: Each line contains N space-separated integers. Line 2 represents row 1; line 3 represents row 2, etc. The first integer on each line represents column 1; the second integer represents column 2; etc. 

* Lines N+2..N+K+1: Each line contains two space-separated integers representing a query. The first integer is the top row of the query; the second integer is the left column of the query. The integers are in the range 1..N-B+1. 

Output

* Lines 1..K: A single integer per line representing the difference between the max and the min in each query. 

Sample Input

5 3 1
5 1 2 6 3
1 3 5 2 7
7 2 4 6 1
9 9 8 6 5
0 6 9 3 9
1 2

Sample Output

5
大意是:n * n的矩阵,让你求b * b的矩阵中最大值和最小值的差,然后会有k次询问,每次输入坐标x,y;

先上代码:

/*
 * train.cpp
 *
 *  Created on: 2016年3月5日
 *      Author: Triose
 */

#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<iterator>
#include<math.h>
#include<stdlib.h>
#include<map>
#include<set>
using namespace std;
//#define ONLINE_JUDGE
#define eps 1e-8
#define INF 0x7fffffff
#define inf 0x3f3f3f3f
#define rep(i,a) for((i)=0; i<(a);(i)++)
#define mem(a,b) (memset((a),b,sizeof(a)))
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sfs(a) scanf("%s",a)
#define pf(a) printf("%d\n",a)
#define pfs(a) printf("%s\n",a)
#define pfI(a) printf("%I64d\n",a)
#define LL __int64
const double PI = acos(-1.0);
template<class T> T gcd(T a, T b) { return b ? gcd(b, a%b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b)*b; }
template<class T> inline T Min(T a, T b) { return a < b ? a : b; }
template<class T> inline T Max(T a, T b) { return a > b ? a : b; }
int n, m, q;
#define N 255
struct node {
	int max_num;
	int min_num;
};
node dp[N][N][10];
int x,y;
void RMQ_Init() {
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= n; j++) {
			sf(dp[i][j][0].max_num);
			dp[i][j][0].min_num = dp[i][j][0].max_num;
		}
	}
	int maxn = (int)log2(n * 1.0) + 1;
	for(int k = 1; k < maxn; k++) {
		int tmp = 1 << (k - 1);
		for(int i = 1; i + 2 * k - 1 <= n; i++) {
			for(int j = 1; j + 2 * k - 1 <= n; j++) {
				dp[i][j][k].max_num = Max(Max(dp[i][j][k - 1].max_num, dp[i + tmp][j][k - 1].max_num), Max(dp[i][j + tmp][k - 1].max_num,dp[i + tmp][j + tmp][k - 1].max_num));
				dp[i][j][k].min_num = Min(Min(dp[i][j][k - 1].min_num, dp[i + tmp][j][k - 1].min_num), Min(dp[i][j + tmp][k - 1].min_num,dp[i + tmp][j + tmp][k - 1].min_num));
			}
		}
	}
}
void search() {
	int k = (int)log2(m * 1.0);
	int limit_x = x + m;
	int limit_y = y + m;
	int up = Max(Max(dp[x][y][k].max_num,dp[limit_x - (1 << k)][y][k].max_num),Max(dp[x][limit_y - (1 << k)][k].max_num,dp[limit_x - (1 << k)][limit_y - (1 << k)][k].max_num));
	int down = Min(Min(dp[x][y][k].min_num,dp[limit_x - (1 << k)][y][k].min_num),Min(dp[x][limit_y - (1 << k)][k].min_num,dp[limit_x - (1 << k)][limit_y - (1 << k)][k].min_num));
	pf(up - down);
}
int main() {
#ifndef ONLINE_JUDGE
//	freopen("in.txt","r",stdin);
//	freopen("Out.txt", "w", stdout);
#endif
	while(~sft(n,m,q)) {
		RMQ_Init();
		for(int i = 0; i < q; i++) {
			sfd(x,y);
			search();
		}
	}
	return 0;
}

这道题和上面那道题相比其实就是维度的推广而已,基本想法其实一模一样,只是区间长度变成了矩阵的“面积”


字丑勿喷

【完美复现】面向配电网韧性提升的移动储能预布局与动态调度策略【IEEE33节点】(Matlab代码实现)内容概要:本文介绍了基于IEEE33节点的配电网韧性提升方法,重点研究了移动储能系统的预布局与动态调度策略。通过Matlab代码实现,提出了一种结合预配置和动态调度的两阶段优化模型,旨在应对电网故障或极端事件时快速恢复供电能力。文中采用了多种智能优化算法(如PSO、MPSO、TACPSO、SOA、GA等)进行对比分析,验证所提策略的有效性和优越性。研究不仅关注移动储能单元的初始部署位置,还深入探讨其在故障发生后的动态路径规划与电力支援过程,从而全面提升配电网的韧性水平。; 适合人群:具备电力系统基础知识和Matlab编程能力的研究生、科研人员及从事智能电网、能源系统优化等相关领域的工程技术人员。; 使用场景及目标:①用于科研复现,特别是IEEE顶刊或SCI一区论文中关于配电网韧性、应急电源调度的研究;②支撑电力系统在灾害或故障条件下的恢复力优化设计,提升实际电网应对突发事件的能力;③为移动储能系统在智能配电网中的应用提供理论依据和技术支持。; 阅读建议:建议读者结合提供的Matlab代码逐模块分析,重点关注目标函数建模、约束条件设置以及智能算法的实现细节。同时推荐参考文中提及的MPS预配置与动态调度上下两部分,系统掌握完整的技术路线,并可通过替换不同算法或测试系统进一步拓展研究。
先看效果: https://pan.quark.cn/s/3756295eddc9 在C#软件开发过程中,DateTimePicker组件被视为一种常见且关键的构成部分,它为用户提供了图形化的途径来选取日期与时间。 此类控件多应用于需要用户输入日期或时间数据的场景,例如日程管理、订单管理或时间记录等情境。 针对这一主题,我们将细致研究DateTimePicker的操作方法、具备的功能以及相关的C#编程理念。 DateTimePicker控件是由.NET Framework所支持的一种界面组件,适用于在Windows Forms应用程序中部署。 在构建阶段,程序员能够通过调整属性来设定其视觉形态及运作模式,诸如设定日期的显示格式、是否展现时间选项、预设的初始值等。 在执行阶段,用户能够通过点击日历图标的下拉列表来选定日期,或是在文本区域直接键入日期信息,随后按下Tab键或回车键以确认所选定的内容。 在C#语言中,DateTime结构是处理日期与时间数据的核心,而DateTimePicker控件的值则表现为DateTime类型的实例。 用户能够借助`Value`属性来读取或设定用户所选择的日期与时间。 例如,以下代码片段展示了如何为DateTimePicker设定初始的日期值:```csharpDateTimePicker dateTimePicker = new DateTimePicker();dateTimePicker.Value = DateTime.Now;```再者,DateTimePicker控件还内置了事件响应机制,比如`ValueChanged`事件,当用户修改日期或时间时会自动激活。 开发者可以注册该事件以执行特定的功能,例如进行输入验证或更新关联的数据:``...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值