AcWing 782. 避嫌抢劫
原题链接
中等
作者:
也许
,
2021-03-14 20:48:36
,
所有人可见
,
阅读 314
#include <iostream>
#include <algorithm>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 200010;
PII a[N];
int n, d;
int main()
{
scanf("%d%d", &n, &d);
for (int i = 0; i < n; i++) scanf("%d%d", &a[i].x, &a[i].y);
sort(a, a + n);
int maxv = 0;
int res = 0;
for (int i = -1, j = 1; j < n; j++)
{
while (j > i + 1 && a[j].x - a[i + 1].x >= d)
{
maxv = max(maxv, a[i + 1].y);
i++;
}
if (i != -1 && a[j].x - a[i].x >= d) res = max(res, a[j].y + maxv);
}
cout << res << endl;
return 0;
}