Submission #1308071

#TimeUsernameProblemLanguageResultExecution timeMemory
1308071OwstinGlobal Warming (CEOI18_glo)C++20
62 / 100
34 ms5448 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define FAST_IO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0)
#define pb push_back
#define all(x) begin(x), end(x)
#define umap unordered_map
#define space " "
#define TEST_CASES int a; cin >> a; for (int i = 0; i < a; i++) {solve(); cout << endl;}
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());

void solve() {
    int n, x; cin >> n >> x;
    vector<int> vec(n);
    for (int i = 0; i < n; i++) {
        cin >> vec[i];
    }
    vector<pair<int, int>> pref(n);
    vector<int> pdp;
    for (int i = 0; i < n; i++) {
        auto it = lower_bound(all(pdp), vec[i]);
        if (it == pdp.end()) {
            pdp.pb(vec[i]);
        }
        else {
            *it = vec[i];
        }
        pref[i] = {pdp.size(), pdp.back()};
    }
    vector<pair<int, int>> suff(n);
    vector<int> ndp;
    for (int i = n - 1; i >= 0; i--) {
        auto it = lower_bound(all(ndp), -vec[i]);
        if (it == ndp.end()) {
            ndp.pb(-vec[i]);
        }
        else {
            *it = -vec[i];
        }
        suff[i] = {ndp.size(), -ndp.back()};
    }
    int res = pref[n - 1].first;
    for (int i = 1; i < n; i++) {
        if (pref[i - 1].first + suff[i].first > res && pref[i - 1].second - suff[i].second < x) {
            res = pref[i - 1].first + suff[i].first;
        }
    }
    cout << res;
}

int main() {
    FAST_IO;
    //freopen("pcb.in", "r", stdin);
    //freopen("pcb.out", "w", stdout);
    //TEST_CASES;
    solve(); cout << endl;
    /*int a; cin >> a;
    for (int i = 1; i <= a; i++){
        cout << "Case #" << i << ": ";
        solve();
        cout << 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...