#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1e9 + 7;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cout << fixed << setprecision(0);
ll n, d; cin >> n >> d;
map<ll, vector<pair<ll, ll>>> stations;
for (ll i = 0; i < n; i++) {
ll x, a, b; cin >> x >> a >> b;
stations[x].push_back({a, b});
}
function<bool(ll)> works = [&](ll f) {
ll curr = f, last = 0;
bool valid = true;
for (auto [pos, lst]: stations) {
if (pos - last <= curr) {
curr -= pos - last;
ll mxf = curr;
for (auto [a, b]: lst)
mxf = max(mxf, curr + a);
curr = mxf;
last = pos;
} else {
valid = false;
break;
}
}
if (d - last <= curr && valid) return true;
return false;
};
ll low = 0, high = d;
ll ans = d;
while (low <= high) {
ll mid = (low + high) / 2;
if (works(mid)) ans = min(ans, mid), high = mid - 1;
else low = mid + 1;
}
cout << ans;
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |