51nod 1272最大距離

max(ji)ai<aj\max(j-i),a_i < a_j
法1⃣️:從前往後維護一個單減棧,每一個新值去棧裏二分查。
法2⃣️:離散化,然後問題轉化爲求max(ajai)i<j\max(a_j-a_i),i < j.設一差分數組bi=aiai1b_i=a_i-a_{i-1},問題就又轉化爲求bib_i的最大子序列。

#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;
}


發佈了151 篇原創文章 · 獲贊 27 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章