[Codeforces Round #669 (Div. 2)] D. Discrete Centrifugal Jumps (单调栈,DP)

[Codeforces Round #669 (Div. 2)] D. Discrete Centrifugal Jumps (单调栈,DP)

题面:

题意:

给定一个含有(mathit n)个整数的数组(mathit h),对于每一个(iin[1,n]),可以跳到(mathit j,i<jleq n)时当且仅当以下三个条件中的至少一个满足:

[i+1=j\ max(h_{i+1},…,h_{j−1})<min(h_i,h_j)\ max(h_i,h_j)<min(h_{i+1},…,h_{j−1}). ]

问从( ext 1) 跳到(mathit n) 最少需要几步?

思路:

通过分析的题目的特别,我们可以发现:

假设我们当前在下标为(mathit x)的位置,我们可以去下标为(mathit y)的位置当且仅当满足以下条件中的:

如果(h_xleq h_y)(h_y)需要是(mathit x)右面第一个不小于(h_x)的数。

否则(h_x> h_y)(h_x)需要是(mathit y)左面第一个大于(h_y)的数。

那么我们可以利用单调栈来求出以下四个数组:

(lre_i,lle_i,rle_i,rre_i)

分别代表第(mathit i)个节点左边第一个大于它的下标,左边第一个小于等于它的下标,右边第一个大于它的下标,右边第一个小于它的下标。

然后直接线性动态规划转移一下即可。

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x)  if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("
");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("
");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '
' : ' ');}}
void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '
' : ' ');}}
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0
int n;
int a[maxn];
int dp[maxn];
stack<pii> st;
int lre[maxn];
int lle[maxn];
int rle[maxn];
int rre[maxn];
std::vector<int> jump_to[maxn];
int main()
{
#if DEBUG_Switch
    freopen("D:\code\input.txt", "r", stdin);
#endif
    //freopen("D:\code\output.txt","w",stdout);
    n = readint();
    repd(i, 1, n) {
        a[i] = readint();
        dp[i] = inf;
    }
    dp[1] = 0;
    repd(i, 1, n) {
        while (!st.empty() && st.top().fi < a[i]) {
            st.pop();
        }
        if (!st.empty()) {
            lre[i] = st.top().se;
        }
        st.push(mp(a[i], i));
    }
    while (!st.empty()) {
        st.pop();
    }
    repd(i, 1, n) {
        while (!st.empty() && st.top().fi > a[i]) {
            st.pop();
        }
        if (!st.empty()) {
            lle[i] = st.top().se;
        }
        st.push(mp(a[i], i));
    }
    while (!st.empty()) {
        st.pop();
    }
    for (int i = n; i >= 1; --i) {
        while (!st.empty() && st.top().fi < a[i]) {
            st.pop();
        }
        if (!st.empty()) {
            rre[i] = st.top().se;
        }
        st.push(mp(a[i], i));
    }
    while (!st.empty()) {
        st.pop();
    }
    for (int i = n; i >= 1; --i) {
        while (!st.empty() && st.top().fi > a[i]) {
            st.pop();
        }
        if (!st.empty()) {
            rle[i] = st.top().se;
        }
        st.push(mp(a[i], i));
    }
    while (!st.empty()) {
        st.pop();
    }
    repd(i, 1, n) {
        if (lle[i]) {
            jump_to[lle[i]].push_back(i);
        }
        if (lre[i]) {
            jump_to[lre[i]].push_back(i);
        }
        if (rle[i]) {
            jump_to[i].push_back(rle[i]);
        }
        if (rre[i]) {
            jump_to[i].push_back(rre[i]);
        }
    }
    repd(i, 1, n) {
        for (auto x : jump_to[i]) {
            dp[x] = min(dp[x], dp[i] + 1);
        }
    }
    printf("%d
", dp[n] );
    return 0;
}



本博客为本人原创,如需转载,请必须声明博客的源地址。 本人博客地址为:www.cnblogs.com/qieqiemin/ 希望所写的文章对您有帮助。
原文地址:https://www.cnblogs.com/qieqiemin/p/13641419.html