Submission #132834

#TimeUsernameProblemLanguageResultExecution timeMemory
132834MinnakhmetovGlobal Warming (CEOI18_glo)C++14
100 / 100
414 ms18552 KiB
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define all(aaa) aaa.begin(), aaa.end()

const int N = 2e5 + 5, INF = 2e9;
vector<int> vs, w[N];
int a[N], t[N * 4], b[N], z[N];

void upd(int x, int y, int v = 1, int L = 0, int R = vs.size() - 1) {
    if (L == R) {
        t[v] = y;
    }
    else {
        int m = (L + R) >> 1;
        if (x <= m)
            upd(x, y, v * 2, L, m);
        else
            upd(x, y, v * 2 + 1, m + 1, R);
        t[v] = max(t[v * 2], t[v * 2 + 1]);
    }
}

int que(int x, int v = 1, int L = 0, int R = vs.size() - 1) {
    if (R < x)
        return 0;
    if (L >= x)
        return t[v];
    int m = (L + R) >> 1;
    return max(que(x, v * 2, L, m), 
            que(x, v * 2 + 1, m + 1, R));
}

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

    int n, x;
    cin >> n >> x;

    for (int i = 0; i < n; i++) {
        cin >> a[i];
        vs.push_back(a[i]);
    }

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

    for (int i = n - 1; i >= 0; i--) {
        b[i] = lower_bound(all(vs), a[i]) - vs.begin();
        int lis = que(b[i] + 1) + 1;

        w[b[i]].push_back(lis);
        upd(b[i], lis);
    }

    fill(z, z + N, INF);

    z[0] = 0;

    int ans = 1;

    for (int i = 0; i < n; i++) {
        w[b[i]].pop_back();
        upd(b[i], w[b[i]].empty() ? 0 : w[b[i]].back());

        int j = lower_bound(z, z + N, a[i]) - z;
        z[j] = a[i];

        int k = upper_bound(all(vs), a[i] - x) - vs.begin();

        ans = max(ans, que(k) + j);
    }

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