Submission #678801

#TimeUsernameProblemLanguageResultExecution timeMemory
678801bebraGlobal Warming (CEOI18_glo)C++17
100 / 100
655 ms24584 KiB
#include <bits/stdc++.h>
using namespace std;

#define dbg(x) cerr << #x << ": " << x << endl;

struct FenwickTree {
    vector<int> tree;
    int size;

    FenwickTree(int n) {
        size = n;
        tree.resize(size);
    }

    void update(int pos, int value) {
        for (int i = pos; i < size; i |= i + 1) {
            tree[i] = max(tree[i], value);
        }
    }

    int query(int pos) {
        if (pos >= size) return 0;
        if (pos < 0) return 0;
        int res = 0;
        for (int i = pos; i >= 0; i = (i & (i + 1)) - 1) {
            res = max(res, tree[i]);
        }
        return res;
    }

    void clear() {
        tree.assign(size, 0);
    }
};


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    vector<int> b;
    b.reserve(n * 3);
    for (auto& x : a) {
        cin >> x;
        b.push_back(x);
        b.push_back(x + k);
    }
    sort(b.begin(), b.end());
    b.resize(unique(b.begin(), b.end()) - b.begin());
    int m = b.size();
    map<int, int> compress;
    for (int i = 0; i < m; ++i) {
        compress[b[i]] = i;
    }
    int ans = 0;
    FenwickTree tree(m);
    vector<int> pref_dp(n);
    for (int i = 0; i < n; ++i) {
        pref_dp[i] = tree.query(compress[a[i]] - 1) + 1;
        tree.update(compress[a[i]], pref_dp[i]);
        ans = max(ans, pref_dp[i]);
    }
    tree.clear();
    vector<int> suf_dp(n);
    for (int i = n - 1; i >= 0; --i) {
        suf_dp[i] = tree.query(m - compress[a[i]] - 2) + 1;
        tree.update(m - compress[a[i]] - 1, suf_dp[i]);
    }
    tree.clear();
    for (int i = 0; i < n; ++i) {
        ans = max(ans, suf_dp[i] + tree.query(compress[a[i] + k] - 1));
        tree.update(compress[a[i]], pref_dp[i]);
    }
    cout << ans << '\n';
    return 0;
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...