#include <bits/stdc++.h>
using namespace std;
int n,m,now;
struct node {
int a,b,ut;
}a[100010];
bool operator<(const node &lhs, const node &rhs) {
return lhs.ut<rhs.ut; // 按时间从小到大排序
}
priority_queue<node> b;
int main() {
cin >> n >> m; // 读取 n 和 m
now = 0; // 初始化 now 为 0
for (int i = 1; i <= n; i++) {
cin >> a[i].a >> a[i].b;
}
for (int i = 1; i <= n; i++) {
a[i].ut = abs(a[i].a - now) + a[i].b; // 计算时间
if (m >= a[i].ut) { // 如果时间足够
b.push(a[i]);
m -= a[i].ut;
now = a[i].a; // 更新当前位置
continue;
}
else { // 时间不够
if (!b.empty() && a[i].ut <= (b.top().ut)) {
m+=b.top().ut;
b.pop();
m-=a[i].ut;
b.push(a[i]);
}
}
}
int ans = 0;
while (!b.empty()) {
b.pop();
ans++;
}
cout << ans << endl;
return 0;
}
思想:半DP半贪心
只要往前走,然后遇到了就拿下,拿不下看看和最大的谁快