题目描述
思路:
我们发现第一次一定要选最大值,然后分别往两边找分别的最大值,用dfs+RMQ实现
c o d e code code
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
const int MAXN = 1e5 + 10;
int n;
int a[MAXN], f[MAXN][21], pos[MAXN][21];
void RMQ() {
for(int j = 1; j <= 20; j ++)
for(int i = 1; i + (1 << j) - 1 <= n; i ++)
if(f[i][j - 1] > f[i + (1 << j - 1)][j - 1])
f[i][j] = f[i][j - 1], pos[i][j] = pos[i][j - 1];
else f[i][j] = f[i + (1 << j - 1)][j - 1], pos[i][j] = pos[i + (1 << j - 1)][j - 1];
}
int query_(int l, int r) {
int k = log2(r - l + 1);
if(f[l][k] > f[r - (1 << k) + 1][k]) return pos[l][k];
else return pos[r - (1 << k) + 1][k];
}
int dfs(int l, int r) {
if(l > r) return 0;
int w = query_(l, r);
return max(dfs(l, w - 1), dfs(w + 1, r)) + 1;
}
int main() {
scanf("%d", &n);
for(int i = 1; i <= n; i ++) scanf("%d", &f[i][0]), pos[i][0] = i;
RMQ();
printf("%d", dfs(1, n));
return 0;
}