这个题目,建议和 最佳牛围栏
一起学习
主要的算法思想就是,距离至少是 $d$
我们可以用一个大小固定的滑动窗口,去维护 $[i-d+1, i]$ 区间里的值
如果距离超过 $d$ 怎么办呢?
好在,$\min, \max$ 信息,我们可以 边扫描边维护
也就是说,扫描到一个点 $i$ ,和 $i$ 距离至少是 $d$ 的点,最大值可以实时维护,为 $maxv$
于是我们可以用 $maxv + a[i]$ 来更新全局的 $ans$
然后再把 $i-d+1$ 这个点放入集合中,去更新 $maxv$
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <bitset>
#include <assert.h>
using namespace std;
typedef long long ll;
typedef set<int>::iterator ssii;
#define Cmp(a, b) memcmp(a, b, sizeof(b))
#define Cpy(a, b) memcpy(a, b, sizeof(b))
#define Set(a, v) memset(a, v, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define _forS(i, l, r) for(set<int>::iterator i = (l); i != (r); i++)
#define _rep(i, l, r) for(int i = (l); i <= (r); i++)
#define _for(i, l, r) for(int i = (l); i < (r); i++)
#define _forDown(i, l, r) for(int i = (l); i >= r; i--)
#define debug_(ch, i) printf(#ch"[%d]: %d\n", i, ch[i])
#define debug_m(mp, p) printf(#mp"[%d]: %d\n", p->first, p->second)
#define debugS(str) cout << "dbg: " << str << endl;
#define debugArr(arr, x, y) _for(i, 0, x) { _for(j, 0, y) printf("%c", arr[i][j]); printf("\n"); }
#define _forPlus(i, l, d, r) for(int i = (l); i + d < (r); i++)
#define lowbit(i) (i & (-i))
#define MPR(a, b) make_pair(a, b)
pair<int, int> crack(int n) {
int st = sqrt(n);
int fac = n / st;
while (n % st) {
st += 1;
fac = n / st;
}
return make_pair(st, fac);
}
inline ll qpow(ll a, int n) {
ll ans = 1;
for(; n; n >>= 1) {
if(n & 1) ans *= 1ll * a;
a *= a;
}
return ans;
}
template <class T>
inline bool chmax(T& a, T b) {
if(a < b) {
a = b;
return true;
}
return false;
}
ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a % b);
}
ll ksc(ll a, ll b, ll mod) {
ll ans = 0;
for(; b; b >>= 1) {
if (b & 1) ans = (ans + a) % mod;
a = (a * 2) % mod;
}
return ans;
}
ll ksm(ll a, ll b, ll mod) {
ll ans = 1 % mod;
a %= mod;
for(; b; b >>= 1) {
if (b & 1) ans = ksc(ans, a, mod);
a = ksc(a, a, mod);
}
return ans;
}
template <class T>
inline bool chmin(T& a, T b) {
if(a > b) {
a = b;
return true;
}
return false;
}
bool _check(int x) {
//
return true;
}
int bsearch1(int l, int r) {
while (l < r) {
int mid = (l + r) >> 1;
if(_check(mid)) r = mid;
else l = mid + 1;
}
return l;
}
int bsearch2(int l, int r) {
while (l < r) {
int mid = (l + r + 1) >> 1;
if(_check(mid)) l = mid;
else r = mid - 1;
}
return l;
}
template<class T>
bool lexSmaller(vector<T> a, vector<T> b) {
int n = a.size(), m = b.size();
int i;
for(i = 0; i < n && i < m; i++) {
if (a[i] < b[i]) return true;
else if (b[i] < a[i]) return false;
}
return (i == n && i < m);
}
// ============================================================== //
const int maxn = 200000 + 10;
class A {
public:
int a, b;
A() = default;
A(int a, int b) : a(a), b(b) {}
bool operator< (const A &rhs) const {
return a < rhs.a;
}
} bank[maxn];
int n, d;
int solve() {
sort(bank+1, bank+1+n);
int st = bank[1].a, to = st + d;
int maxv = bank[1].b;
int i = 1, j = 1;
while (i <= n && bank[i].a < to) i++;
int ans = 0;
for (; i <= n; i++) {
ans = max(ans, bank[i].b + maxv);
while (j < i && bank[i].a - bank[j].a + 1 >= d) maxv = max(maxv, bank[j].b), j++;
}
return ans;
}
void init() {
//
}
int main() {
//freopen("input.txt", "r", stdin);
scanf("%d%d", &n, &d);
_rep(i, 1, n) scanf("%d%d", &bank[i].a, &bank[i].b);
// then solve
int ans = solve();
printf("%d\n", ans);
}