求
max
(
j
−
i
)
,
a
i
<
a
j
\max(j-i),a_i < a_j
max(j−i),ai<aj
法1⃣️:从前往后维护一个单减栈,每一个新值去栈里二分查。
法2⃣️:离散化,然后问题转化为求
max
(
a
j
−
a
i
)
,
i
<
j
\max(a_j-a_i),i < j
max(aj−ai),i<j.设一差分数组
b
i
=
a
i
−
a
i
−
1
b_i=a_i-a_{i-1}
bi=ai−ai−1,问题就又转化为求
b
i
b_i
bi的最大子序列。
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#define mem(ss) memset(ss,0,sizeof(ss))
#define rep(d, s, t) for(int d=s;d<=t;d++)
#define rev(d, s, t) for(int d=s;d>=t;d--)
#define inf 0x3f3f3f3f
typedef long long ll;
typedef long double ld;
typedef double db;
typedef std::pair<int, int> pii;
typedef std::pair<ll, ll> pll;
const ll mod = 1e9 + 7;
const int N = 5e4 + 10;
#define io_opt ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
inline ll read() {
ll x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
struct node {
ll elem, pos;
bool operator<(const node &rhs) const {
if (elem == rhs.elem)
return pos < rhs.pos;
return elem < rhs.elem;
}
} a[N];
int main() {
ll n, b[N];
cin >> n;
rep(i, 1, n) cin >> a[i].elem, a[i].pos = i;
sort(a + 1, a + n + 1);
rep(i, 1, n - 1) b[i] = a[i + 1].pos - a[i].pos;
int sum = 0, ans = 0;
rep(i, 1, n - 1) {
sum = max(b[i], sum + b[i]);
ans = max(ans, sum);
}
cout << max(0, ans);
return 0;
}