Codeforces
CF Step
Youtube Linkedin Discord Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Code : Smooth Subsequence

#include <bits/stdc++.h>
using namespace std;

void solve(vector<int> &a, int d) {
    int n = a.size();

    vector<int> dp(n);
    // dp[i] is the maximum length of good subsequence ending at i.

    for (int i = 0; i < n; i++) {
        dp[i] = 1;
        for (int j = i - 1; j >= 0; j--) {
            if (abs(a[i] - a[j]) <= d) {
                dp[i] = max(dp[i], 1 + dp[j]);
            }
        }
    }
    cout << *max_element(dp.begin(), dp.end()) << endl;
}

int main() {
    int n, d;
    cin >> n >> d;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    solve(a, d);
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

void solve(vector<int> &a, int d) {
    int n = a.size();

    int max_val = *max_element(a.begin(), a.end());

    vector<int> dp(max_val + 1);
    // At any point of time, dp[val] is the maximum length of a good
    // subsequence whose last element is val.

    for (int i = 0; i < n; i++) {
        int mx = 0;
        for (int s = max(0, a[i] - d); s <= min(a[i] + d, max_val); s++) {
            mx = max(mx, 1 + dp[s]);
        }
        dp[a[i]] = mx;
    }
    cout << *max_element(dp.begin(), dp.end()) << endl;
}

int main() {
    int n, d;
    cin >> n >> d;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    solve(a, d);
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

class SegmentTree {
  public:
    int n;
    vector<int> t;
    SegmentTree(int n) {
        this->n = n;
        t.resize(4 * n + 1);
    }

    void build(vector<int> &a, int v, int tl, int tr) {
        if (tl == tr) {
            t[v] = a[tl];
            return;
        }

        int tm = (tl + tr) / 2;
        build(a, 2 * v, tl, tm);
        build(a, 2 * v + 1, tm + 1, tr);
        t[v] = max(t[2 * v], t[2 * v + 1]);
    }

    int query(int v, int tl, int tr, int l, int r) {
        if (l > r) {
            return 0;
        }
        if (tl == l && tr == r) {
            return t[v];
        }

        int tm = (tl + tr) / 2;
        auto left = query(2 * v, tl, tm, l, min(tm, r));
        auto right = query(2 * v + 1, tm + 1, tr, max(tm + 1, l), r);
        return max(left, right);
    }

    int get_max(int l, int r) { return query(1, 0, n - 1, l, r); }

    void update(int v, int tl, int tr, int idx, int new_val) {
        if (tl == tr) {
            t[v] = new_val;
            return;
        }

        int tm = (tl + tr) / 2;
        if (idx <= tm) {
            update(2 * v, tl, tm, idx, new_val);
        } else {
            update(2 * v + 1, tm + 1, tr, idx, new_val);
        }

        t[v] = max(t[2 * v], t[2 * v + 1]);
    }

    void set(int idx, int new_val) { update(1, 0, n - 1, idx, new_val); }
};

void solve(vector<int> &a, int d) {
    int n = a.size();

    int max_val = *max_element(a.begin(), a.end());

    vector<int> dp(max_val + 1);
    // At any point of time, dp[val] is the maximum length of a good
    // subsequence whose last element is val.

    SegmentTree st(max_val + 1);
    for (int i = 0; i < n; i++) {
        dp[a[i]] = 1 + st.get_max(max(0, a[i] - d), min(a[i] + d, max_val));
        st.set(a[i], dp[a[i]]);
    }
    cout << *max_element(dp.begin(), dp.end()) << endl;
}

int main() {
    int n, d;
    cin >> n >> d;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    solve(a, d);
    return 0;
}
#include <atcoder/segtree>
#include <bits/stdc++.h>

using namespace std;
using namespace atcoder;

int op(int a, int b) { return max(a, b); }

int e() { return 0; }

void solve(vector<int> &a, int d) {
    int n = a.size();

    int max_val = *max_element(a.begin(), a.end());

    vector<int> dp(max_val + 1);
    // At any point of time, dp[val] is the maximum length of a good
    // subsequence whose last element is val.

    segtree<int, op, e> st(max_val + 1);
    for (int i = 0; i < n; i++) {
        // For atcoder's segtree, prod(l, r) returns max(l ... r - 1).
        // Hence we add +1 to make inclusive range.
        dp[a[i]] = 1 + st.prod(max(0, a[i] - d), min(a[i] + d, max_val) + 1);
        st.set(a[i], dp[a[i]]);
    }
    cout << *max_element(dp.begin(), dp.end()) << endl;
}

int main() {
    int n, d;
    cin >> n >> d;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    solve(a, d);
    return 0;
}