Submission #147631

#TimeUsernameProblemLanguageResultExecution timeMemory
147631forestryksGlobal Warming (CEOI18_glo)C++14
100 / 100
396 ms12780 KiB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
#define rep(i, n) for (int (i) = 0; (i) < (n); ++(i))
#define all(x) (x).begin(), (x).end()
#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

const int MAXN = 4e5 + 5;
int n, d;
int a[MAXN];
int b[MAXN];
vector<int> vals;

int t[MAXN * 4];

void build() {
    memset(t, 0, sizeof t);
}

int get(int v, int tl, int tr, int l, int r) {
    if (r <= tl || tr <= l) return 0;
    if (l <= tl && tr <= r) return t[v];

    int tm = tl + (tr - tl) / 2;
    int a = get(v * 2 + 1, tl, tm, l, r);
    int b = get(v * 2 + 2, tm, tr, l, r);
    return max(a, b);
}

void upd(int v, int tl, int tr, int p, int x) {
    if (tr - tl == 1) {
        t[v] = max(t[v], x);
        return;
    }

    int tm = tl + (tr - tl) / 2;
    if (p < tm) {
        upd(v * 2 + 1, tl, tm, p, x);
    } else {
        upd(v * 2 + 2, tm, tr, p, x);
    }
    t[v] = max(t[v * 2 + 1], t[v * 2 + 2]);
}

int sz[MAXN];
int res = 1;

int main() {
    FAST_IO;
    cin >> n >> d;
    rep(i, n) {
        cin >> a[i];
    }

    rep(i, n) {
        b[i] = a[i] + d;
    }

    rep(i, n) {
        vals.push_back(a[i]);
        vals.push_back(b[i]);
    }

    sort(all(vals));
    vals.resize(unique(all(vals)) - vals.begin());

    rep(i, n) {
        a[i] = lower_bound(all(vals), a[i]) - vals.begin();
        b[i] = lower_bound(all(vals), b[i]) - vals.begin();
    }

    build();
    rep(i, n) {
        sz[i] = get(0, 0, vals.size(), 0, a[i]) + 1;
        upd(0, 0, vals.size(), a[i], sz[i]);
    }

    build();
    for (int i = n - 1; i >= 0; --i) {
        res = max(res, sz[i] + get(0, 0, vals.size(), a[i] + 1, vals.size()));

        int r = get(0, 0, vals.size(), b[i] + 1, vals.size()) + 1;
        upd(0, 0, vals.size(), b[i], r);
    }

    cout << res << endl;
}
#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...