Submission #1296703

#TimeUsernameProblemLanguageResultExecution timeMemory
1296703chithanhnguyenGlobal Warming (CEOI18_glo)C++20
100 / 100
150 ms13008 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define ld long double
#define pii pair<int, int>
#define fi first
#define se second
#define __builtin_popcount __builtin_popcountll
#define all(x) (x).begin(), (x).end()

#define debug(a, l, r) for (int i = (l); i <= (r); ++i) cout << (a)[i] << ' '; cout << '\n';

struct FenwickTree{
    int n; vector<int> fen;

    FenwickTree() {
    }

    FenwickTree(int _n) : n(_n) {
        fen.resize(n + 5, 0);
    }

    void reset() {
        fill(all(fen), 0);
    }

    void update(int idx, int v) {
        for (int i = idx; i <= n; i += i & -i)
            fen[i] = max(fen[i], v);
    }

    int get(int idx) {
        int res = 0;
        for (int i = idx; i; i -= i & -i)
            res = max(res, fen[i]);
        return res;
    }
};

const int MAXN = 2e5 + 5;
int n, m, x, a[MAXN], dpl[2][MAXN], dpr[MAXN];
vector<int> vals;
FenwickTree fen;

int toIdx(int x) {
    return (int)(lower_bound(all(vals), x) - vals.begin()) + 1;
}

void init() {
    cin >> n >> x;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        vals.push_back(a[i]);
        vals.push_back(a[i] + x);
    }

    sort(all(vals));
    vals.erase(unique(all(vals)), vals.end());

    m = (int)vals.size();
    fen = FenwickTree(m + 5);
}

void solve() {
    for (int i = 1; i <= n; ++i) {
        dpl[0][i] = fen.get(toIdx(a[i]) - 1) + 1;
        dpl[1][i] = fen.get(toIdx(a[i] + x) - 1) + 1;
        fen.update(toIdx(a[i]), dpl[0][i]);
    }

    fen.reset();

    for (int i = n; i >= 1; --i) {
        int newid = m - toIdx(a[i]) + 1;
        dpr[i] = fen.get(newid - 1) + 1;
        fen.update(newid, dpr[i]);
    }

    int res = 0;
    for (int i = 1; i <= n; ++i)
        res = max(res, dpl[1][i] + dpr[i] - 1);
    cout << res;
}

signed main() {
    #ifdef NCTHANH
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr); cout.tie(nullptr);

    init();
    solve();

    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...