Submission #1271899

#TimeUsernameProblemLanguageResultExecution timeMemory
1271899marshziinGlobal Warming (CEOI18_glo)C++20
0 / 100
246 ms22448 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pii pair<int,int>

const int maxn = 2e5 + 10;
int A[maxn];

//Seg tree
int tree[4 * maxn];
void update(int node, int l, int r, int pos, int val) {
    if(l == r) {        
        tree[node] = val;
        return;
    }

    int mid = (l + r) / 2;
    if(pos <= mid) update(2 * node, l, mid, pos, val);
    else update(2 * node + 1, mid + 1, r, pos, val);

    tree[node] = max(tree[2 * node], tree[2 * node + 1]);
    return;
}   

int query(int node, int tl, int tr, int l, int r) {
    if(tl > r || tr < l) return 0;
    if(tl >= l && tr <= r) return tree[node];

    int mid = (tl + tr) / 2;
    return max(query(2 * node, tl, mid, l, r), query(2 * node + 1, mid + 1, tr, l ,r));
}

int32_t main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
    int n, x; cin >> n >> x;
    vector<int> ordered(n);
    for (int i = 1; i <= n; i++) {
        cin >> A[i];
        ordered[i - 1] = A[i];
    }

    sort(ordered.begin(), ordered.end());
    ordered.erase(unique(ordered.begin(), ordered.end()), ordered.end());
    
    map<int,int> comp;
    int id = 1;
    for (auto x : ordered) comp[x] = id++;

    vector<int> as(n + 1);

    for (int i = n; i >= 1; i--) {
        int aux = query(1, 1, n, comp[A[i]], n + 1);
        as[i] = aux + 1;
        update(1, 1, n, comp[A[i]], aux + 1);
    }

    priority_queue<pii, vector<pii>, greater<pii>> pq;
    pq.push({-x, 0});
    int res = 0;
    vector<int> lis;
    for (int i = 1; i < n; i++) {
        while(!pq.empty() && pq.top().first <= A[i]) {
            res = max(res, pq.top().second + as[i + 1]);
            pq.pop();
        }
        
        auto it = lower_bound(lis.begin(), lis.end(), A[i]);
        if(it == lis.end()) lis.push_back(A[i]);
        else *it = A[i];
        pq.push({A[i] - x, lis.size()});
    }

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