求
法1⃣️:從前往後維護一個單減棧,每一個新值去棧裏二分查。
法2⃣️:離散化,然後問題轉化爲求.設一差分數組,問題就又轉化爲求的最大子序列。
#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;
}