Submission #1329015

#TimeUsernameProblemLanguageResultExecution timeMemory
1329015shirokitoGlobal Warming (CEOI18_glo)C++20
100 / 100
40 ms3040 KiB
#include <bits/stdc++.h>
using namespace std;

#define UP1(i, n) for (int i = 0; i < (n); i++)
#define UP2(i, a, b) for (int i = (a); i <= (b); i++)
#define UP3(i, a, b, c) for (int i = (a); i <= (b); i += (c))
#define DN1(i, n) for (int i = (n) - 1; i >= 0; i--)
#define DN2(i, a, b) for (int i = (a); i >= (b); i--)
#define DN3(i, a, b, c) for (int i = (a); i >= (b); i -= (c))
#define FOR_IMPL(_1, _2, _3, _4, NAME, ...) NAME
#define FOR_UP(...) FOR_IMPL(__VA_ARGS__, UP3, UP2, UP1) (__VA_ARGS__)
#define FOR_DN(...) FOR_IMPL(__VA_ARGS__, DN3, DN2, DN1) (__VA_ARGS__)

#define POPCOUNT(n) __builtin_popcountll(n)
#define CLZ(n) __builtin_clzll(n)
#define CTZ(n) __builtin_ctzll(n)
#define LOG(n) __lg(n)
#define BIT(n, i) (((n) >> (i)) & 1LL)
#define FLIP(n, i) ((n) ^ (1LL << (i)))
#define ON(n, i) ((n) | (1LL << (i)))
#define OFF(n, i) ((n) & ~(1LL << (i)))

#define all(x) (x).begin(), (x).end()
#define len(x) (int)x.size()

#define fi first
#define se second

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<long long, long long>;

#if __cplusplus <= 201402L
template <typename T> T gcd(T a, T b) { 
    return __gcd(a, b); 
}
template <typename T> T lcm(T a, T b) {
    return a / gcd(a, b) * b;
}
#endif

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
long long rand(long long l, long long r) {
    return uniform_int_distribution<long long>(l, r)(rng);  
}  

template <class T> void remove_duplicate(vector<T> &v) {
    sort(v.begin(), v.end()); 
    v.resize(unique(v.begin(), v.end()) - v.begin());
} 

template <class T> bool maximize(T &x, const T &y) {
    if (x < y) {
        x = y;
        return true;
    }
    return false;
}
template <class T> bool minimize(T &x, const T &y) {
    if (x > y) {
        x = y;
        return true;
    }
    return false;
}  

const int N = 2e5 + 24;
const int INF = 2e9;

int n, d, a[N], b[N];
int dpL[N], dpR[N];

int main() {
    cin.tie(0) -> sync_with_stdio(0);

    cin >> n >> d; 
    FOR_UP (i, 1, n) cin >> a[i];

    b[0] = -INF;

    int lcs = 0;
    FOR_UP (i, 1, n) {
        dpL[i] = lower_bound(b, b + lcs + 1, a[i]) - b;
        maximize(lcs, dpL[i]);
        b[dpL[i]] = a[i];
    }

    lcs = 0;
    FOR_DN (i, n, 1) {
        a[i] = -a[i];
        int R = lower_bound(b, b + lcs + 1, a[i]) - b;
        dpR[i] = lower_bound(b, b + lcs + 1, a[i] + d) - b;
        maximize(lcs, R);
        b[R] = a[i];
        // cout << R << ' ' << dpR[i] << '\n';
        // FOR_UP (i, 0, lcs) cout << b[i] << " \n" [i == lcs];
    }

    int res = 0; FOR_UP (i, 1, n) {
        // cout << i << ' ' << dpL[i] << ' ' << dpR[i] << '\n';
        maximize(res, dpL[i] + dpR[i] - 1);
    } 

    cout << res << '\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...