This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ve vector
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9 + 10;
const ll LLINF = (ll)1e18 + 10;
int n, d, t;
ve<int> T;
struct value {
    int pos;
    int inc;
    pll min;
    pll seg;
};
pll calcDP(ll lambda) {
    stack<value> U;
    pll seg;
    U.push({-1, 0, {LLINF, -1}, {LLINF, -1}});
    if (T[n - 1] > t) {
        U.push({n - 1, 0, {0, 0}, {0, 0}});
        seg = {LLINF, 0};
    }
    else seg = {1, 0};
    for (int i = n - 2; i >= 0; i--) {
        pll m = min(seg, U.top().min);
        pll v = m;
        v.first += lambda;
        v.second++;
        seg = min(seg, v);
        if (T[i] > t) {
            U.push({i, 0, m, seg});
            seg = {LLINF, 0};
        }
        else {
            int diff = t - T[i];
            seg.first++;
            U.top().inc++;
            U.top().min.first++;
            U.top().seg.first++;
            while (U.size() > 1 && (U.top().pos - i) <= diff) {
                U.top().seg.first++;
                seg = min(seg, U.top().seg);
                int inc = U.top().inc + 1;
                U.pop();
                U.top().inc += inc;
                U.top().min.first += inc;
                U.top().seg.first += inc;
            }
        }
    }
    return min(seg, U.top().min);
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> d >> t;
    T = ve<int>(n);
    for (int i = 0; i < n; i++) {
        cin >> T[i];
    }
    ll lo = 0;
    ll hi = 4e6;
    ll c, x;
    while (lo != hi) {
        ll mi = (lo + hi) / 2;
        x = calcDP(mi).second;
        if (x > d) lo = mi + 1;
        else hi = mi;
    }
    tie(c, x) = calcDP(lo);
    cout << c - d * lo << endl;
    return 0;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |