Submission #1125813

#TimeUsernameProblemLanguageResultExecution timeMemory
1125813vladiliusGlobal Warming (CEOI18_glo)C++20
100 / 100
349 ms7636 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second

struct ST{
    vector<int> t;
    int n;
    ST(int ns){
        n = ns;
        t.resize(4 * n);
    }
    void upd(int v, int tl, int tr, int& p, int& x){
        if (tl == tr){
            t[v] = max(t[v], x);
            return;
        }
        int tm = (tl + tr) / 2, vv = 2 * v;
        if (p <= tm){
            upd(vv, tl, tm, p, x);
        }
        else {
            upd(vv + 1, tm + 1, tr, p, x);
        }
        t[v] = max(t[vv], t[vv + 1]);
    }
    void upd(int p, int x){
        upd(1, 1, n, p, x);
    }
    int get(int v, int tl, int tr, int& l, int& r){
        if (l > tr || r < tl) return 0;
        if (l <= tl && tr <= r) return t[v];
        int tm = (tl + tr) / 2, vv = 2 * v;
        return max(get(vv, tl, tm, l, r), get(vv + 1, tm + 1, tr, l, r));
    }
    int get(int l, int r){
        if (l > r) return 0;
        return get(1, 1, n, l, r);
    }
    void clear(){
        fill(t.begin(), t.end(), 0);
    }
};

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    int n, X; cin>>n>>X;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; i++){
        cin>>a[i];
    }
    
    vector<int> all;
    for (int i = 1; i <= n; i++){
        all.pb(a[i]);
    }
    sort(all.begin(), all.end());
    
    vector<int> vv = {0};
    int i = 0;
    while (i < n){
        int j = i;
        while (j < n && all[i] == all[j]){
            j++;
        }
        vv.pb(all[i]);
        i = j;
    }
    
    vector<int> :: iterator it;
    
    
    vector<int> D1(n + 1);
    ST T(n);
    for (int i = 1; i <= n; i++){
        it = lower_bound(vv.begin(), vv.end(), a[i]);
        int j = (int) (it - vv.begin());
        
        D1[i] = 1 + T.get(1, j - 1);
        T.upd(j, D1[i]);
    }
    
    vector<int> D2(n + 1);
    T.clear();
    for (int i = n; i > 0; i--){
        it = lower_bound(vv.begin(), vv.end(), a[i]);
        int j = (int) (it - vv.begin());
        
        D2[i] = 1 + T.get(j + 1, n);
        T.upd(j, D2[i]);
    }
    
    int out = 0;
    T.clear();
    for (int i = n; i > 0; i--){
        it = upper_bound(vv.begin(), vv.end(), a[i] - X);
        int j = (int) (it - vv.begin());
        
        out = max(out, D1[i] + T.get(j, n));
        
        it = lower_bound(vv.begin(), vv.end(), a[i]);
        j = (int) (it - vv.begin());
        T.upd(j, D2[i]);
    }

    T.clear();
    for (int i = 1; i <= n; i++){
        it = lower_bound(vv.begin(), vv.end(), a[i] + X);
        int j = (int) (it - vv.begin());

        out = max(out, T.get(1, j - 1) + D2[i]);
        
        it = lower_bound(vv.begin(), vv.end(), a[i]);
        j = (int) (it - vv.begin());
        
        T.upd(j, D1[i]);
    }

    cout<<out<<"\n";
}
#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...